Home > Software design >  How to control an argument of a function depending on a radiobutton choice in R shiny
How to control an argument of a function depending on a radiobutton choice in R shiny

Time:10-26

This is a follow-up to this enter image description here

CodePudding user response:

The way you use the if will not work to set the functions argument. Instead use a single if for each argument, e.g. prunesmaller = if (input$smaller_bigger == "smaller") input$prune.

Note: Maybe I missed something, but I got an error when trying to set prunebigger and according to the docs there is no prunebigger argument.

library(shiny)
library(vtree)


# Define UI ----
ui <- pageWithSidebar(

  # App title ----
  headerPanel("Cyl vtree"),

  # Sidebar panel for inputs ----
  sidebarPanel(
    radioButtons("smaller_bigger", h3("Prune smaller or bigger?"), choices = c("smaller", "bigger"), inline = TRUE),
    sliderInput(inputId = "prune", label = "Number to prune?", step = 10, min = 0, max = 100, value = 0),
    selectizeInput("level", label = "Level", choices = NULL, multiple = TRUE),
    # This line is the only change from the original code
    selectizeInput("values", label = "Values", choices = NULL, multiple = TRUE),
  ),

  # Main panel for displaying outputs ----
  mainPanel(
    vtreeOutput("VTREE")
  )
)

# Define server logic to plot ----
server <- function(input, output, session) {
  df <- reactiveVal(mtcars)
  vector <- c("cyl", "vs", "am", "gear")

  observe({
    updateSelectizeInput(session, "level", choices = colnames(df()[vector]), selected = NULL)
    updateSelectizeInput(session, "values", choices = unique(df()$cyl))
  })

  output[["VTREE"]] <- renderVtree({
    vtree(df(), c(input$level),
      sameline = TRUE,
      follow = list(cyl = input$values),
      prunesmaller = if (input$smaller_bigger == "smaller") input$prune
      #prunebigger = if (input$smaller_bigger == "bigger") input$prune
    )
  })
}

shinyApp(ui, server)

enter image description here

enter image description here

  • Related