Adjust so that the condition panel appears only when a correct file is loaded

£可爱£侵袭症+ 提交于 2021-02-10 14:44:42

问题


I inserted a condition panel so that as soon as the file is loaded by fileInput. It is working, however, I would like this panel to be shown only when the correct file is loaded. As it is, even with the wrong format files, the condition panel appears, as shown in the image. Can someone help me?? The executable code is below:

Thank you!

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),
                          
                          conditionalPanel(
                            "output.fileUploaded == true",
                            tags$p(h3("Are you satisfied?")),
                            tags$b(h5("(a) Choose others filters")),
                            tags$b(h5("(b) Choose number of clusters"))), 
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                        
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL,clear=FALSE)
  
  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"
      )
      
    }
    v$clear <- TRUE 
  })
  
  
  Modelcl<-reactive({if (!is.null(v$df)) {
    function.cl(v$df,input$Slider)
  }
  })
  
  
  output$fileUploaded <- reactive({
    v$clear
  })
  outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
}

shinyApp(ui = ui, server = server)


回答1:


Add return(NULL) after the sendSweetAlerts:

sendSweetAlert(
  session = session,
  title = "Error !!",
  text = "You Need 3 files, '*.shp', '*shx' and '*.dbf'",
  type = "error"
)
return(NULL)

and

  sendSweetAlert(
    session = session,
    title = "Error !!",
    text = "Wrong File",
    type = "error"
  )
  return(NULL)

The return(NULL) statement interrupts the code in the observer. In this way the statement v$clear <- TRUE will not be executed when a sweet alert is sent.



来源:https://stackoverflow.com/questions/62641030/adjust-so-that-the-condition-panel-appears-only-when-a-correct-file-is-loaded

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