3

I have an app where you input your own data and then there are two select inputs where the user can select their dependent variable (column from inputted data) and multiple independent variables. I want to make it so if they select a column from their data that has a null value, an error message will immediately pop up telling them there is a null value. This is because if there is a null value then the app will not run and crash out when they hit the run button.

My first thought was to use the validate command like this:

validate(
 need(is.na(input$yvariable), "Error: null value detected in variable")
 ))  

(input$yvariable being the dependent variable)

However, this has not seemed to do anything when I apply it. I also tried using if-else statements to hide a helptext in the UI but also had no luck there. Again, it is important that the error pops up as soon as they select a column with any null values. Is there any clear way to pull this off? Has anyone done something similar?

Thanks!

Sawyer Keels
  • 47
  • 1
  • 6
  • Welcome to stackoverflow! `Validate()` works in general. Why it doesnt work in your code can only be answered with your code :) We can only guess that `is.na(input$yvariable)` doesnt return true. Maybe you are looking for `is.null()`, maybe `is.na(input$yvariable)` is an array,.... – Tonio Liebrand Aug 07 '18 at 19:07

1 Answers1

5

Here is a start code you can use to build on top of it. The following code is using showModal to pop up a message when there is a missing value, you can customize this message as you want. Another option would be using shinyjs::disable to disable the Run button when there is a missing value.

  library(shiny)
  ui <- fluidPage(
    uiOutput('inVar'),
    textOutput("textsummary")
  )

  server <- function(input, output, session) {

    df <- mtcars

    df$disp[3:8]<-NA

    observe(print(df[,input$DepVar]))

    output$inVar <- renderUI({
      selectInput(inputId = "DepVar", label = h4("Select variables:"), choices =  colnames(df))
    })

    output$textsummary <- renderText({
      paste("Missing of",input$DepVar, " is ", sum(is.na(df[,input$DepVar])))
    })

    observe({
      if(sum(is.na(df[,input$DepVar]))>0)
      showModal(modalDialog(
        title = "Somewhat important message",
        "This is a somewhat important message.",
        easyClose = TRUE,
        footer = NULL
      ))
    })

  }

  shinyApp(ui, server) 
A. Suliman
  • 12,923
  • 5
  • 24
  • 37