UI and Server Components
User Interface (UI)
The User Interface defines the layout, structure, and visual elements of the application that users interact with. It encompasses everything visible to the user, from buttons and input fields to plots and output displays.
Examples of UI Elements:
- Input Fields: Text inputs, sliders, dropdown menus.
- Action Elements: Buttons, checkboxes, radio buttons.
- Output Elements: Plots, data tables, text outputs.
It allowing us to design and customize the appearance of their applications without delving into web development languages like HTML or CSS.
Server Logic
The Server component is where the computational and reactive aspects of the application reside. It consists of R code that responds to user inputs, processes data, and generates dynamic outputs in real-time.
1. Reactive Expressions: Functions that automatically update when their inputs change. They are used to compute intermediate values or perform calculations based on user input.
Syntax:
reactive_expr <- reactive({
input_val <- input$numInput
computed_val <- input_val * 2
return(computed_val)
})
2. Reactive Outputs: Output elements in the UI that are dynamically updated based on changes in reactive expressions or other inputs.
Syntax:
output$plot <- renderPlot({
input_val <- input$numSlider
plot(x = seq(1, input_val), y = seq(1, input_val), type = "l")
})
3.Reactive Dependencies: The connections between reactive expressions, inputs, and outputs that define the flow of data and interactions within the application.
Syntax:
observeEvent(input$actionBtn, {
output$txtOutput <- renderText({
req(input$txtInput) # Ensures input is not NULL
return(paste("You entered:", input$txtInput))
})
})
Through reactive programming, the Server component ensures that the application remains responsive and adapts to user actions without the need for manual intervention.
Users input a number, and the app immediately doubles it and displays the result.
- They can select a number on the slider, and the app generates a plot showing a sequence of numbers from 1 to the selected value.
- Users can enter text, and when they click the “Display Message” button, the app shows the entered text.
library(shiny)
# Define UI
ui <- fluidPage(
titlePanel("Reactive Programming Example"),
sidebarLayout(
sidebarPanel(
numericInput("numInput", "Enter a number:", value = 5),
sliderInput("numSlider", "Select a number:", min = 1, max = 10, value = 5),
actionButton("actionBtn", "Display Message"),
textInput("txtInput", "Enter text:")
),
mainPanel(
textOutput("doubleOutput"),
plotOutput("plot"),
textOutput("messageOutput")
)
)
)
# Define Server logic
server <- function(input, output, session) {
# Reactive expression to double the numeric input
output$doubleOutput <- renderText({
input_val <- input$numInput
computed_val <- input_val * 2
paste("Double of", input_val, "is", computed_val)
})
# Reactive plot output based on the slider value
output$plot <- renderPlot({
input_val <- input$numSlider
plot(x = seq(1, input_val), y = seq(1, input_val), type = "l",
main = paste("Sequence from 1 to", input_val))
})
# Observer for button click event
observeEvent(input$actionBtn, {
output$messageOutput <- renderText({
req(input$txtInput) # Ensures input is not NULL
paste("You entered:", input$txtInput)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
Output:
R Shiny Examples
Shiny is an R package that allows us to build interactive web applications directly from the R Programming Language. It bridges the gap between data analysis in R and web development, enabling us to create interactive dashboards.