Insert warning message in Shiny

眉间皱痕 提交于 2020-07-09 12:03:48

问题


I would like to insert a alert message when the file inserted in fileInput was different from ".xlsx", ".shp", ".shx", ".dbf". Can you help me? I entered an executable code below. You can even see it in my observeEvent (input$data, which I inserted something similar, but I would like it to be presented as a text box in Shiny.

Thank you!

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)

回答1:


As @Stéphane Laurent already pointed out in the comments, shinyWidgets can be used to display sweet alerts. For some inputs the {shinyFeedback} package will work as well, however, fileInputs are not supported yet.

Below is one possible implementation using sendSweetAlert replacing your print calls.

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)


来源:https://stackoverflow.com/questions/62613323/insert-warning-message-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!