Embed rmarkdown html file in shiny app based on input (with htmlwidgets)

Hi Everyone,

I'm a little stuck and would love some help. I'm trying to place a rendered rmarkdown html file in a shiny app based on an input. The trick here is also that the rmarkdown files often include ggiraph/plotly widgets. So far I've tried:

  • putting the includeHTML function on the server side within a renderUI(). However, the widgets don't show up. (I think that these add code to the html head which might be the issue here, but this is beyond my knowledge).
  • keeping includeHTML on the client side and using a conditionalPanel() to only show it if the input is true. html document shows up all the time.
  • same as #2 but using shinyjs hidden() and toggle() to show the html file. (I'm thinking the html filename would become a variable in this scenario...but TBD). same problem.
  • iframes. - they work-ish, but take forever to load on shinyapps.io (I assume because of a large html file size) and aren't really that responsive and sometimes the css doesnt seem to load (even with self_contained:true). I'd love to avoid iframes if possible.

Edit: Huge bonus points if any solution accomodates distill_articles because they are beautiful

thanks!

Reprex:

Here's an rmarkdown doc with ggiraph plot:

---
title: "reprex"
date: "4/20/2021"
output:
  html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
library(ggplot2)
library(ggiraph)
g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) )
my_gg <- g + geom_point_interactive(aes(tooltip = model), size = 2)
girafe(code = print(my_gg) )
```

Shiny app:

library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
  checkboxInput("box", "show rmd doc", value = FALSE),
  htmlOutput("rmark")
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$rmark <- renderUI({
    req(input$box==TRUE)
    includeHTML("reprex.html")
  })
  
}
# Run the application 
shinyApp(ui = ui, server = server)

Alternative shiny app:

library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
  checkboxInput("box", "show rmd doc", value = FALSE),
  conditionalPanel("input$box == false",
   includeHTML("reprex.html")
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  
}
# Run the application 
shinyApp(ui = ui, server = server)
2 Likes

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.