Learning about Shiny with RMD

I’ve played around with Rstudio’s Shiny and used it to produce web pages driven with R. Although it’s not the only path from R to the web, Shiny is a really convenient way to build sophisticated dashboards.

I’ve used Shiny, but haven’t had time to explore the advanced capabilities. With delight, I found “Creating Interactive Presentations with Shiny and R” available from LinkedIn Learning, authored by Martin Hadley. In this course, Martin discusses how to¬† use Shiny to produce interactive documents, as well as websites at www.shinyapps.io. Watching Martin’s course taught me several things I probably would have never noticed. Most importantly, I didn’t realize shiny could be used with R Markdown.

Welcome from Creating Interactive Presentations with Shiny and R by Martin Hadley

In chapter 4, Martin details how to an R Markdown document with interactive Shiny components.

  1. Create an R Markdown document. (RStudio->File->New File->R Markdown)
  2. In the header of the Rmd file, include “runtime: shiny”
  3. Create an R statement that would benefit from interactivity. The example Martin uses looks like….
    ```{r curve_static}
    curve(x^2, from = -5, to=5)
    ```
  4. Load the shiny app function in the Rmd.
    ```{r curve_shiny}
    library(shiny)
    shinyApp(
       ui= fluidPage(
          sliderInput("exponent",
             label = "Exponent",
             min = 1,
             max = 5,
             value = 2),
          plotOutput("curvePlot")
       ),
       server = function(input,output) {
          output$curvePlot <- renderPlot(
             curve(x^input$exponent, from = -5, to = 5)
       }
    ```
  5. In RStudio, there should be a “run presentation” button at the top. Press that button. The interactive element should now work.

I’ve abbreviated the process, you can watch Martin discuss this in more detail in the following video…

Add a shinyApp to a presentation from Creating Interactive Presentations with Shiny and R by Martin Hadley

Leave a Reply

Your email address will not be published. Required fields are marked *