我正在尝试在选项卡中动态渲染多个图表(如果可以跨多个选项卡进行,则更好)。经过一些搜索,我发现这篇文章非常有帮助。但在我的情况下,图表的数量由上传的CSV文件确定。所以我认为问题是如何在for循环中调用
此时,我能够通过调用
plotInput()$n_plot
?感谢任何建议!此时,我能够通过调用
renderUI
来创建多个<div>
。<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.
Server.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])
})
})
}
})