Creating an interactive report with Shiny

This recipe gives you a perfect tool for sharing analysis results with third parties.

Interactive reports are electronic documents enriched by Shiny functionalities, giving the user the ability to change the assumptions on which analyses are based and consequently changing the document's output.

You should now be comfortable with your analysis, autonomously answering questions that arise while reading.

How to do it…

  1. Create a Shiny document:
    How to do it…
  2. Remove the default content yaml parameters.
  3. Add a setup chunk:
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
  4. Embed your Shiny app:
    ```{r tabsets, echo=FALSE}
    shinyAppDir(
      "app.R",
      options = list(
        width = "100%", height = 550
      )
    )
    ```
  5. Add an input panel and a related plot output:
    ```{r eruptions, echo=FALSE}
    inputPanel(
      selectInput("selected_species", label = "iris species to plot",
                  choices = c("virginica","setosa","versicolor"), selected = "versicolor")
      
    )
    data <- reactive({
      subset(iris,iris$Species == input$selected_species)
    })
    renderPlot({
      plot(data())
    })
    ```
  6. Style your document.

How it works...

We first create a Shiny document. Shiny documents are actually R Markdown documents where an additional render argument is set to shiny. This argument is specified within the yaml chunk, as seen in the Using one markup language for all types of documents – rmarkdown recipe.

Next, we remove the default content, except the yaml parameters.

We don't want the default content to confuse us, do we? So, let's clean up our paper, leaving only our dear yaml chunk.

Next, we add a setup chunk. Setup chunks are chunks of R code where some general and preliminary activities are performed, such as package loading and data frame importing. Take a look at the Using one markup language for all types of documents – rmarkdown recipe.

We then embed our Shiny app. There are two ways of embedding a Shiny app in your document:

  • Embedding a previously developed Shiny app in your document by leveraging shinyAppDir()
  • Introducing inputpanel and a consequent ui element, such as plots or tables

The second option is treated in the next step, while here we source our previously developed app, passing only the relative path, since it is placed in the project directory.

Be aware of the option can specify within the options argument, which is the same as that available for the runApp function (see runApp for more on them), plus specific controls for the width and height of the app shown in your document.

Further on, add an input panel and a related plot output.

This is a fairly specific way of using the Shiny framework, since we don't have to specify any server object or ui object to use control widgets and output objects.

The logical model behind this implementation is in some way different from the general approach in the following ways:

  • An input panel is required to store all control widgets, meaning all those elements that will appear in the document to let the user perform choices such as data filtering or model assumptions
  • Using a render function to show the results of user choices, such as renderPlot and renderTable

What is missing here? The output function. For instance, when dealing with a plot, you will not require a plotOutput function, since the renderplot function will do the job.

Lastly, we style our document. As we discussed at the beginning of this recipe, Shiny documents are R Markdown documents empowered by Shiny framework functionalities. This means you can leverage all the great features of R Markdown documents to style and customize your report. Refer to the Using one markup language for all types of documents – rmarkdown recipe for further information on this topic.

See also

As usual with Shiny, the sky is the limit. Given the relatively young age of the framework, a lot of development is still to come.

I suggest that you always keep an eye on the Shiny website, especially for this particular topic, on the section in the R Markdown website dedicated to Shiny at http://rmarkdown.rstudio.com/authoring_shiny.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset