Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R
Dean Attali
Shiny Consultant
x đổi, mọi thứ phụ thuộc x được tính lạiSo với R thông thường:
x <- 5
y <- x + 1
x <- 10
Giá trị của y là gì? 6 hay 11?
input$<inputId> trong hàm render sẽ khiến output render lại
output$my_plot <- renderPlot({
plot(rnorm( input$num ))
})
output$my_plot phụ thuộc input$num
input$num đổi ⇒
output$my_plot phản ứng
render*() nào là ngữ cảnh phản ứngserver <- function(input, output) {
print(input$num)
}
ERROR: Operation not allowed without an active reactive context.
observe({ ... }) để truy cập biến phản ứng
server <- function(input, output) {
observe({
print( input$num )
})
}
Hữu ích để debug, theo dõi biến phản ứng
observe({
print( input$num1 )
print( input$num2 )
})
reactive({ ... }) để tạo biến phản ứng
Sai:
server <- function(input, output) {
x <- input$num + 1
}
ERROR: Operation not allowed without an active reactive context.
Đúng:
server <- function(input, output) {
x <- reactive({
input$num + 1
})
}
()server <- function(input, output){
x <- reactive({
input$num + 1
})
observe({
print( input$num )
print( x() )
})
}
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R