【问题标题】:Shiny conditionalPanel conditions based on NULL values in R?基于 R 中的 NULL 值的闪亮条件面板条件?
【发布时间】:2016-06-30 00:37:33
【问题描述】:

我正在编写一个 Shiny 应用程序,它将查询数据库几次。查询可能需要一些时间,所以我使用actionButton 允许用户控制它们何时启动。

申请的一般流程是:

  • 用户加载页面,按下按钮从数据库加载可用选项。
  • 用户从可用选项中选择一行,然后启动一个需要更长时间的更大查询。
  • 查询完成后,用户将获得漂亮的可视化和其他类似内容。

我知道您可以允许用户使用 selection 选项从 DataTable 中选择行,并且我目前正在使用开发版本,以便可以启用单选。我的问题是弄清楚如何隐藏某些条件面板,直到用户做出选择。据我所知,选择的值存储在input$[data frame name]_rows_selected 中。但是,在选择值之前,该值要么为 NULL,要么不存在。

我不知道如何将反映内部 R 逻辑的条件传递给 conditionalPanel()condition 参数。我在下面编写了一个最小的工作示例,它显示了我所指的行为。

library(shiny)
library(DT)

# Create sample data
df_sample_data <- data.frame(name = c("John Smith","Jane Cochran","Belle Ralston","Quincy Darcelio"),
                             color = c("Red","Blue","Red","Green"),
                             age = c(25,52,31,29))

ui <-
    fluidPage(

        titlePanel("The Title!"),

        sidebarPanel(

            h1("Load Data"),
            actionButton("load_data","Load Data"),
            br(),
            conditionalPanel(

                h1("Do Thing"),
                actionButton("do_thing","Do Thing"),
                condition = "input.df_data_rows_selected !== undefined")
            ),

        mainPanel(

            dataTableOutput("df_data"),
            conditionalPanel(

                tableOutput("row_selected"),
                condition = "input.df_data_rows_selected !== undefined")
            )
    )

server <-
    function(input, output) {

        # This function loads the data (in reality it is a server operation)
        uFunc_ReactiveDataLoad <- eventReactive(eventExpr = input$load_data,valueExpr = {

            df_data <- df_sample_data
            return(list(display_table = datatable(data = df_data[c("name","age")],
                                                  options = list(dom = "tip"),
                                                  filter = "top",
                                                  selection = "single",
                                                  colnames = c("Person Name" = "name",
                                                               "Person Age" = "age")),
                        data_table = df_data))
        })
        output$df_data <- renderDataTable(expr = uFunc_ReactiveDataLoad()$display_table)
        output$row_selected <- renderTable(expr = uFunc_ReactiveDataLoad()$data_table[input$df_data_rows_selected,])
    }

shinyApp(ui = ui, server = server)

在使用input.df_data_rows_selected !== undefined 的当前设置中,面板在使用第一个actionButton 加载数据之前是隐藏的。但是,除非用户选择了一行,否则我需要它们保持隐藏状态。我尝试过其他方法,例如:

  • input.df_data_rows_selected !== null
  • input.df_data_rows_selected !== 'null'
  • input.df_data_rows_selected !== ''
  • input.df_data_rows_selected !== 'NULL'

...等等,但我没有运气。 R 中的 NULL 值如何在用于 conditionalPanel()condition 参数的 JavaScript 中表示?

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    condition = "(typeof input.df_data_rows_selected !== 'undefined' &amp;&amp; input.df_data_rows_selected.length &gt; 0)" 这两个条目似乎都有效。

    【讨论】:

    • 这似乎对我不起作用。和以前一样,一旦按下“加载数据”按钮,但在选择一行之前,就会出现“执行操​​作”面板。
    • 伙计,这是一个艰难的... condition = 'input.df_data_rows_selected.length &gt; 0') 真的很接近,但不是很接近...
    • @TARehman 我明白了...必须检查数组是否未定义且长度 > 0。我已经编辑了答案
    • 有效!如果您想在答案中添加一点解释它为什么会起作用,这可能会帮助像我这样的其他 JS 新手。
    • 我开始尝试做出解释,但这只是猜测。我可能和你一样是 javascript 新手。我只是在谷歌上搜索有关在 javascript 中测试空数组的信息,这是我发现的一件事。我希望我能做得更好,但老实说,这对我来说只是反复试验。
    【解决方案2】:
    condition = "(typeof input.df_data_rows_selected === null")
    

    如果有人在几年后偶然发现这个问题......

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2016-01-20
      • 2021-04-06
      • 2023-03-03
      相关资源
      最近更新 更多