Embedding Shiny with inline not rendering with HTML output

I have a Rmd file with html output. When I run rmarkdown::render_site() I take the files generated within _site and add them to a webserver. Then, when I visit the URL everything works and I can see my analysis as an html document online.

I attempted to add a shiny code block to my analysis using the inline embed method.

When I run just this code chunk by clicking the 'run current chunk' option within the Rmd file it works in that a popup window appears and I can see my app render.

However, when I run the entire document, everything renders except the shiny part. Instead there's a grey rectangle square in the place where the shiny app was expected to appear.

I'll add information here that I think may be relevant, let me know if there's more ifnormation I should provide.

My Rmd file looks like this:

Th yml part at the top. I also have this exact same code in a separate file called _site.yml

title: "My Analysis"
subtitle: "Day 7 to day 365 analysis"
date: "`r Sys.Date()`"
author: "My Name"
    toc: true
    toc_collapsed: true
    df_print: kable
link-citations: yes
runtime: shiny

Then I have several code chunks:
```{r setup, include=FALSE}
# code here

```{r somecode, include=FALSE}
# code here

Now for the chunk with inline shiny:

{r shiny_app, echo = FALSE}

  ui = fluidPage(

    # inputs
    radioButtons("usa", "United States", choiceNames = c("USA", "ROW"), choiceValues = c(1, 0)),
    radioButtons("fb", "Facebook Connected", choiceNames = c("Is Connected", "Not Connected"), choiceValues = c(1, 0)),
    radioButtons("paid", "Paid Source", choiceNames = c("Paid", "Organic"), choiceValues = c(1, 0)),
    radioButtons("returner", "Returning User", choiceNames = c("Returner", "Single Session"), choiceValues = c(1, 0)),
    sliderInput("sessions", "Sessions", min = 1, max = 150, value = 30),
    sliderInput("rating", "Avg Star Rating", min = 2.5, max = 5, value = 3, step = 0.1),
    sliderInput("submissions", "Design Submissions", min = 0, max = 55, value = 10, step = 1),

server = function(input, output) {
filtered_row <- reactive({
  # median of spend distribution to feed into predict (one row to mutate)
  user_inputs <- model_dat %>% select(matches("Dist")) %>% lapply(median) %>% unlist() %>% t() %>% data.frame() %>% 
      united_states = input$usa,
      facebook_connected = input$fb,
      paid_source = input$paid,
      returner = input$returner,
      sessions = input$sessions,
      avg_rating_received = input$rating,
      submissions = input$submissions
    ) %>%
    mutate_all(as.numeric) %>% 
      fb_x_sessions = facebook_connected * sessions,
      fb_x_united_states = facebook_connected * united_states,
      avg_rating_receivedSqd = avg_rating_received^2.0,
      submissionsSqd = submissions^2.0,
      sessionsSqd = sessions^2.0

  output$predicted_rpi = renderTable(predict(lm_model_refined_log, newdata = filtered_row()) %>% exp())


  options = list(height = 1500)


Here is a screen shot of the html doc that resulted from running the code. After inspecting the area where the shiny app should be it looks like an iframe except it is empty. How can I get the shiny part to render here?

Here is the contents of _site folder after running rmarkdown::render_site():
I'm not sure if more files are expected to be in here?

How do you run the document ?
I see a mention of a render_site in your post.

Shiny document are run and not rendered - the function use is rmarkdown::run and not rmarkdown::render. When adding runtime: shiny, RStudio IDE knit button change into Run Document. So if you use rmarkdown::render or if rmarkdown::render_site does not handle shiny document, the part with shiny app is not created.

Let's recall that a shiny document can't be rendered to a static html file you can but in a static website. It is way of creating shiny app from a single Rmd document, but it is still require to be hosted and run when consulted. shiny_prerendered feature helps run it quicker by pre renderingall the static part.

I would advice to consult the different ressource available to understand how this works

Know that, for including an app into a static document or website, like a bookdown, you can host the shinyapps somewhere (like shinyapps.io) and include it in the document with knitr::include_app. see 2.11 Web pages and Shiny apps | bookdown: Authoring Books and Technical Documents with R Markdown

Hope it helps.

1 Like

The other way to achieve interactivity in static HTML that's output from an R Markdown document is via HTML widgets. These are a family of R wrappers for JavaScript libraries, so all the interactive code execution happens on the client side in your web browser. But they tend to be mostly clickable visualizations, rather than fully reactive interfaces like the ones that Shiny builds. See:

If you find that the differences between all these types of interactive documents and apps are hard to wrap your head around, you're not alone! Speaking for myself, I wish there were a comprehensive overview comparing the various options and their limitations. There's an open issue requesting such documentation on the shiny GitHub. If you agree, you can help by voting with an emoji reaction, or by adding your thoughts:

Thanks for the links. Those links made it clear:

Note: If you are familiar with R Markdown, you might expect RStudio to save an HTML version of an interactive document in your working directory. However, this only works with static HTML documents. Each interactive document must be served by a computer that manages the document. As a result, interactive documents cannot be shared as a standalone HTML file.

This is the confusion I had.

But it sounds like if I install Shiny server and host the app there on the same server where I host RStudio, I can link to an app on my server and use an iframe to show the app in an html site built with Markdown.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.