Home > front end >  Combining NULL and numeric value in Shiny
Combining NULL and numeric value in Shiny

Time:01-09

I would like to replicate the following R code in Shiny

if ((is.null(num.iters)) || (num.iters == 1))

I tried this but I get an

character(0)
Error: missing value where TRUE/FALSE needed

using

        numericInput(inputId = "num.iters",
                            label = "Number of iterations to run (num.iters)",
                            value = NULL,
                            min = 1,
                            max = 1)

The problem is that NULL is not numeric. The intent is for NULL to run all iterations. Unfortunately, how many iterations to run is not known in advance since my app involves stochastic simulations. I saw this post, which involves a similar problem: How do I use the NULL Value as a variable call in R Shiny

but I want to combine NULL and a numeric value with only one input label, which is a bit different than the response to the linked post.

Is this possible?

CodePudding user response:

numericInput default to NA when it's empty. Maybe a workaround can be:

library(shiny)

ui <- fluidPage(
  numericInput(
    inputId = "num.iters",
    label = "Number of iterations to run (num.iters)",
    value = NA,
    min = 1,
    max = 1
  )
)

server <- function(input, output, session) {
  observe({
    if ((is.na(input$num.iters)) || (input$num.iters == 1)) {
      print(as.null(input$num.iters))
    }
  })
}

shinyApp(ui, server)
  •  Tags:  
  • Related