1

Using a loop in R, I generated 100 random datasets and made a plot for each of these 100 datasets:

library(ggplot2)

results = list()

for (i in 1:100)

{

my_data_i = data.frame(var_1 = rnorm(100,10,10), var_2 = rnorm(100,10,10))

plot_i = ggplot(my_data_i, aes(x=var_1, y=var_2)) + geom_point() + ggtitle(paste0("graph", i))

results[[i]] = plot_i

}

list2env(setNames(results,paste0("plot",seq(results))),envir = .GlobalEnv)

What I am now trying to do, is make a Rmarkdown/flexdashboard that can be saved as an HTML file, and:

  • Contains all these plots
  • Allows the user to search for these plots (e.g. type in "plot76")

In a previous question (How to create a dropdown menu in flexdashboard?) I learned how to do something similar.

But I am still trying to figure out how I can get something like this to work. I would like there to be a single page with a search bar, and you can type in which graph you want to see.

Can someone please help me out with this? Are there any online tutorials that someone could recommend?

enter image description here

Thank you!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • My answer to a recent question of yours answered this, I think. I can see that this question was asked about the same time. If it didn't answer this question, let me know. – Kat Oct 22 '22 at 00:20

1 Answers1

2

This is a close solution depending on how much you need the type feature. First in the YAML specify toc and theme as below. This will create a table of contents and allow the users to click each anchor in the list and it will bring them to the appropriate figure. Second use knit_expand() and knit() to dynamically create html blocks in your code. I did 5 plots here but it should scale to 100.

---
title: "plots"
author: "Michael"
output: 
  html_document:
    toc: true
    theme: united
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(ggplot2)
library(glue)
library(knitr)

```



```{r,echo=FALSE, results = 'asis'}


my_data_i = data.frame(var_1 = rnorm(100,10,10), var_2 = rnorm(100,10,10))

out = NULL
out2 = NULL
plot_i = NULL
for (i in 1:5)
{
cat('\n## Plot = ', i, '\n')
plot_i = ggplot(my_data_i, aes(x=var_1, y=var_2)) + geom_point() + ggtitle(paste0("graph", i))

out2 = c(out2, knit_expand(text = "{{print(plot_i)}}" ))

cat('\n')

}


```

`r knit(text = out2)`
Mike
  • 3,797
  • 1
  • 11
  • 30
  • @ Mike: Thanks! I copied your code into the R markdown window and ran it ... it does not seem to be working? Am I doing something wrong? – stats_noob Oct 19 '22 at 21:23
  • This is what I got: https://imgur.com/a/EO0jmD0 – stats_noob Oct 19 '22 at 21:24
  • could you share a screen shot of the code you tried or put it into the question above? – Mike Oct 20 '22 at 14:03
  • it looks like you may have tried to create a dashboard and not an .rmd file I would open an rmarkdown file copy the code above and knit it to html – Mike Oct 20 '22 at 14:55