Generating a text-string using checkboxGroupInput in Shiny for another function

Hi,

I want to use an interactive app to show the output of some experimental logistic regression in in shiny. I thought the best way to do this is to use the checkboxGroupInput to select which independent factors to use in the regression model. However, I am a bit flummoxed by how best to extract the choices into a string that would then feed into the model. Ultimately, the output would be someway of displaying the odds ratio etc of each factor. To illustrate:

# The input

checkboxGroupInput("fct_choice", label = "Independent Factors",
                                                      choices = list("ASA" = "ASA",
                                                                     "Frailty" = "Frailty",
                                                                     "Surgical Approach" = "Sx_Approach",
                                                                     "Acute Kidney Injury" = "AKI")
                                                      )

Ideally, the user of the app can select any combination of the choices above. I thought something like a paste function with reactive might be used, but I am struggling to work out how to put this together when not all the choices are ticked.

For example, the below only works when all the factors are ticked (I am using renderText just to see what I would get).

output$value <- renderText({
        
        value_factors <- input$fct_choice
        
        list_factors <- paste(value_factors[[1]], value_factors[[2]], value_factors[[3]], value_factors[[4]], sep = "+" )
        
        print(list_factors)
        
    })

I would appreciate any suggestions, I thought maybe this can be solved with purr but I am not familiar with this.

is this what you expected?

library(shiny)

ui <- fluidPage(
  checkboxGroupInput("fct_choice", label = "Independent Factors",
                     choices = list("ASA" = "ASA",
                                    "Frailty" = "Frailty",
                                    "Surgical Approach" = "Sx_Approach",
                                    "Acute Kidney Injury" = "AKI")
  ),
  textOutput('value')
)

server <- function(input, output, session) {
  output$value <- renderText({
    req(input$fct_choice)
    value_factors <- paste(input$fct_choice, collapse = '+')
    
    # list_factors <- paste(value_factors[[1]], value_factors[[2]], value_factors[[3]], value_factors[[4]], sep = "+" )
    
    print(value_factors)
    
  })
}

shinyApp(ui, server)
1 Like

Hi,

That looks great, I won't have a chance to try for a few days but thanks for taking the time to reply. I've not used collapse before but definitely looks like it would work.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.