【问题标题】:R - Disabled action button conditional to column's nameR - 以列名称为条件的禁用操作按钮
【发布时间】:2017-01-26 23:36:05
【问题描述】:

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

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

这可以用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)

【问题讨论】:

  • 仅供参考:您的代码在复制时对我抛出错误:Error in tag("form", list(...)) : argument is missing, with no default
  • @Tonio Liebrand。我刚刚打开了一个新的 R 会话并毫无问题地运行了代码。
  • 哦,对不起,清除环境后它起作用了。

标签: r shiny shinyjs


【解决方案1】:

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

shinyjs::disable("Submit")

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

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

当然启用也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多