Case Studies: Building Web Applications with Shiny in R
Dean Attali
Shiny Consultant
x
changes, anything that relies on x
is re-evaluatedContrast with regular R:
x <- 5
y <- x + 1
x <- 10
What is the value of y? 6 or 11?
input$<inputId>
inside render function will cause output to re-render
output$my_plot <- renderPlot({
plot(rnorm( input$num ))
})
output$my_plot
depends on input$num
input$num
changes ⇒
output$my_plot
reacts
render*()
function is a reactive contextserver <- function(input, output) {
print(input$num)
}
ERROR: Operation not allowed without an active reactive context.
observe({ ... })
to access reactive variable
server <- function(input, output) {
observe({
print( input$num )
})
}
Useful for debugging, track reactive variable
observe({
print( input$num1 )
print( input$num2 )
})
reactive({ ... })
to create reactive variable
Wrong:
server <- function(input, output) {
x <- input$num + 1
}
ERROR: Operation not allowed without an active reactive context.
Correct:
server <- function(input, output) {
x <- reactive({
input$num + 1
})
}
()
server <- function(input, output){
x <- reactive({
input$num + 1
})
observe({
print( input$num )
print( x() )
})
}
Case Studies: Building Web Applications with Shiny in R