【问题标题】:Rshiny conditional button/divs/table if button is pressed or rows selected from DT如果按下按钮或从 DT 中选择行,则 R 闪亮的条件按钮/div/表格
【发布时间】:2020-10-22 05:49:20
【问题描述】:

我可以在 UI 中为输入字段使用条件而不涉及服务器端。但是当我尝试为 input_rows_selected 或按钮状态之类的事情做这件事时;它们不起作用。

下面我有几个设置了条件的输入字段。第二个字段取决于第一个被选中的内容。然后第一个按钮取决于第二个字段中被选中的内容。

这就是问题所在。如果在数据表中选择了行,则应该只显示第三个按钮。如果选择了一行,我已经让它工作了,但不超过一行。然后第三个按钮下方的 html 和表格应该只在按下第三个按钮后显示。目前显示的是html,table不工作。

想法?我希望在 UI 方面尽可能地保留它。这样,我可以轻松地将按钮隐藏/显示功能调整到各种其他代码块。但我不确定这是否可能。

编辑:更新为当前代码 编辑 2:更新为最终工作代码

  if (interactive()) {
  library(shiny)
  
  items <- data.frame(
    category = c("Room", "IceBreaker", "Activity", "Break"),
    group = c(1, 2, 3, 4),
    className   = c ("red_point", "blue_point", "green_point", "purple_point"),
    content = c("Big Room", "Introductions", "Red Rover", "Lunch"),
    length = c(480, 60, 120, 90)
  )
  
  
  ui <- fluidPage(shinyUI(
    tagList(
      useShinyalert(),
      useShinyjs(),
      title = "Conditional Inputs",
      tabsetPanel(id = "mainTabset",
                  tabPanel(
                    title = "Wheeeeeee!",
                    class = "inputs",
                    column(
                      12,
                      
                      selectInput(
                        inputId = "input1",
                        label = "A, B, or C?",
                        choices = c(
                          Choose = '',
                          A = 'a',
                          B = 'b',
                          C = 'c'
                        ),
                        selectize = FALSE
                      ),
                      
                      conditionalPanel(
                        condition = "input.input1 !== ''",
                        selectInput(
                          inputId = "input2",
                          label = "D, or E?",
                          choices = c(Choose = '',
                                      D = 'd',
                                      E = 'e'),
                          selectize = FALSE
                          
                        )
                      ),
                      conditionalPanel(
                        condition = "input.input2 !== ''",
                        
                        actionButton(
                          "button1",
                          "SUBMIT",
                          style = "background-color:#221B70;
                          color:#E0EB15;
                          border-color:#E61029;
                          border-style:double;
                          border-width:4px;
                          border-radius:50%;
                          font-size:19px;"
                        ),
                        DT::dataTableOutput("tbl1"),
                        
                        
                        conditionalPanel(
                          condition = "typeof input.tbl1_rows_selected  !== 'undefined' && input.tbl1_rows_selected.length > 0",
                          actionButton(
                            "button2",
                            "GENERATE TABLE",
                            style = "background-color:#221B70;
                            color:#E0EB15;
                            border-color:#E61029;
                            border-style:double;
                            border-width:4px;
                            border-radius:50%;
                            font-size:19px;"
                          )
                          ),
                        
                        conditionalPanel(condition = "input.button2 > 0",
                                         div(
                                           "Selected items:", DT::dataTableOutput("tbl2")
                                         ))
                          )
                        )
                      ))
      )
    ))
  
  server <- function(input, output) {
    observeEvent(input$button1, {
      output$tbl1 <- DT::renderDataTable({
        items
      }, selection = 'multiple',
      class = "display nowrap compact",
      extensions = 'Scroller',
      options = list(dom = 'Bfrtip'))
      
    })
    observeEvent(input$button2, {
      table <- items[input$tbl1_rows_selected,c(2,3,4)]
      output$tbl2 <- DT::renderDataTable({
       table
      })
      
    })
  }
  
  
  shinyApp(ui, server)
}

【问题讨论】:

    标签: r shiny shiny-server shiny-reactivity


    【解决方案1】:

    input$tbl1_rows_selected 如果只选择一行,则为单个整数,如果选择多行,则为向量,如果未选择行,则为 NULL。所以合适的条件是

    "input.tbl1_rows_selected !== null"
    

    input$button2 始终是整数。它被初始化为0,然后在每次按下按钮时递增。然后检查按钮是否被按下的条件是

    "input.button2 > 0"
    

    我不明白您所说的“第三个按钮下方的表格”是什么意思。我在此按钮下方没有看到任何表格。

    编辑

    这不起作用:

    "input.tbl1_rows_selected !== null"
    

    有效:

    "input.tbl1_rows_selected.length > 0"
    

    input$tbl1_rows_selected在R中是NULL时,input.tbl1_rows_selected似乎是一个空的JavaScript数组。否则它是一个非空数组

    【讨论】:

    • 我刚刚在嗅探 NULL 和 0 条件,但我让它们打开了按钮!感谢那。在按下“生成表”按钮之前,不应显示“div(”Selected items:”,textOutput(“selected”,inline = TRUE)“块。我想我必须移动所有这些到服务器端以阻止 div 一直显示?
    • @Steve 对不起,我不明白你的意思。如果您执行"input.button2 &gt; 0",则在按下按钮之前不应显示div
    • 你是对的,(我错过了一个括号)它确实解决了这个问题。但由于某种原因,在 rows_selected 中使用 null 不起作用。我错过了一些东西。我试过 NULL、null、'NULL'、'null'、“NULL”、“null”...等
    • @Steve 你说得对,那是行不通的。我找到了解决方案,请参阅我的编辑。
    • 嗯,我一定还是错过了什么。我已将我的代码更新为我当前的工作版本。 (下面的表格现在按预期工作。)但是提交按钮和生成表格按钮仍然同时显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2019-02-10
    • 2019-05-05
    • 2021-05-01
    • 1970-01-01
    • 2018-08-01
    • 2020-06-20
    相关资源
    最近更新 更多