基于 R 中的 NULL 值的闪亮条件面板条件?
Shiny conditionalPanel conditions based on NULL values in R?
我正在编写一个 Shiny 应用程序,它将查询数据库几次。查询可能需要一些时间,所以我使用 actionButton
来允许用户控制它们何时开始。
申请的大体流程是:
- 用户加载页面,按下按钮以加载数据库中的可用选项。
- 用户 select 从可用选项中选择一行,然后开始一个更大的查询,这将花费更长的时间。
- 查询完成后,用户将获得漂亮的可视化效果和其他类似内容。
我知道您可以允许用户使用 selection
选项从 DataTable 中获取 select 行,我目前正在使用开发版本,因此 single-selection可以启用。我的问题是弄清楚如何在用户做出选择之前隐藏某些条件面板。据我所知,选择的值存储在 input$[data frame name]_rows_selected
中。但是,在值被 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
加载数据。但是,我需要它们保持隐藏状态,除非用户已经 select 编辑了一行。我尝试过其他的东西,例如:
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 中表示?
condition = "(typeof input.df_data_rows_selected !== 'undefined' && input.df_data_rows_selected.length > 0)"
两个条目似乎都有效。
condition = "(typeof input.df_data_rows_selected === null")
如果几年后有人遇到这个问题...
我正在编写一个 Shiny 应用程序,它将查询数据库几次。查询可能需要一些时间,所以我使用 actionButton
来允许用户控制它们何时开始。
申请的大体流程是:
- 用户加载页面,按下按钮以加载数据库中的可用选项。
- 用户 select 从可用选项中选择一行,然后开始一个更大的查询,这将花费更长的时间。
- 查询完成后,用户将获得漂亮的可视化效果和其他类似内容。
我知道您可以允许用户使用 selection
选项从 DataTable 中获取 select 行,我目前正在使用开发版本,因此 single-selection可以启用。我的问题是弄清楚如何在用户做出选择之前隐藏某些条件面板。据我所知,选择的值存储在 input$[data frame name]_rows_selected
中。但是,在值被 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
加载数据。但是,我需要它们保持隐藏状态,除非用户已经 select 编辑了一行。我尝试过其他的东西,例如:
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 中表示?
condition = "(typeof input.df_data_rows_selected !== 'undefined' && input.df_data_rows_selected.length > 0)"
两个条目似乎都有效。
condition = "(typeof input.df_data_rows_selected === null")
如果几年后有人遇到这个问题...