Shiny basics for interactive apps in R Programming - Time & Space Complexity
When building interactive apps with Shiny, it's important to know how the app's response time changes as users interact more or provide larger inputs.
We want to understand how the app's work grows when inputs change or increase.
Analyze the time complexity of the following Shiny server code snippet.
server <- function(input, output) {
output$sum <- renderText({
total <- sum(input$numbers)
paste("Sum is", total)
})
}
This code calculates the sum of a numeric input vector whenever the user changes it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Summing all numbers in the input vector.
- How many times: Once each time the input changes, the sum function goes through all numbers.
As the number of input numbers grows, the time to sum them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of input numbers.
Time Complexity: O(n)
This means the time to update the sum grows in a straight line as the input size grows.
[X] Wrong: "The sum calculation is instant no matter how many numbers there are."
[OK] Correct: Summing requires looking at each number once, so more numbers mean more work and more time.
Understanding how input size affects app responsiveness shows you can build smooth user experiences and write efficient reactive code.
"What if we changed the sum to calculate the sum of squares of the input numbers? How would the time complexity change?"
Practice
ui part?Solution
Step 1: Understand the purpose of
Theuiin Shinyuidefines the layout and elements the user interacts with, like buttons and plots.Step 2: Differentiate from
Theserverroleserverhandles logic and calculations, not theui.Final Answer:
To define what the user sees and interacts with -> Option BQuick Check:
ui= user interface [OK]
- Confusing
uiwithserver - Thinking
uidoes data processing - Assuming
uistores data
Solution
Step 1: Recall numericInput syntax
The correct order isnumericInput(inputId, label, value).Step 2: Check each option
numericInput(inputId = "num", label = "Number", value = 0) matches the correct syntax exactly; others have wrong argument order or missing arguments.Final Answer:
numericInput(inputId = "num", label = "Number", value = 0) -> Option AQuick Check:
Correct argument order = numericInput(inputId = "num", label = "Number", value = 0) [OK]
- Mixing argument order
- Omitting inputId
- Using unnamed arguments incorrectly
server <- function(input, output) {
output$text <- renderText({
paste("Hello", input$name)
})
}and the UI has a textInput with
inputId = "name" where the user types "Alice"?Solution
Step 1: Understand renderText and input usage
TherenderTextfunction returns text that updates automatically usinginput$name.Step 2: Substitute input$name with user input
User typed "Alice", sopaste("Hello", input$name)becomes "Hello Alice".Final Answer:
Hello Alice -> Option DQuick Check:
Dynamic text output = "Hello Alice" [OK]
- Thinking input$name prints literally
- Confusing renderText with renderPrint
- Expecting error due to input usage
server <- function(input, output) {
output$plot <- renderPlot({
plot(data)
})
}Assuming
data is not defined anywhere else.Solution
Step 1: Check for variable definitions
The code usesdatainsideplot()butdatais not defined anywhere.Step 2: Understand consequences of missing data
This will cause an error because R cannot find the objectdata.Final Answer:
Missing definition of 'data' causes an error -> Option AQuick Check:
Undefined variable = error [OK]
- Assuming plot() is invalid in renderPlot
- Confusing output variable names
- Thinking renderPlotly is required
Solution
Step 1: Choose correct input control for variable selection
selectInputlets user pick a variable name from choices.Step 2: Use input to select data column in server
Access the chosen variable asdata[[input$var]]and plot histogram withrenderPlot.Final Answer:
UseselectInputin UI for variable choice, then in server userenderPlotto plot histogram ofdata[[input$var]]-> Option CQuick Check:
Dropdown input + dynamic plot = UseselectInputin UI for variable choice, then in server userenderPlotto plot histogram ofdata[[input$var]][OK]
- Using numericInput instead of selectInput
- Trying to plot input$var directly without data
- Using sliderInput for variable selection
Ready to prove it?
Pass the test and this pattern moves unknown
