根据ggplots列表中的图表数量创建一些renderPlot函数?(Create a number of renderPlot functions, based on the number of p

编程入门 行业动态 更新时间:2024-10-13 22:20:38
根据ggplots列表中的图表数量创建一些renderPlot函数?(Create a number of renderPlot functions, based on the number of plots I have in a list of ggplots?)

有没有办法根据renderPlot列表中的图表数量动态创建一些renderPlot函数?

我有一个Shiny应用程序,而不是使用稳定的UI,而不是使用renderUI,我依靠用户提供的配置文件来告诉Shiny要显示多少个图。 配置文件还提供数据,几乎可以帮助完成大部分繁重的工作。

经过多次战斗,我大部分都在那里。 使用方便的配置文件,我可以构建正确的UI,并生成正确数量的ggplots。 ggplots存在于列表中,创造性地命名为list_of_ggplots 。

但现在,我正处于一个ggplots列表的位置,我需要允许它们像这样使用它们来绘制:

output$plot1 <- renderPlot({ print(list_of_ggplots[[1]]) })

但现在我有一个存在主义危机 - 我不能这样做,因为用户提供的配置文件告诉我我有多少个情节。 我不能像通常在Shiny中那样硬编码renderPlot调用,因为所需的这些函数的数量在配置文件中定义。

鉴于我的ggplots列表,我需要一些方法来生成renderPlot调用。

有没有人这样做或有任何想法? 非常感激。

这是我的代码:

SERVER.R:

library(shiny) library(ggplot2) # 3 simple plots of different colors -- used here instead of all the complicated stuff # where someone uses the config file that specified 3 plots, with data, etc. ggplot_names <- c("p1", "p2", "p3") ggplot_colors <- c("red", "blue", "green") list_of_ggplots <- list() j = 1 for (i in ggplot_names){ i <- ggplot(data.frame(x = c(-3, 3))) i <- i + aes(x) i <- i + stat_function(fun = dnorm, colour=ggplot_colors[[j]]) list_of_ggplots[[j]] <- i j <- j+ 1 } ## here's the problem -- the user specified 3 plots. ## I can't hardcode the following shinyServer functions!!! ## What if tomorrow, the user specifies 2 plots instead? shinyServer(function(input, output) { output$plot1 <- renderPlot({ print(list_of_ggplots[[1]]) }) output$plot2 <- renderPlot({ print(list_of_ggplots[[2]]) }) output$plot3 <- renderPlot({ print(list_of_ggplots[[3]]) }) })

UI.R

## this top part is actually sourced from the config file ## since Shiny needs to know how many tabPages to use, ## names for the tabs, etc number_of_tabPages <- 3 tab_names <- c("", "Tab1", "Tab2", "Tab3") tabs<-list() tabs[[1]]="" for (i in 2:(number_of_tabPages+1)){ tabs[[i]]=tabPanel(tab_names[i],plotOutput(paste0("plot",i-1)))} ## Here's the familiar UI part shinyUI(fluidRow( column(12, "", do.call(navbarPage,tabs) ) ) )

Is there any way I can dynamically create a number of renderPlot functions, based on the number of plots I have in a list of ggplots?

I have a Shiny app where instead of having a stable UI, and instead of using renderUI, I am relying on a user-supplied config file to tell Shiny how many plots to show. The config file also supplies data and pretty much helps do most of the heavy lifting.

After much battling, I'm mostly there. With the handy-dandy config file, I can build the correct UI, and generate the correct number of ggplots. The ggplots live in a list, creatively named list_of_ggplots.

But now, I'm at a point where I have a list of ggplots, and I need to allow them to be plotted by using them like this:

output$plot1 <- renderPlot({ print(list_of_ggplots[[1]]) })

But now I have an existentialist crisis -- I can't do it like this, since the user-supplied config file tells me how many plots I have. I can no longer hard code the renderPlot call like is usually done in Shiny, since the number of these functions needed is defined in the config file.

Given my list of ggplots, I need some way to generate the renderPlot calls.

Has anyone done this or have any ideas? Much appreciated.

Here's my code:

SERVER.R:

library(shiny) library(ggplot2) # 3 simple plots of different colors -- used here instead of all the complicated stuff # where someone uses the config file that specified 3 plots, with data, etc. ggplot_names <- c("p1", "p2", "p3") ggplot_colors <- c("red", "blue", "green") list_of_ggplots <- list() j = 1 for (i in ggplot_names){ i <- ggplot(data.frame(x = c(-3, 3))) i <- i + aes(x) i <- i + stat_function(fun = dnorm, colour=ggplot_colors[[j]]) list_of_ggplots[[j]] <- i j <- j+ 1 } ## here's the problem -- the user specified 3 plots. ## I can't hardcode the following shinyServer functions!!! ## What if tomorrow, the user specifies 2 plots instead? shinyServer(function(input, output) { output$plot1 <- renderPlot({ print(list_of_ggplots[[1]]) }) output$plot2 <- renderPlot({ print(list_of_ggplots[[2]]) }) output$plot3 <- renderPlot({ print(list_of_ggplots[[3]]) }) })

UI.R

## this top part is actually sourced from the config file ## since Shiny needs to know how many tabPages to use, ## names for the tabs, etc number_of_tabPages <- 3 tab_names <- c("", "Tab1", "Tab2", "Tab3") tabs<-list() tabs[[1]]="" for (i in 2:(number_of_tabPages+1)){ tabs[[i]]=tabPanel(tab_names[i],plotOutput(paste0("plot",i-1)))} ## Here's the familiar UI part shinyUI(fluidRow( column(12, "", do.call(navbarPage,tabs) ) ) )

最满意答案

你可以使用这个解决方案(我只修改了脚本的shinyServer部分,所以我不在这里列出重复的代码):

shinyServer(function(input, output) { observe( lapply(seq(3),function(i) output[[paste0("plot",i)]] <- renderPlot(list_of_ggplots[[i]])) ) })

当然,您可以用变量替换3 。

You can use this solution (I modified only the shinyServer part of your scripts, so I don't list the repeating code here):

shinyServer(function(input, output) { observe( lapply(seq(3),function(i) output[[paste0("plot",i)]] <- renderPlot(list_of_ggplots[[i]])) ) })

Of course, you can replace 3 by a variable.

更多推荐

本文发布于:2023-07-17 13:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图表   函数   数量   列表中   renderPlot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!