Appendix B: WHR exercises

Content for WHR exercises

Objectives

In this file I will try out things I have learned about {shiny} with my practice example of WHR data.

B.1 Choosing the year via a slider

In WHR I have datasets for the years 2011, 2012, 2014-2024. (Yes, 2013 is missing!)

At first I thought to use radio boxes to choose the year the country map should show. But after reading “Numeric Input” in Section 2.2.3 I decided for the better alternative of a sliderInput with the available years.

Code Collection B.1 : Choosing the year of the dataset with a slider

R Code B.1 : Choosing the year of the dataset with a slider

Code
library(shiny)

ui <- fluidPage(
  sliderInput("year", "Choose year", value = 2024, min = 2011, max = 2024)
)

server <- function(input, output, session) {

    }

shinyApp(ui, server)

R Code B.2 : Choosing the year of the dataset with a slider

Code
library(shiny)

ui <- fluidPage(
  sliderInput("year", 
      label = "Choose year", 
      value = 2024,
      min = 2011,
      max = 2024,
      sep =""
      )
)

server <- function(input, output, session) {

    }

shinyApp(ui, server)

R Code B.3 : Choosing the year of the dataset with a slider

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 100

library(shiny)

ui <- fluidPage(
  sliderInput("year", "Choose year", value = 2024, min = 2011, max = 2024)
)

server <- function(input, output, session) {

    }

shinyApp(ui, server)

The year is not in the desired format. I do not want the comma as a thousand separator. Reading the documentation I learned about the parameter sep with it default value sep = ",". So with the next tab I will use sep = "" to get rid of the thousand separator.

R Code B.4 : Choosing the year of the dataset with a slider

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 100

library(shiny)

ui <- fluidPage(
  sliderInput("year", 
      label = "Choose year", 
      value = 2024,
      min = 2011,
      max = 2024,
      sep =""
      )
)

server <- function(input, output, session) {

    }

shinyApp(ui, server)

This is now better. But I have still the problem, that there is no data for 2013. I could leave it in the hope that it will not generate an error but showing all countries with NA-values. (Additionally I could place a message saying “For 2013 there is no dataset available”.)

Another option would be trying to use shinyWidgets::sliderTextInput() to construct a slider widget with characters instead of numeric values.

But in both cases I have to use the data to see the effect of the different code snippets.

Another very different option is not to use a slider but a menu! But then I don’t have the animation option anymore.

I will come back shiny::sliderInput() resp. shinyWidgets::sliderTextInput() to check the following options:

  • What happens when in a standard slider configuration people choose the year 2013?
  • What does it look with a message saying “For 2013 there is no dataset available”.
  • Trying out the animation feature for the shiny::sliderInput()
  • Using a specified list of years as character strings with shinyWidgets::sliderTextInput()
  • Try out a menu choice with specified years: Which UI has a better, more natural, feeling?

B.2 Choose year via drop-down menu

In WHR I have datasets for the years 2011, 2012, 2014-2024. Yes, 2013 is missing! Because of the missing year I could use the limited choices option. Radio buttons are for 13 different choices an overhead, so I will go with the drop-down menu.

Code Collection B.2 : Limited choices: Choose year via drop-down menu

R Code B.5 : Limited choices: Choose year via drop-down menu

Code
library(shiny)

years <- c("2011", "2012", "2014", "2015", "2016", "2017",
           "2018", "2019", "2020", "2021", "2022", "2023", "2024")

ui <- fluidPage(
  selectInput("year", 
              "Choose year (2013 are no data available)", 
              base::rev(years)
              )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)