首页 > 解决方案 > 在 Shiny 中插入警告信息

问题描述

当 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)

标签: rshiny

解决方案


正如@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)

推荐阅读