The tidyverse (and a few base R functions) essentially…
These will be key ingredients in shiny apps!
Alternative: flexdashboard (if you’re curious)
In Python:
Any combination of:
\(\rightarrow\) usually produced by the tidyverse or extensions! (DT, plotly, etc.)
This slide is very important. Refer to it whenever need be.
Just like before (teaching = repeating!):
In the code, steps 1 & 3 will be aggregated in the UI section with a strong differentiation (sidebar versus body).
source: https://shiny.rstudio.com/tutorial/written-tutorial/lesson3/
source: https://shiny.rstudio.com/gallery/widget-gallery.html
source: https://mastering-shiny.org/basic-ui.html#inputs
sliderInput( inputId = "id", # For the server label = "What the user sees", # For the UI min = 0, max = 100, value = 50, # Default value step = 5)
In short:
sliderInput("bla", h3("BlaBla"), # For the UI min = 0, max = 100, value = 50, step = 5)
For each output type, there is a particular function:
Content | UI (displaying) | Server (rendering = “creating”) |
---|---|---|
simple numbers | valueBoxOutput() or infoBoxOutput() | renderValueBox() of renderInfoBox() |
tables (DT package) | dataTableOutput() | renderDataTable() |
simple plots | plotOutput() | renderPlot() |
enhanced plots | plotlyOutput() | renderPlotly() |
text | textOutput() | renderText() |
In short, this gives (I repeat!): \[\text{UI sidebar} \overset{input}{\rightarrow} \text{Server} \overset{output}{\rightarrow} \text{UI body}\]
Paraphrased:
UI sidebar (input), Server (input transformed in output), UI body (output).
When the user changes some parameters in the UI (sidebar in a shinydashboard), this impacts what happens in the server.
In practice, it is better to resort to reactivity at an early stage in the structure, i.e., on the data source:
data <- reactive({ # Creates the dynamic data emissions %>% # Filter years, seasons & gender filter(year >= input$years[1], year <= input$years[2]) })
This is done via the reactive() function. \(\rightarrow\) only used in the server section.
Reactive objects are functions. BEWARE: the syntaxes are not always obvious.
With a direct call and no return() in the reactive part.
server <- function(input, output){ react_obj <- reactive({ # Creates the dynamic data emissions %>% # Filter years filter(year >= input$years[1], year <= input$years[2]) }) output$pt <- renderDataTable(react_obj()) # Create the output object! }
They are super useful for dynamic (reactive) filters!!! The structure is simple:
if(condition){
instruction 1 (expression)
instruction 2 (expression)
instruction 3 (expression) ….
}
h1(“text”) is for very large text.
…
h6(“other text”) is for very small text.
SIMPLE (that’s one short slide)!
In the UI section:
In the server section:
tail(emissions)
country | year | population | gdp | energy_consumption | source | emissions | |
---|---|---|---|---|---|---|---|
155929 | Zimbabwe | 2020 | 14.9 | NA | NA | all | 10.531 |
155930 | Zimbabwe | 2020 | 14.9 | NA | NA | coal | 6.257 |
155931 | Zimbabwe | 2020 | 14.9 | NA | NA | cement | 0.697 |
155932 | Zimbabwe | 2020 | 14.9 | NA | NA | flaring | NA |
155933 | Zimbabwe | 2020 | 14.9 | NA | NA | gas | NA |
155934 | Zimbabwe | 2020 | 14.9 | NA | NA | oil | 3.576 |
factor(c("small", "large", "medium", "small"), levels = c("small", "medium", "large"), ordered = TRUE)
## [1] small large medium small ## Levels: small < medium < large
Easy: use the save() function. Keep the original RData format, it’s very convenient and well compressed.
test <- sqrt(1:9) save(test, file = "test.RData")
You can then access the data via load():
load("test.RData")
Just make sure you are in the right directory!
Two sides:
Chronologically, the way the app processes the flow:
Tip: for your project, choose a dataset with a mixture of numerical and categorical data. It is the best combination to create a nice looking dashboard!