firmo23
firmo23

Reputation: 8404

Get rid of 'argument is of length zero' error message in R console

I have the shiny app below in which 2 numeric inputs are displayed. The app works fine since when the sum is not 40 an error message is displayed. What is annoying and I would like to get rid off is the error message

Warning: Error in if: argument is of length zero

which appears in the r console when I run the app for the 1st time. I know that this comes from line 38 and has to do with the NULL values in the beginning. The interesting thing is thatthis error message is not displayed when I do not use renderUI() for the 2 numeric inputs. But I need them to be like this in my actual case.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      #This hides the temporary warning messages while the plots are being created
      tags$style(type="text/css",
                 ".shiny-output-error { visibility: hidden; }",
                 ".shiny-output-error:before { visibility: hidden; }"
      ),
      uiOutput("factors_weight_one_two"),
      htmlOutput('weight_message')
    ),
    mainPanel(

    )
  )
)

server <- function(input, output) {
  output$factors_weight_one_two <- renderUI({
    fluidRow(
      column(6, numericInput(
        "factors_weight_one",
        "Factor 1", 20, 
        min = 1, max = 100,
        width = "90%")),
      column(6, numericInput(
        "factors_weight_two",
        "Factor 2", 20, 
        min = 1, max = 100,
        width = "90%"))
    )
  })
  output$weight_message <- renderText({

      if(!is.null(as.numeric(input$factors_weight_one) + as.numeric(input$factors_weight_two) ) & as.numeric(input$factors_weight_one) + as.numeric(input$factors_weight_two)  != 40){
        sprintf('<font color="%s">%s</font>', 'red', "Your weights don't sum to 40")
      }  else {
        sprintf('<font color="%s">%s</font>', 'red', "")
      }

  })

}

shinyApp(ui, server)

Upvotes: 1

Views: 1483

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

What about changing the server part to this

server <- function(input, output) {
  output$factors_weight_one_two <- renderUI({
    fluidRow(
      column(6, numericInput(
        "factors_weight_one",
        "Factor 1", 20,
        min = 1, max = 100,
        width = "90%")),
      column(6, numericInput(
        "factors_weight_two",
        "Factor 2", 20,
        min = 1, max = 100,
        width = "90%"))
    )
  })
  output$weight_message <- renderText({

      req(input$factors_weight_one, input$factors_weight_two)

      if (input$factors_weight_one + input$factors_weight_two  != 40) {
        sprintf('<font color="%s">%s</font>', 'red', "Your weights don't sum to 40")
      }  else {
        sprintf('<font color="%s">%s</font>', 'red', "")
      }

  })

}

I'm using req to check for "truthy-ness" of input$factors_weight_one and input$factors_weight_two. BTW, you shouldn't need as.numeric for the return input of numericInput because it is already numeric.

Upvotes: 1

Related Questions