【发布时间】:2015-01-11 21:56:30
【问题描述】:
我正在尝试在一个选项卡中动态呈现多个图(如果可以跨多个选项卡完成则更好)。经过一番搜索,我发现这个post 很有帮助。但就我而言,地块的数量由上传的 CSV 文件决定。所以我认为问题是如何在 for 循环中调用plotInput()$n_plot?我很感激任何建议!
此时,我可以通过调用renderUI 来创建多个<div>s。
<div id="plot1" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
<div id="plot2" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
但我无法正确调用 for (i in 1:plotInput()$n_plot) 循环中的反应函数。错误信息是:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context.
服务器.R
shinyServer(function(input, output) {
### This is the function to break the whole data into different blocks for each page
plotInput <- reactive({
get_the_data()
return (list("n_plot"=n_plot, "total_data"=total_data))
})
##### Create divs######
output$plots <- renderUI({
plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
do.call(tagList, plot_output_list)
})
# Call renderPlot for each one.
####This is the place caused the error##
for (i in 1:plotInput()$n_plot) {
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
hist(plotInput()$total_data[i])
})
})
}
})
更新
如果我将 for 循环包装在 reactive 函数中并将其作为 renderUI 的一部分调用,则循环有效,但情节缺失,我猜这是因为 renderUI 仅创建 HTML 标签,它不会将图像分配给生成的标签。
output$plots <- renderUI({
plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
do.call(tagList, plot_output_list)
plot_concent()
})
# Call renderPlot for each one.
####This is the place caused the error##
plot_concent<-reactive({
for (i in 1:plotInput()$n_plot) {
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
hist(plotInput()$total_data[i])
})
})
}
})
【问题讨论】:
-
在添加反应式时,没有数字。没有反应,它不起作用。这就是问题所在。
-
我也遇到了同样的问题,请问您找到解决方案了吗?
-
@Benedikt:这有帮助吗? groups.google.com/forum/…
标签: r shiny shiny-server