Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate uploaded csv in Shiny

Tags:

r

shiny

I have a Shiny App that uploads a csv file that uploads a csv file.

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)

What I want to do is if the data does not meet certain criteria (it must have a column named ID) then the upload fails with error message, and if the data is correct an action button appears for processing (only appears if data is fine else is hidden.

Thx!

like image 515
EGM8686 Avatar asked Sep 01 '25 03:09

EGM8686


1 Answers

You can use showModal to show warning messages and you can use renderUI to create button once you verify the file. I create one more button called uploa dfile so that I can use this to trigger an observeEvent where I can run an 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)
like image 164
Mohan Govindasamy Avatar answered Sep 02 '25 18:09

Mohan Govindasamy