在R中,基于NULL值的Shiny条件面板条件是什么?

10

我正在编写一个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'

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

2个回答

13

condition = "(typeof input.df_data_rows_selected !== 'undefined' && input.df_data_rows_selected.length > 0)" 对于这两个输入都可以工作。


这对我似乎不起作用。与之前一样,当按下“加载数据”按钮时,“Do Thing”面板会出现,但在选择行之前。 - TARehman
这个有点难啊… condition = 'input.df_data_rows_selected.length > 0' 很接近了,但还没到那里… - cory
@TARehman 我明白了...必须检查数组是否未定义并且长度大于0。我已经编辑了我的答案。 - cory
它有效!如果您想在答案中添加一点解释为什么它有效,那可能会帮助像我这样的JS新手。 - TARehman
我开始尝试给出一个解释,但那只是猜测罢了。我和你一样,可能都是JavaScript的初学者。我只是在谷歌上搜寻JavaScript中测试空数组的相关信息时偶然发现了这个方法。我希望我能够做得更好,但说实话这对我来说只是试错而已。 - cory
可以,如果你曾经来过麦迪逊的话,联系我,我会请你喝一杯啤酒或者其他什么的。 :) - TARehman

2
condition = "(typeof input.df_data_rows_selected === null")

如果有人在几年后遇到这个问题...


请问您能解释一下为什么要使用三个等号符号吗? - Cris
1
@Cris 三个等号("===")比较原始引用并跳过重载运算符("==")。 - david glen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接