Shiny - conditionalPanel - set condition as output from server

前端 未结 1 603
礼貌的吻别
礼貌的吻别 2021-01-27 05:29

I am trying to build an app in shiny that will be able to load a dataset in the server function and then based on the user choose and then if there is a factor variable to open

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 05:40

    You were almost there, you need to put the outputOptions after the declaration of factorflag. Just reengineered a bit your code:

    library(shiny)
    library(caret)
    
    ui <- fluidPage(
      selectInput('dataset', 'Select Dataset', 
                  list(GermanCredit = "GermanCredit",
                       cars = "cars")),
    
      conditionalPanel(
        condition = "output.factorflag == true",
        checkboxInput("UseFactor", "Add Factor Variable")
      ) 
    )
    
    
    server <- function(input, output) {
      # Loading the dataset
      df <- reactive({
        if(input$dataset == "GermanCredit"){
          data("GermanCredit")
          GermanCredit
        }else {
          data("cars")
          cars
        }
      })
      output$factorflag <- reactive("factor" %in% sapply(df(),class))
      outputOptions(output, "factorflag", suspendWhenHidden = FALSE)
    }
    
    shinyApp(ui = ui, server = server)
    

    0 讨论(0)
提交回复
热议问题