简体   繁体   中英

shinyjs::toggleState does not work with input produced in renderUI

In the following app, users are supposed to upload a csv dataset file and select a column for further processing. The columns to choose from will of course depend on the uploaded file. Therefore, the dropdown that implements the choices is dynamically generated via renderUI .

I want to use shinyjs::toggleState to disable the dropdown menu until the dataset is actually uploaded. Unfortunately, this does not work. Does somebody know why and/or has a workaraound?

Many thanks!

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  fileInput("file", "Choose a File"),
  uiOutput("select")
)

server <- function(input, output) {
  dataset <- reactive({
    if(is.null(input$file))
      NULL
    else
      read.csv(input$file$datapath)
  })
  output$select <- renderUI({
    if(is.null(dataset()))
      choices <- "this should not be selectable!"
    else
      choices <- colnames(dataset())
    selectizeInput("choose", "Choose a Column", choices=choices)
  })
  observe({
    toggleState("choose", !is.null(dataset()))
  })
}

shinyApp(ui=ui, server=server)
library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  fileInput("file", "Choose a File"),
  uiOutput("select")
)

server <- function(input, output) {
  dataset <- reactive({
    if(is.null(input$file))
      NULL
    else
      read.csv(input$file$datapath)
  })
  output$select <- renderUI({
    if(is.null(dataset()))
      choices <- "this should not be selectable!"
    else
      choices <- colnames(dataset())
    selectizeInput("choose", "Choose a Column", choices=choices)
  })
  observe({
    req(input$choose)
    toggleState("choose", !is.null(dataset()))
  })
}

shinyApp(ui=ui, server=server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM