gpt4 book ai didi

r - Shiny R - 使用循环打印多个图

转载 作者:行者123 更新时间:2023-12-04 12:02:45 28 4
gpt4 key购买 nike

我的 global.R 文件中有一个创建绘图的函数:

CreatePlot<-function(mergedFiles,File1, File2) 
{
main<- mergedFiles
csv1<- main[,1:4]
csv2<- cbind( main[,1],main[,5:7] )
strikes<- factor(main$Strike,levels=c(main$Strike),ordered=TRUE)
stacked<- stacked <- data.frame(time=strikes, value =c(c(csv1$Vol), c(csv2$Vol)) , variable = rep(c(File1,File2), each=NROW(csv1[,1])))

g<- ggplot(stacked, aes( x = time, y=value, colour=variable, group= variable) ) + geom_line() + xlab("Strike") +geom_point(shape = 7,size = 1) +
ylab("Volatiltiy") +ggtitle("Plot of Volatilities") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
return(g)
}

我想创建一个函数来循环打印多个图。假设我想要 3 个地 block ,我正在我的服务器中做这样的事情。R:

  output$MyList <- renderUI({ 
main<- mergedFiles() # this is the data for my plots
g<-CreatePlot(main, input$File1, input$File2) #this calls the create plot function
for (i in 1:3){
renderPlot({
print(g) #for testing purposes I am just trying to print the same plot 3 times
})
}

})

还有我在主面板中的 UI.R:

uiOutput("MyList")

当我运行它时,什么也没有发生。屏幕是空白的,没有任何错误。任何想法如何使用循环打印这样的多个图?

谢谢你!

最佳答案

您不能使用 renderUI 渲染绘图。 renderUI 仅评估“返回 Shiny 标记对象、HTML 或此类对象列表的表达式。”

您必须为每个要显示的绘图创建一个plotOutput。因此,您可以使用 renderUI 动态创建多个 plotOuputs,然后使用循环创建图形。

看看这个例子:https://gist.github.com/wch/5436415/

关于r - Shiny R - 使用循环打印多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823843/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com