Casestudies: webapplicaties bouwen met Shiny in R
Dean Attali
Shiny Consultant
x verandert, wordt alles dat x gebruikt herberekendIn tegenstelling tot regulier R:
x <- 5
y <- x + 1
x <- 10
Wat is de waarde van y? 6 of 11?
input$<inputId> binnen een render-functie triggert her-renderen
output$my_plot <- renderPlot({
plot(rnorm( input$num ))
})
output$my_plot hangt af van input$num
input$num verandert ⇒
output$my_plot reageert
render*()-functie is een reactieve contextserver <- function(input, output) {
print(input$num)
}
ERROR: Operation not allowed without an active reactive context.
Gebruik observe({ ... }) om een reactieve variabele te benaderen
server <- function(input, output) {
observe({
print( input$num )
})
}
Handig voor debuggen; volg een reactieve variabele
observe({
print( input$num1 )
print( input$num2 )
})
reactive({ ... }) om een reactieve variabele te maken
Fout:
server <- function(input, output) {
x <- input$num + 1
}
ERROR: Operation not allowed without an active reactive context.
Goed:
server <- function(input, output) {
x <- reactive({
input$num + 1
})
}
() achterserver <- function(input, output){
x <- reactive({
input$num + 1
})
observe({
print( input$num )
print( x() )
})
}
Casestudies: webapplicaties bouwen met Shiny in R