Case Studies: створення вебзастосунків із Shiny в R
Dean Attali
Shiny Consultant
x, усе, що залежить від x, обчислюється повторноНа відміну від звичайного R:
x <- 5
y <- x + 1
x <- 10
Яке значення має y? 6 чи 11?
input$<inputId> усередині функції render призведе до повторного рендерингу виходу
output$my_plot <- renderPlot({
plot(rnorm( input$num ))
})
output$my_plot залежить від input$num
input$num змінюється ⇒
output$my_plot реагує
render*() — реактивний контекстserver <- function(input, output) {
print(input$num)
}
ERROR: Operation not allowed without an active reactive context.
observe({ ... }) для доступу до реактивної змінної
server <- function(input, output) {
observe({
print( input$num )
})
}
Корисно для зневадження, відстежуйте реактивну змінну
observe({
print( input$num1 )
print( input$num2 )
})
reactive({ ... }) для створення реактивної змінної
Неправильно:
server <- function(input, output) {
x <- input$num + 1
}
ERROR: Operation not allowed without an active reactive context.
Правильно:
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: створення вебзастосунків із Shiny в R