Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotly not working ? #744

Closed
munoztd0 opened this issue Oct 20, 2022 · 6 comments · Fixed by #747
Closed

Plotly not working ? #744

munoztd0 opened this issue Oct 20, 2022 · 6 comments · Fixed by #747

Comments

@munoztd0
Copy link

munoztd0 commented Oct 20, 2022

Can anyone explain why basic ggplotly object doesn't render on learnR tutorials ?

Or do I have to to something special ?

Code:

```{r interactive, exercise = TRUE, exercise.eval = FALSE, warning=FALSE}
#hint: load hrbrthemes and plotly
```

```{r interactive-solution, warning=FALSE}
library(hrbrthemes) # for general style
library(plotly)     # to make the chart interactive
library(tidyverse)     
library(gapminder )    

p <- gapminder |> 
  filter(year=="1952" & country!="Kuwait") |>
  arrange(desc(pop)) |>
  ggplot( aes(x=gdpPercap, y=lifeExp, fill=continent, size=pop)) +
    geom_point(alpha=0.7, stroke="white", shape=21) +
    theme_ipsum()

ggplotly(p)
```
@gadenbuie
Copy link
Member

Try including a plotly plot in the static portion of your content. We remove web dependencies from code rendered in exercises since it would allow users to add arbitrary web deps to your main tutorial. If you include the plotly dependencies in your tutorial, by adding a plotly plot to the static part, then exercise results should work as expected. If not, feel free to follow up.

@munoztd0
Copy link
Author

Ok so basically is impossible for students to input the code and see the results by themselves ?

@gadenbuie
Copy link
Member

Ok so basically is impossible for students to input the code and see the results by themselves ?

No it's very possible but you have to anticipate the usage.

@munoztd0
Copy link
Author

Try including a plotly plot in the static portion of your content. We remove web dependencies from code rendered in exercises since it would allow users to add arbitrary web deps to your main tutorial. If you include the plotly dependencies in your tutorial, by adding a plotly plot to the static part, then exercise results should work as expected. If not, feel free to follow up.

No it's very possible but you have to anticipate the usage.

I don't understand, how can you do it then ?

@gadenbuie
Copy link
Member

gadenbuie commented Oct 22, 2022

Here's an example. If you show students a plotly plot in your tutorial, the dependencies are loaded and available when their code returns a plotly plot.

---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
library(hrbrthemes) # for general style
library(plotly)     # to make the chart interactive
library(tidyverse)     
library(gapminder)  
knitr::opts_chunk$set(echo = FALSE)
```


## Topic 1

Try to recreate this plot, but better looking.

```{r preview, warning = FALSE}
p <- gapminder %>%
  filter(year == "1952" & country != "Kuwait") %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, fill = continent)) +
  geom_point(alpha = 0.7, stroke = "white", shape = 21)

ggplotly(p)
```

### Exercise 

```{r interactive, exercise = TRUE, exercise.eval = FALSE, warning=FALSE}
p <- gapminder %>%
  filter(year == "1952" & country != "Kuwait") %>%
  arrange(desc(pop)) %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, fill = continent, size = pop)) +
  geom_point(alpha = 0.7, stroke = "white", shape = 21) +
  theme_ipsum()

ggplotly(p)
```

```{r interactive-solution, warning=FALSE}
p <- gapminder %>%
  filter(year == "1952" & country != "Kuwait") %>%
  arrange(desc(pop)) %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, fill = continent, size = pop)) +
  geom_point(alpha = 0.7, stroke = "white", shape = 21) +
  theme_ipsum()

ggplotly(p)
```

@gadenbuie
Copy link
Member

@munoztd0 I'm sorry, I was misremembering how html dependencies are handled in learnr. We do filter out some web dependencies – in particular to limit dependencies to only those served by packages — but your example should have worked without modifications.

I have a fix in #747 that I will merge shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants