Search code examples
rinputshinyselectinput

How to remove "" from shiny selectInput?


I want to directly use some selectInput in a boolean way. Is there a possibility to do so?

Have a look at my minimal example:

ui <- fluidPage(
  selectInput("in", "some input", choices = c("0"=F, "1"=T))
)

server <- function(input, output, session) {
 test_data <- read.csv("testfile",
                       header= input$in,
                       sep= ";")
}

Normal tricks to remove quotes (as desribed here) won't do the trick. I've also tried to force R to treat the input's output to be logical (via as.logical) and I've tried something simple as print(..., quote=F). Nothing worked...


Solution

  • in is a reserved word in R. You can still refer to the input name with backticks to avoid an error. Also, input[['in']] will work too. Finally we can use as.logical to convert the string into a boolean.

    app:

    note: replace 'PATHTOFILE' before running the app.

    library(shiny)
    
    ui <- fluidPage(
      selectInput("in", "some input", choices = c("0"=F, "1"=T))
    )
    
    server <- function(input, output, session) {
      
      test_data <- reactive({
        read.csv("PATHTOFILE",
                            header= as.logical(input$`in`),
                            sep= ";")})
      
      observe({
        print(input$`in`)
        print(head(test_data()))
      })
    }
    
    shinyApp(ui, server)