在 renderPlot 之外的 Shiny 中为 renderTable 提供一个对象
Making an object available in Shiny outside renderPlot for renderTable
我正在起草一个简单的闪亮应用程序,它提供对动态图表和相应 table 的访问。 server.R
代码的相关部分如下所示:
output$some_plot<- renderPlot({
# Subset data on change in the indicator selection
chrt_demo_dta <- subset(x = dta_la_demo,
subset = <<my-conditions>>>)
# Define the demography chart
ggplot(data = chrt_demo_dta, aes(x = variable_a, y = variable_b)) +
geom_line(aes(colour = GEOGRAPHY_NAME), size = 2) +
theme_bw()}, height = 650, width = 800)
# Section generating table
output$chrt_demo_dta_tbl <- renderTable({chrt_demo_dta})
当我尝试访问 table 时出现问题,我收到以下错误消息:
Error in func() : object 'chrt_demo_dta' not found
对象 chrt_demo_dta
似乎是在 renderTable
的范围规则之外创建的。我的问题是如何实现以下目标:
- 我希望图表和相应的 table 在选择时动态更新,因此我的想法是将
subset
命令嵌入 renderPlot
中,该命令有效
- 我想在相应的 table 中使用相同的子集。理想情况下,我想避免重复 subset 命令。由于我已准备好所需的数据框,看来只需通过
renderTable
访问它即可
我知道代码不能完全重现,但在这个阶段我不一定要寻找特定的解决方案,而是寻找是否可以访问创建的对象的更通用的指导在来自其他服务器元素的一个服务器元素中。 如果到了紧要关头,我可以将子集机制封装在一个函数中并调用它两次,但这似乎是一个相当混乱的解决方案。
<<- 运算符可能会产生不良后果。它将结果发送到共享环境,会话的所有用户都可以看到结果。这不仅会造成 race-condition 他们试图 over-write 彼此,还可能将机密工作暴露给其他人。您有两个解决方案:1) 在每个本地环境中重复这些步骤,2) 使用唯一名称(例如 Sys.time() + 数据哈希)将结果写入磁盘。然后您可以在其他地方需要时检索它。不过不要忘记删除保存的文件,否则会占用您的存储空间。
在server.R
的server
函数中:
# Create reactive object w/in server func
chrt_demo_dta <- reactiveVal()
output$some_plot<- renderPlot({
chrt_demo_dta.temp <- subset(
x = dta_la_demo,
subset = <<my-conditions>>>)
# Update the obj within scope of nested function
chrt_demo_dta(chrt_demo_dta.temp)
})
# Access of the updated obj is now possible in original scope
output$chrt_demo_dta_tbl <- renderTable({chrt_demo_dta()})
查看相关内容:
我正在起草一个简单的闪亮应用程序,它提供对动态图表和相应 table 的访问。 server.R
代码的相关部分如下所示:
output$some_plot<- renderPlot({
# Subset data on change in the indicator selection
chrt_demo_dta <- subset(x = dta_la_demo,
subset = <<my-conditions>>>)
# Define the demography chart
ggplot(data = chrt_demo_dta, aes(x = variable_a, y = variable_b)) +
geom_line(aes(colour = GEOGRAPHY_NAME), size = 2) +
theme_bw()}, height = 650, width = 800)
# Section generating table
output$chrt_demo_dta_tbl <- renderTable({chrt_demo_dta})
当我尝试访问 table 时出现问题,我收到以下错误消息:
Error in func() : object 'chrt_demo_dta' not found
对象 chrt_demo_dta
似乎是在 renderTable
的范围规则之外创建的。我的问题是如何实现以下目标:
- 我希望图表和相应的 table 在选择时动态更新,因此我的想法是将
subset
命令嵌入renderPlot
中,该命令有效 - 我想在相应的 table 中使用相同的子集。理想情况下,我想避免重复 subset 命令。由于我已准备好所需的数据框,看来只需通过
renderTable
访问它即可
我知道代码不能完全重现,但在这个阶段我不一定要寻找特定的解决方案,而是寻找是否可以访问创建的对象的更通用的指导在来自其他服务器元素的一个服务器元素中。 如果到了紧要关头,我可以将子集机制封装在一个函数中并调用它两次,但这似乎是一个相当混乱的解决方案。
<<- 运算符可能会产生不良后果。它将结果发送到共享环境,会话的所有用户都可以看到结果。这不仅会造成 race-condition 他们试图 over-write 彼此,还可能将机密工作暴露给其他人。您有两个解决方案:1) 在每个本地环境中重复这些步骤,2) 使用唯一名称(例如 Sys.time() + 数据哈希)将结果写入磁盘。然后您可以在其他地方需要时检索它。不过不要忘记删除保存的文件,否则会占用您的存储空间。
在server.R
的server
函数中:
# Create reactive object w/in server func
chrt_demo_dta <- reactiveVal()
output$some_plot<- renderPlot({
chrt_demo_dta.temp <- subset(
x = dta_la_demo,
subset = <<my-conditions>>>)
# Update the obj within scope of nested function
chrt_demo_dta(chrt_demo_dta.temp)
})
# Access of the updated obj is now possible in original scope
output$chrt_demo_dta_tbl <- renderTable({chrt_demo_dta()})
查看相关内容: