3

I am creating a RMarkdown HTML document where chunks are sourcing R files:

```{r }
source("test.R")
```

Where test.R is:

library(ggplot2)
library(plotly)

data <- as.data.frame(datasets::mtcars)

create_plot <- function(data, var_x, var_y, var_color, var_size) {
data %>% ggplot(aes_string(
    x = var_x, 
    y = var_y, 
    color = var_color, 
    size = var_size)) + 
    geom_point()
}

p <- create_plot(data, "disp", "qsec", "vs", "hp")

p <- ggplotly(p)

print(p)

This works inline the Rmarkdown document (RStudio) but not when I knitr the document (no plotly output shows up in the generated HTML file). I have tried several alternatives to print(p) such as:

p <- as_widget(p)
print(p)

or

p <- as_widget(p)
htmltools::tagList(p)

but no plot shows up when knitting the HTML document. No warning messages either. Any thoughts?

dbs
  • 31
  • 1

0 Answers0