9 Embedding Shiny Apps

Now that you have created a Shiny app, it is time to share it within a webpage. You can either use your Shiny app itself as the webpage (and further customize your app with CSS and JavaScript), or you can embed your Shiny app in another webpage. Long-term, embedding the Shiny app in another page is the better choice. If the location of the app is updated (change of app name, change of owner), you will only have to update the one website where the app is embedded, rather than every external link that points to it.

Embedding your app in an R Markdown or HTML document is simple.

To include your Shiny app in an R Markdown document, use the include_app() function from knitr. Provide it with your app URL. Change the height argument as necessary:

knitr::include_app("https://sscc.wisc.edu/shiny/users/USERNAME/FOLDERNAME/", height = "600px")

To embed your app in an HTML document, use the iframe tag, changing the width and height arguments as needed.

<iframe width="100%" height="600px" src="https://sscc.wisc.edu/shiny/users/USERNAME/FOLDERNAME/"> </iframe>

9.1 Exercises

Create Your Own App:

  1. Create a webpage with your app embedded in it through R Markdown.
    • If you are new to R Markdown, do not worry. Simply go to File > New File > R Markdown. Click OK. Insert a new R chunk into your document as follows (including the back ticks `), modifying the link to point to your Shiny app:

      ```{r}
      knitr::include_app("https://sscc.wisc.edu/shiny/users/USERNAME/FOLDERNAME/", height = "600px")
      ```
    • Then, click Knit at the top of the document to create an HTML file.

  2. View your page in a browser.
    • If you are an SSCC member, place the HTML file in your Z:/PUBLIC_web/ directory. The website will be accessible at https://sscc.wisc.edu/~USERNAME/FILENAME.html
    • If you are not an SSCC member, simply double-click the HTML file produced by R Markdown to open the page in a browser.

If you received an error when trying to run your app via the web, see the next chapter on Troubleshooting Errors.