我正在编写一个Shiny应用程序,它将对数据库进行几次查询。查询可能需要一些时间,因此我使用actionButton
来允许用户控制它们何时启动。
该应用程序的一般流程是:
我知道您可以允许用户使用selection
选项从DataTable中选择行,我目前正在使用开发版本,以便可以启用单选。我的问题是弄清楚如何隐藏某些条件面板,直到用户做出选择。据我所知,选择的值存储在input$[data frame name]_rows_selected
中。但是,在选择值之前,此值为NULL或不存在。
我无法弄清楚如何将condition
conditionalPanel()
参数传递给反映内部R逻辑的条件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'
condition
......等等,但我没有运气。 R中的NULL值如何在用于conditionalPanel()
的{{1}}参数的JavaScript中表示?
答案 0 :(得分:5)
condition = "(typeof input.df_data_rows_selected !== 'undefined' && input.df_data_rows_selected.length > 0)"
似乎都有效。