【发布时间】:2015-11-06 20:06:42
【问题描述】:
我正在尝试开发一个闪亮的应用程序,用户可以在其中上传 csv 文件,这些文件随后由我的 R 脚本进行分析。因此,我想根据处理的文件数(每个文件一个图)显示动态的图数。
我发现this 问题非常有帮助,但我不知道提前的最大绘图数。这是我尝试过的:
shinyServer(function(input, output) {
# Insert the right number of plot output objects into the web page
output$densityPlots <- renderUI({
plot_output_list <- lapply(1:length(input$file1$datapath), function(i) {
plotname <- paste("densityPlot", i, sep="")
plotOutput(plotname)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:length(input$file1$datapath)) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
plotname <- paste("densityPlot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1)
})
})
}
}
但是,它给了我这个错误:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
我尝试将 for 循环放在输出函数中,但根本没有创建任何绘图。
【问题讨论】: