首页 > 解决方案 > 禁用文件输入

问题描述

我的闪亮应用程序中有一个selectizeInput和一个。fileInput

我想保持fileInput禁用状态,直到未在selectizeInput.

此外,我希望每次用户单击时都会弹出一条消息fileInput来选择一个值。我怎样才能做到这一点。

标签: rshiny

解决方案


正如其他人所提到的,该shinyjs软件包在这里很有用。您可以使用enable,disabletoggleState.

library(shiny)
library(shinyjs)

ui = fluidPage(
    shinyjs::useShinyjs(),
    selectizeInput("selector", label="Choose 2:", multiple=TRUE,
                   choices=letters[1:5], selected=letters[1:5]),
    fileInput("file_inputer", label="upload file")#,
    # dataTableOutput('table') 
)

server = function(input, output) {
    observe({
        shinyjs::toggleState("file_inputer", length(input$selector) %in% 0:4)
    })

    observeEvent(input$file_inputer, {
        showModal(modalDialog(
           title="Do you want a header row?",
           selectInput("option_selector", label="Choose an option",
                       choices=c("option 1", "option 2", "option 3")),
           footer = tagList(actionButton("read_file", "Read File"),
                            modalButton("Cancel")
           )
        ))
    })

    observeEvent(input$read_file, {
        # do something with your option value
        removeModal()
    })

}

# Run the application 
shinyApp(ui = ui, server = server)

推荐阅读