Creating List of Objects

Hi Good Morning
I have a shiny app that creates dynamically ggplots (scatterplots) and assign it to dynamically created names eg plot1, plot2, plot3 and so on
when i bring it to dashboard, i use grid.arrange. my concern is some times 10 plots and some times 8. but in grid.arrange function i have to specify the list of plots. I tried to create the list for the grid.arrange function. but I dont get it as given below. it all comes with quotes that the function does not accept.

grid.arrange(grobs =list(plot1,plot2,plot3,plot4,plot5,plot6,plot7,plot8),nrow=r, ncol=c)

i would like to create dynamically a list of object names that i have created for plots and mention the list name inside grid.arrange function. could someone help me out please
Thank you very much

The best thing to do in your case is simply to actually make a list of plots. Dynamically generating variable names is almost always a bad idea.

For example, something like:

my_plots <- list()

my_plots[[1]] <- ggplot(mtcars) +
  geom_point(aes(x = mpg, y = gear))
my_plots[[2]] <- ggplot(mtcars) +
  geom_point(aes(x = mpg, y = wt))

gridExtra::grid.arrange(grobs = my_plots)

That being said, if you really want to transform strings into code, you can use str2expression() and then eval(). I repeat here it's probably better not to do that if you can instead use a list which is meant for that exact purpose.

string1 <- "aa"
string2 <- "bb"


eval(str2expression("paste0(string1, string2)"))
#> [1] "aabb"
1 Like

Excellent - Thank you so much AlexisW. my problem is solved with your " list" approach

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.