First Shiny instructions
Part 1:
Instructions for Creating Shiny App in Rmarkdown
Click “File” > “New File” > “R Markdown” (Note: Quarto has just recently implemented some of its capabilities for interactive apps, including dashboards, so in 264 we will use Rmarkdown files for stability and also for consistency with past projects that you might want to learn from.)
On Left side, select “Shiny.” You may want to update the Title, but leave the other options as they are. Click “OK”.
Delete the “Embedded Application” section at the end.
Save the resulting file as “first_Shiny.Rmd”.
Carefully examine the code, including the use of “echo = FALSE” to prevent R code from appearing in the Shiny app.
Hit “Run Document” to see the app. Now you are running a Shiny app through Rmarkdown!
Group exercise I:
- Create a second interactive histogram which recreates the 1st histogram (old faithful eruption) as closely as possible using ggplot.
Hint 1: write ggplot code that works, and then “shiny-ize” it
Hint 2: Use
aes(y = after_stat(density))
in the histogram to make histogram and density use same y-axis.Hint 3: Give your input variables different names than in the first histogram.
- Modify the input panel choices for number of bins and bandwidth. Also change the default/starting input for both bins and bandwidth.
Part 2:
See how a few more input features can be used in AER_Shiny.Rmd
, which uses the medical expenditure data set NMES1988
Save this file, then hit “Run Document” to see a few new features (note that you can’t simply run individual R chunks in an Rmd meant to produce a Shiny app)
Note how a Yes/No check box can be integrated with
if
andelse if
Note how user-input variables for ggplot attributes can be integrated with
.data[[.]]
. In some of the sample code from past projects, you may seeaes_string()
used instead of.data[[.]]
.aes_string()
still works, but it was recently deprecated so it will no longer be supported in the tidyverse.
Group exercise II:
Create a new shiny app! Use a dataset from class that’s built into R (e.g., flights, diamonds, ames, NMES1988,…) and create an interactive graph. (Hint: you can see all of the built-in R data sets available to you from the packages currently loaded by running
data()
)You might try having multiple inputs, or examining the Shiny CheatSheet Input Options (https://www.rstudio.com/resources/cheatsheets/) and using an input we haven’t discussed yet.
Links to other Rmd files we will use to produce interactive apps
Remember to hit the Download Raw File button in each case.
We will also use these folders as well. You can copy the URL for the folder into the textbox at download-directory.github.io, hit Enter, and you’ll have a zipped version of the folder.
Check out my Shiny app on Ames Housing: