R循环以显示多个绘图
好的,我有你可以在下面运行的代码。我正在尝试修改此代码: 因此,可以使用来自UI的输入动态设置max_图。来自UI的输入也是动态的 这是我的UI.R:R循环以显示多个绘图,r,shiny,R,Shiny,好的,我有你可以在下面运行的代码。我正在尝试修改此代码: 因此,可以使用来自UI的输入动态设置max_图。来自UI的输入也是动态的 这是我的UI.R: shinyUI(pageWithSidebar( headerPanel("Dynamic number of plots"), sidebarPanel( selectInput("Desk", "Desk:" , c("A","B")), uiOutput("choose_Product"), uiOutput("c
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
selectInput("Desk", "Desk:" , c("A","B")),
uiOutput("choose_Product"),
uiOutput("choose_File1"),
uiOutput("choose_Term1"),
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),
mainPanel(
# This is the dynamic UI for the plots
h3("Heading 1"),
uiOutput("plots"),
h3("Heading 2")
)
))
这是我的服务器。R:
shinyServer(function(input, output) {
output$choose_Product <- renderUI({
selectInput("Product", "Product:", as.list( c("Product1","Product2","Product3")))
})
output$choose_File1 <- renderUI({
selectInput("File1", "File1:", as.list(c("File1","File2","File3")))
})
# Insert the right number of plot output objects into the web page
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})##End outputplots
PrintPlots<- reactive({
## I need max_plots to be set as the length of the Vector returns from getAugmentedTerms()
max_plots<-length(getAugmentedTerms(input$Desk, input$Product, input$File1))
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max_plots) {
# 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("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n is ", input$n, sep = "") )
})
})
}##### End FoR Loop
})# END OF PRINT PLOTS
})# END OF ENERYTHING
运行此命令时,可以看到创建了空间,就像有打印输出一样,但输出为空。有什么想法吗?谢谢大家! 因此
max_图
是硬编码的。启动应用程序时,(i in 1:max_plots){(仅运行一次)的循环将创建5(=max_plots)个新的输出
元素(输出$plot1输出$plot2…)用于渲染绘图,但仅当UI中存在相应的
plotOutputs时才会显示绘图。在函数
lapply(1:input$n,函数(i){input$n是滑块的值)中动态创建
plotOutputs。
创建了5个绘图,但我们只创建
input$nplot outputs。这就是为什么即使
max_plots`是硬编码的,这也是动态的
如果需要,可以用动态参数替换max_plots
,但需要将循环包含到和观察者中。每次input$n
更改时,它都会运行
例如:
observe({
for (i in 1:input$n) {
# 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("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n is ", input$n, sep = "")
)
})
})
}
})
input
对象在Shining server函数外未定义。在示例中,max_plots
是硬编码的,创建绘图输出的循环的input$n
不是硬编码的t@user3022875把你的评论作为一个单独的答案发表,或者编辑你的问题,不要编辑别人的答案ces我所经历的。max_绘图是从动态UI的输入设置的。您可以运行代码并查看发生了什么。谢谢!Julien--我现在的问题是,在您编写的最后一段代码中({max_plot我测试了你的代码,它不适用于被动
和观察
。我不明白你想做什么?问题是“观察”在填充输入$Product和输入$File1之前发生。请参见此处:……由于观察首先发生,是否有方法将for循环移动到反应函数中,而不是使用观察?
library(shiny)
runApp("C:/Users/user/Desktop/R Projects/TestDynamicPlot")
observe({
for (i in 1:input$n) {
# 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("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n is ", input$n, sep = "")
)
})
})
}
})
observe({
max_plots<-length(getTerm1UI(input$Desk, input$Product, input$File1))
for(i in 1:max_plots) {
...