1

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...

micsky
  • 113
  • 12
  • 2
    Values from `selectInput` will always be character. That's just how web/HTML/javascript works. Just leave `choices=c("F","T")` and then check `input$in=="T"` to get a logical value. – MrFlick Jul 14 '21 at 19:14
  • @MrFlick Thanks, that worked well. Can you also tell me how to display some other labels than F or T? What if I want to dipslay serveral different choices, like 0,1,2,3 while 0 stands for FALSE the other three (3) stand for True? – micsky Jul 14 '21 at 19:32
  • 1
    Just put whatever you want in the `choices=`. If all the others are true, just test `input$in != "0"` or whatever the false value is. – MrFlick Jul 14 '21 at 19:34

1 Answers1

1

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)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23