Code
library(shiny)
ui <- fluidPage(
sliderInput("year", "Choose year", value = 2024, min = 2011, max = 2024)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Objectives
In this file I will try out things I have learned about {shiny} with my practice example of WHR data.
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
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
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.
I will come back shiny::sliderInput()
resp. shinyWidgets::sliderTextInput()
to check the following options:
shiny::sliderInput()
shinyWidgets::sliderTextInput()
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.