Recall that in order to add a plot p assigned to an object named x to a Shiny app, you need to:
1. Render the plot object using renderPlot({p}).
2. Assign the rendered plot to output$x.
3. Display the plot in the UI using plotOutput(“x”).
You can use layout functions provided by Shiny to arrange the UI elements. In this case, we want to use a sidebarLayout(), where the input is placed inside a sidebarPanel() and the output is placed inside the mainPanel(). You can use this template to update the layout of your app.
sidebarLayout(
sidebarPanel(p("This goes into the sidebar on the left")),
mainPanel(p("This goes into the panel on the right"))
)
In order to add any output to a Shiny app, you need to: 1. Create the output (plot, table, text, etc.). 2. Render the output object using the appropriate render___ function. 3. Assign the rendered object to output$x. 4. Add the output to the UI using the appropriate ___Output function.
There are multiple htmlwidgets packages like DT, leaflet, plotly, etc. that provide highly interactive outputs and can be easily integrated into Shiny apps using almost the same pattern.
The magic behind Shiny is driven by reactivity. As you learned in this lesson, there are three types of reactive components in a Shiny app.
Reactive source: User input that comes through a browser interface, typically.
Reactive conductor: Reactive component between a source and an endpoint, typically used to encapsulate slow computations.
Reactive endpoint: Something that appears in the user's browser window, such as a plot or a table of values.
ui <- fluidPage(
titlePanel('BMI Calculator'),
theme = shinythemes::shinytheme('cosmo'),
sidebarLayout(
sidebarPanel(
numericInput('height', 'Enter your height in meters', 1.5, 1, 2),
numeriInput('weight', 'Enter your weight in Kilograms', 60, 45, 120)
),
mainPanel(
textOutput("bmi"),
textOutput("bmi_range")
)
)
)
server <- function(input, output, session) {
rval_bmi <- reactive({
input$weight/(input$height^2)
})
output$bmi <- renderText({
bmi <- rval_bmi()
paste("Your BMI is", round(bmi, 1))
})
output$bmi_range <- renderText({
bmi <- rval_bmi()
health_status <- cut(bmi,
breaks = c(0, 18.5, 24.9, 29.9, 40),
labels = c('underweight', 'healthy', 'overweight', 'obese')
)
paste("You are", health_status)
})
}
shinyApp(ui, server)
A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. Reactive expressions are lazy and cached.
One of the central tenets of reactivity is that reactive expressions are executed lazily, and their values are cached.
Lazy: Evaluated only when it is called, typically by a reactive endpoint.
Cached: Evaluated only when the value of one of its underlying dependencies changes.
Ordinarily, the simple act of reading a reactive value is sufficient to set up a relationship, where a change to the reactive value will cause the calling expression to re-execute. The isolate() function allows an expression to read a reactive value without triggering re-execution when its value changes.
Shiny’s reactive programming framework is designed such that any changes to inputs automatically updates the outputs that depend on it. In some situations, we might want more explicitly control the trigger that causes the update.
The function eventReactive() is used to compute a reactive value that only updates in response to a specific event.