【问题标题】:How to validate uploaded csv in Shiny如何在 Shiny 中验证上传的 csv
【发布时间】:2023-04-10 21:43:01
【问题描述】:

我有一个上传 csv 文件的 Shiny 应用程序。

library(shiny)

ui <- fluidPage(
    fileInput("file_input","Choose your file in csv"),            
    textOutput("choose")
)

server <- function(input, output) {
    
    output$choose <- reactive({
        if(is.null(input$file_input))
        {
            "No input given yet."
        }
        else
        {
            "Action Button Appears!"
        }
    })
    
}

shinyApp(ui = ui, server = server)

我想要做的是,如果数据不符合特定条件(它必须有一个名为 ID 的列),则上传失败并显示错误消息,如果数据正确,则会出现一个操作按钮进行处理(仅在数据很好,否则隐藏。

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用showModal 显示警告消息,并且您可以在验证文件后使用renderUI 创建按钮。我又创建了一个名为 uploa dfile 的按钮,以便我可以使用它来触发observeEvent,我可以在其中运行if else

    library(shiny)
    library(tidyverse)
    
    ui <- fluidPage(
      fileInput("file_input", "Choose your file in csv",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      actionButton("p_file","Upload File"),
      uiOutput("button_rui")
    )
    
    server <- function(input, output) {
      
      observeEvent(input$p_file,{
        if(is.null(input$file_input$datapath)){
          showModal(modalDialog(
            title = "Warning",
            "Please upload a csv file",
            easyClose = TRUE
          ))
        } else{
          input$file_input$datapath %>% 
            read_csv() -> df
          
          if("ID" %in% colnames(df)){
            output$button_rui <- renderUI(
              actionButton("pp_file","Process File"),
            )
          } else {
            showModal(modalDialog(
              title = "Warning",
              "File is not of correct format",
              easyClose = TRUE
            ))
          }
          
        }
      })
      
    
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2013-11-30
      • 2015-05-25
      • 2012-04-03
      • 1970-01-01
      • 2020-01-15
      • 2019-05-02
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多