class: center, middle, inverse, title-slide .title[ # Your first Shiny app ] .subtitle[ ## If it’s not worth putting in a Shiny app it’s not worth doing. ] .author[ ### Riinu Pius, www.riinu.me ] .date[ ### Created: 2017-10-17, Updated: 2022-05-27 ] --- You've made a nice plot: ```r library(tidyverse) library(gapminder) gapminder %>% filter(year == 2007) %>% ggplot(aes(y = lifeExp, x = continent)) + geom_jitter(aes(size = pop/1000000, fill = lifeExp), shape = 21, colour = "grey", alpha = 0.8, width = 0.15) + scale_fill_distiller(palette = "Paired") + coord_cartesian(ylim = c(30,85)) + scale_size("Population, millions", range = c(5, 25), breaks = c(10, 50, 100, 250, 1000)) + theme_bw() + labs(caption = "Each bubble is a country.", ylab = "Life expectancy") + theme(aspect.ratio=0.6) ``` <!-- --> ...that you want to make into a Shiny app. You can do so by following just 5 easy steps. --- **STEP 0**: `install.packages("shiny")`. Use RStudio. **STEP 1**: Create a script called `app.R` using this skeleton: ```r library(shiny) library(tidyverse) library(gapminder) # User Interface ui <- basicPage( plotOutput(outputId = "myplot") ) # Server server <- function(input, output) { output$myplot <- renderPlot({ #your plot code here: }) } shinyApp(ui, server) ``` This does not have to be in a "Shiny project", you can create the file `app.R` in whichever project/folder you are currently working in. --- **STEP 2**: Copy your plot code into the renderPlot function: ```r # Server server <- function(input, output) { output$myplot <- renderPlot({ #your plot code here: gapminder %>% filter(year == 2007) %>% ggplot(aes(y = lifeExp, x = continent)) + geom_jitter(aes(size = pop/1000000, fill = lifeExp), shape = 21, colour = "grey", alpha = 0.8, width = 0.15) + scale_fill_distiller(palette = "Paired") + coord_cartesian(ylim = c(30,85)) + scale_size("Population, millions", range = c(5, 25), breaks = c(10, 50, 100, 250, 1000)) + theme_bw() + labs(caption = "Each bubble is a country.", ylab = "Life expectancy") + theme(aspect.ratio=0.6) }) } ``` Press `Control+Shift+Enter` or the "Run App" button. Your first Shiny app will pop up. --- ## Print is dead We will now make one thing - the year - interactive. **STEP 3**: Add a `sliderInput` to your User Interface (`ui`): ```r # User Interface ui <- basicPage( sliderInput("year", "Select year:", min = 1952, max = 2007, value = 2007, step = 5, # so thousands are not separated with a comma # (without sep = "" the scale defaults to 1,952 - 2,007) sep = "" ), # note this comma here - different to our usual R code plotOutput(outputId = "myplot") ) ``` `Control+Shift+Enter` again. Your Shiny app now has a slider, but moving it does not change the plot. Yet. --- **STEP 4**: Tell your Server you wish the `dplyr::filter()` to use the value from the slider. All inputs from the User Interface (`ui`) are stored in `input$variable_name`: replace the `2007` with `input$year`. Change this ```r gapminder %>% * filter(year == 2007) %>% ggplot(aes(y = lifeExp, x = continent)) + ... ``` to this: ```r gapminder %>% * filter(year == input$year) %>% ggplot(aes(y = lifeExp, x = continent)) + ... ``` Press `Control+Shift+Enter` or the "Run App" button. You now have a fully functioning Shiny app and all you had to do was wrap your beautiful plot code inside some `({})`s, add a `sliderInput()`, and replace `2007` with `input$year`. --- ## Animate **STEP 5**: Add `animate = TRUE` inside `sliderInput()`: ```r # User Interface ui <- basicPage( sliderInput("year", "Select year:", * animate = TRUE, min = 1952, max = 2007, value = 2007, step = 5, # so thousands are not separated with a comma # (without sep = "" the scale defaults to 1,952 - 2,007) sep = "" ), # note this comma here - different to our usual R code plotOutput(outputId = "myplot") ) ``` You'll now see a little play button below the slider. --- The final resulting app and code: https://riinu.shinyapps.io/shiny_testing/ * You now understand the basic skeleton - that there is a user interface (ui) and a server. * More about getting started with Shiny can found here: https://shiny.rstudio.com/articles/basics.html * Free Shiny book: https://mastering-shiny.org/ * It is possible to separate app.R into two separate scripts: ui.R and server.R. This does not change the functionality but allows to better organise your code. Furthermore, if you find yourself writing very large and complicated Shiny apps, then look into 'modularizing Shiny code'. `library(golem)` is particularly helpful for creating modular Shiny apps. * This tutorial runs a Shiny app on your computer. To share it with the world it will need to be deployed to a server running R. - It's easiest to use https://www.shinyapps.io/, a few small apps are free to deploy and share - Your company/team/institute may host Shiny apps using RStudio Connect software - Or you may install Shiny Server Opensource on a server you own and manage: https://www.rstudio.com/products/shiny/download-server/ Instructions by: [www.riinu.me](www.riinu.me)