Parsa
Parsa

Reputation: 3236

Rmarkdown - HTML file select pre-generated graphs

I have 10 locations, and for each location I have generated 4 interactive plotly graphs which are saved as HTML files.

Is it possible to create a rmarkdown with a dropdown allowing the user to select a location which will load the relevant plotly graphs?

I cannot generate the graphs on the fly, and everything must be stored in HTML files with no server side interaction.

Upvotes: 1

Views: 919

Answers (1)

bschneidr
bschneidr

Reputation: 6277

The simplest solution as of January 2018 is to use tabsets as well as the self_contained: true option in the RMarkdown YAML (see this documentation).

Here are a couple images to show what that would look like. At the end of this post is the RMarkdown used to generate this example.

enter image description here enter image description here

Here's the RMarkdown used to generate this example.

---
title: "Example"
output:
  html_document:
    self_contained: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(magrittr)
library(tidyverse)
library(plotly)
```
# Plots by Location {.tabset}

## First Location
```{r first_location_plot}
first_plot <- iris %>% 
  filter(Species == "setosa") %>% 
  ggplot(mapping = aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(color = 'blue')

ggplotly(first_plot)
```

## Second Location
```{r second_location_plot}
second_plot <- iris %>% 
  filter(Species == "virginica") %>% 
  ggplot(mapping = aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(color = 'red')

ggplotly(second_plot)
```

Upvotes: 2

Related Questions