Responsive plolty output in Rmarkdown documents as in shiny apps

I am trying to generate a fluid and responsive plotly output in a Rmarkdown document as I normally get in shiny apps, but by default the output overflow in small screens.

I need to know if I have to include any dependency to my Rmarkdown document, or activate any chunk option in order to make it work properly.

See the example below.

Shiny app

library(shiny)
library(plotly)
library(ggplot2)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
      static <- ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
          geom_point()
      
      ggplotly(static)
  })
}

shinyApp(ui, server)

The output is a html document with a responsive plot:

Rmarkdown html document

---
output: html_document
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}

library(plotly)
library(ggplot2)

static <- ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
          geom_point()
      
      ggplotly(static)

The output is also a html document but without a responsive plot

Thanks in advance for your attention.

This topic was automatically closed 21 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.

I ran you code and got a correctly adjusted plot

---
output: html_document
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}

library(plotly)
library(ggplot2)

static <- ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
          geom_point()
      
ggplotly(static)
```

FAQ: How to Format R Markdown Source