我正在编写一个Shiny应用程序,它将查询数据库几次.查询可能需要一些时间,因此我使用它actionButton来允许用户控制它们何时启动.
该应用程序的一般流程是:
我知道您可以允许用户使用该selection选项从DataTable中选择行,并且我当前正在使用开发构建,以便可以启用单选.我的问题是弄清楚如何隐藏某些条件面板,直到用户做出选择.据我所知,选择的价值存储在input$[data frame name]_rows_selected.但是,在选择值之前,此值为NULL或不存在.
我无法弄清楚如何传递反映内部R逻辑的条件的condition参数conditionalPanel().我在下面写了一个最小的工作示例,它显示了我所指的行为.
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)
Run Code Online (Sandbox Code Playgroud)
在当前使用的设置中,input.df_data_rows_selected !== undefined面板被隐藏,直到使用第一个加载数据actionButton.但是,除非用户选择了一行,否则我需要它们保持隐藏状态.我尝试过其他的东西,比如:
input.df_data_rows_selected !== nullinput.df_data_rows_selected !== 'null'input.df_data_rows_selected !== ''input.df_data_rows_selected !== 'NULL'......等等,但我没有运气.如何在用于condition参数的JavaScript中表示R中的NULL值conditionalPanel()?
condition = "(typeof input.df_data_rows_selected !== 'undefined' && input.df_data_rows_selected.length > 0)" 这两个条目似乎都有效.