Show different feedback warnings depending on single user input R Shiny

Hello fellow programmers!

I want to show a user a feedback warning for the following scenarios:

  1. The total number of files uploaded does not equal 23
  2. The file type is the wrong format
  3. Both 1 and 2.

I tried using if else statements but can only get the first one to work.

library(shiny)
library(tools)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(),
  fileInput("site",
            "Upload .json",
            accept = ".json",
            multiple = T),
  actionButton("submit", "Submit"),
  textOutput("validate")
)

server <- function(input, output, session){
  validateJSON <- reactive({
    json <- input$site
    json$filetype <- file_ext(json$datapath)
    totalFiles <- nrow(json)
    
    if(totalFiles != 23){
      shinyFeedback::feedbackWarning("site",
                                     show = totalFiles != 23,
                                     "Total .json files must equal 23.")
    } else if (json$filetype != "json"){
      shinyFeedback::feedbackWarning("site",
                                     show = json$filetype != "json",
                                     "Wrong file format! .json files only")
    } else if ((totalFiles !=23) & (json$filetype != "json")) {
      shinyFeedback::feedbackWarning("site",
                                     show = (totalFiles != 23) & (json$filetype != "json"),
                                     "Wrong file type - 23 total .json files needed.")
    }
  })
  validateWhenClick <- eventReactive(input$submit, validateJSON())
  output$validate <- renderPrint(validateWhenClick())
}

shinyApp(ui, server)

Anybody know what I'm doing wrong? Thank you so much!

Hi @HABSaremyjam. Below is one way to achieve the desired outcome. Rather than using if and else if statements, I modified validateJSON to be a series of three separate if statements. When any of the statements are TRUE, the object Text, which is initially set to NULL, is overwritten with the desired message. Finally, if Text is no longer NULL, the feedback warning is called.

validateJSON <- reactive({
    json <- input$site
    json$filetype <- file_ext(json$datapath)
    totalFiles <- nrow(json)
    
    # default Text value
    Text = NULL
    
    # logic checks with associated Text updates
    if (totalFiles != 23){
      Text = "Total .json files must equal 23."
    }
    
    if (any(json$filetype != "json")){
      Text = "Wrong file format! .json files only"
    }
    
    if (totalFiles !=23 & any(json$filetype != "json")) {
      Text = "Wrong file type - 23 total .json files needed."
    }
    
    # show warning when one of the logic checks is true (i.e. Text is no longer NULL)
    if(!is.null(Text)) {
      feedbackWarning("site", show = T, Text)
      }
    
  })
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.