我有一个Shiny仪表板应用程序,用户在其中将其名称输入到文本框中。
仪表板从连接的数据库中检索数据后读取用户数据集,并将其分配给包含其用户名的变量
示例
assign(paste0(input$user_name, "_dataset"),
sqlQuery(connectionString, str_paste("Select * from ",user_name,"_table;"))
)
问题:在不了解input$user_name
的情况下,如何根据数据准备图表和图表?
我尝试过这个,
plot_ly(paste0(input$user_name, "_dataset"), x = ..., y = ... etc)
但是它不起作用。
如上所述,我们需要一个完整的可复制示例,但一般而言,您应该使用reactive expression。
示例:
data <- reactive({
req(input$user_name) # loads only if an input username is available
sqlQuery(connectionString, str_paste("Select * from ",user_name,"_table;"))
})
然后通过[]在您的renderPlot
中调用此表达式:
plot_ly(data(), x = ..., y = ... etc) # note the parentheses after `data`