0

下面是我的应用程序的简化版本shiny。我查看了shinyjs包中的一些示例,但没有找到任何可以帮助我的东西。

我想禁用Submit button如果上传的数据框之一(在我的真实示例中)或选定的数据框具有特定的列名(下例中的 Col 3)。

这可以做到shinyjs吗?

library(rhandsontable)
library(shiny)
library(shinyjs)

df1 <- data.frame(col1 = rnorm(20),
                  col2 = rep(T, 20))

df2 <- data.frame(col1 = rnorm(20),
                  col2 = rep(F, 20),
                  col3 = rnorm(20))


server <- function(input, output) {

  values = reactiveValues()
  values[["df1"]] <- df1
  values[["df2"]] <- df2


  df <- reactive({
    if (input$df == "df1") {
      df <- values[["df1"]]
    } else {
      df <- values[["df2"]]
    }
    df
  })

  observeEvent(input$Submit, {
  shinyjs::alert("Thank you!")
})


#observe({
 # if (is.null(input$df) || input$df == "df1") {
  #  shinyjs::disable("submit")
  #} else {
   # shinyjs::enable("submit")
  #}
#})


  output$out <- renderRHandsontable({
    hot <- rhandsontable(df())
    hot
  })
}


ui <- fluidPage(
shinyjs::useShinyjs(),

sidebarLayout(sidebarPanel(
  selectInput(
    'df', 'Select data.frame:',
    choices = c('df1', 'df2'),
    selected = 'df1'
  ),
  actionButton("Submit", label = "Submit")
),
mainPanel(rHandsontableOutput("out"))))

shinyApp(ui = ui, server = server)
4

1 回答 1

1

首先,有一个小错字:注意大写的“S”。

shinyjs::disable("Submit")

编辑:要检查“col3”,请使用以下代码:

  observe({
    if (is.null(input$df) || sum(colnames(df()) == "col3")) {
      shinyjs::disable("Submit")
    }else{
      shinyjs::enable("Submit")
    }
  })

当然启用也是如此。

于 2017-01-26T13:09:08.113 回答