R 在文本中插入警告消息

R 在文本中插入警告消息,r,shiny,R,Shiny,我想在fileInput中插入的文件与“.xlsx”、“.shp”、“.shx”、“.dbf”不同时插入一条警告消息。你能帮助我吗?我在下面输入了一个可执行代码。您甚至可以在我的observeEvent(input$data)中看到它,我插入了一些类似的内容,但我希望它以文本框的形式显示在Shiny中 谢谢大家! library(shiny) library(ggplot2) library(shinythemes) library(rdist) library(openxlsx) libra

我想在fileInput中插入的文件与“.xlsx”、“.shp”、“.shx”、“.dbf”不同时插入一条警告消息。你能帮助我吗?我在下面输入了一个可执行代码。您甚至可以在我的observeEvent(input$data)中看到它,我插入了一些类似的内容,但我希望它以文本框的形式显示在Shiny中

谢谢大家!

library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
library(openxlsx) 
library(geosphere)
library(rgdal)

function.cl<-function(df,k){
 
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Excel or Shapefile import"),
                                accept = c(".xlsx",".shp",".shx",".dbf"),
                                multiple= T),  
                      sidebarLayout(
                        sidebarPanel(
                          
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 4, value = 3)
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                        
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL)
  
  observeEvent(input$data, {
    if(any(grepl(".xlsx",input$data$name))){
      v$df <- read.xlsx(input$data$datapath) 
    }else if(any(grepl(".shp",input$data$name))){
      shpDF <- input$data
      failed <- F
      if(!any(grepl(".shx",input$data$name))){
        failed<-T
      }
      
      if(!any(grepl(".dbf",input$data$name))){
        failed<-T
      }
      
      if(failed){
        print("You Need 3 files, '*.shp', '*shx' and '*.dbf'")
      }else{
        prevWD <- getwd()
        uploadDirectory <- dirname(shpDF$datapath[1])
        setwd(uploadDirectory)
        for (i in 1:nrow(shpDF)){
          file.rename(shpDF$datapath[i], shpDF$name[i])
        }
        shpName <- shpDF$name[grep(x=shpDF$name, pattern="*.shp")]
        shpName<-substr(shpName,1,nchar(shpName)-4)
        
        setwd(prevWD)
        shpFile<-readOGR(dsn=uploadDirectory,layer=shpName)
        
        v$df<-shpFile@data
      } 
    }else{
      print("Wrong File")
    }
  })
  
  
  Modelcl<-reactive({if (!is.null(v$df)) {
    function.cl(v$df,input$Slider)
  }
  })
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(shinythemes)
图书馆(rdist)
库(openxlsx)
图书馆(地球圈)
图书馆(rgdal)

function.cl正如@Stéphane Laurent在评论中指出的那样,
shinyWidgets
可以用来显示甜蜜的警报。对于某些输入,{shinyFeedback}包也可以工作,但是,
fileInput
s还不受支持

下面是一个可能的实现,使用
sendSweetAlert
替换
print
调用

library(shiny)
library(shinyWidgets)
library(ggplot2)
library(shinythemes)
library(rdist)
library(openxlsx) 
library(geosphere)
library(rgdal)

function.cl<-function(df,k){
  
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Excel or Shapefile import"),
                                accept = c(".xlsx",".shp",".shx",".dbf"),
                                multiple= T),  
                      sidebarLayout(
                        sidebarPanel(
                          
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 4, value = 3)
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                        
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL)
  
  observeEvent(input$data, {
    if(any(grepl(".xlsx",input$data$name))){
      v$df <- read.xlsx(input$data$datapath) 
    }else if(any(grepl(".shp",input$data$name))){
      shpDF <- input$data
      failed <- F
      if(!any(grepl(".shx",input$data$name))){
        failed<-T
      }
      
      if(!any(grepl(".dbf",input$data$name))){
        failed<-T
      }
      
      if(failed){
        
        sendSweetAlert(
          session = session,
          title = "Error !!",
          text = "You Need 3 files, '*.shp', '*shx' and '*.dbf'",
          type = "error"
        )
        
      }else{
        prevWD <- getwd()
        uploadDirectory <- dirname(shpDF$datapath[1])
        setwd(uploadDirectory)
        for (i in 1:nrow(shpDF)){
          file.rename(shpDF$datapath[i], shpDF$name[i])
        }
        shpName <- shpDF$name[grep(x=shpDF$name, pattern="*.shp")]
        shpName<-substr(shpName,1,nchar(shpName)-4)
        
        setwd(prevWD)
        shpFile<-readOGR(dsn=uploadDirectory,layer=shpName)
        
        v$df<-shpFile@data
      } 
    }else{
      sendSweetAlert(
        session = session,
        title = "Error !!",
        text = "Wrong File",
        type = "error"
      )
    
      }
  })
  

  Modelcl<-reactive({if (!is.null(v$df)) {
    function.cl(v$df,input$Slider)
  }
  })
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(shinyWidgets)
图书馆(GG2)
图书馆(shinythemes)
图书馆(rdist)
库(openxlsx)
图书馆(地球圈)
图书馆(rgdal)

function.cl您可以使用
shinyWidgets
package触发所谓的sweet警报。shinyFeedback是另一个选项。您好TimTeaFan,您对这个问题有何想法: