【问题标题】:disable/enable selectInput and fileInput upon the selection of Advanced checkboxInput在选择 Advanced checkboxInput 时禁用/启用 selectInput 和 fileInput
【发布时间】:2020-12-24 12:00:36
【问题描述】:

我有一个像这样的闪亮代码

library(datasets)
ui <-fluidPage(    
titlePanel("Telephones by region"),
sidebarLayout(      
sidebarPanel(
  selectInput("region", "Region:", 
              choices=colnames(WorldPhones)), checkboxInput(inputId = "Adv",
                                                            label = strong("Advanced"),
                                                            value = FALSE),fileInput("file1", "Choose CSV File",
            multiple = FALSE,accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
  hr(),
  helpText("Data from AT&T (1961) The World's Telephones.")),
mainPanel(
  plotOutput("phonePlot")   )))

server <- function(input, output) {

output$phonePlot <- renderPlot({

barplot(WorldPhones[,input$region]*1000, 
        main=input$region,
        ylab="Number of Telephones",
        xlab="Year")})}
shinyApp(ui, server)

我需要进行以下修改

  1. 如何在选择高级复选框输入时禁用/启用 selectInput 和 fileInput。如果用户选择高级,则必须禁用 selectInput(反之亦然)

  2. 如何使用 if 函数用于用户输入的文件输入(亚洲、非洲……每行一个)

【问题讨论】:

    标签: shiny


    【解决方案1】:

    要启用/禁用输入,您可以使用包shinyjs。 像这样的东西应该可以工作:

    library(datasets)
    library(shiny)
    ui <-fluidPage(   
        shinyjs::useShinyjs(),
        titlePanel("Telephones by region"),
        sidebarLayout(      
            sidebarPanel(
                selectInput("region", "Region:", 
                            choices=colnames(WorldPhones)),
                checkboxInput(inputId = "Adv",
                              label = strong("Advanced"),
                              value = FALSE),
                fileInput("file1", "Choose CSV File",
                          multiple = FALSE,accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
                hr(),
                helpText("Data from AT&T (1961) The World's Telephones.")),
            mainPanel(
                plotOutput("phonePlot")   )))
    
    server <- function(input, output) {
        
        observe({
            
            if((input$Adv == TRUE)) {
                shinyjs::disable("region")
                shinyjs::disable("file1")
            } else {
                shinyjs::enable("region")
                shinyjs::enable("file1")
            }
        })
        
        output$phonePlot <- renderPlot({
            
            barplot(WorldPhones[,input$region]*1000, 
                    main=input$region,
                    ylab="Number of Telephones",
                    xlab="Year")})}
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2017-09-09
      • 2020-06-15
      • 2021-12-14
      • 2023-01-30
      • 2012-07-22
      • 2012-12-07
      相关资源
      最近更新 更多