Câu hỏi này có liên quan đến this one. Cả hai có thể tạo ra cùng một chức năng, nhưng việc triển khai hơi khác một chút. Một khác biệt đáng kể là reactiveValue
là một vùng chứa có thể có nhiều giá trị, như input$
. Trong chức năng shiny documentation thường được triển khai bằng cách sử dụng reactive()
, nhưng trong hầu hết các trường hợp, tôi thấy reactiveValues()
thuận tiện hơn. Có bắt ở đây không? Có sự khác biệt lớn nào khác giữa hai điều mà tôi có thể không nhận thức được không? Hai đoạn mã này có tương đương nhau không?R Sáng bóng: reactiveValues vs reactive
Xem cùng example code triển khai sử dụng:
một biểu hiện phản ứng:
library(shiny) ui <- fluidPage( shiny::numericInput(inputId = 'n',label = 'n',value = 2), shiny::textOutput('nthValue'), shiny::textOutput('nthValueInv') ) fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2)) server<-shinyServer(function(input, output, session) { currentFib <- reactive({ fib(as.numeric(input$n)) }) output$nthValue <- renderText({ currentFib() }) output$nthValueInv <- renderText({ 1/currentFib() }) }) shinyApp(ui = ui, server = server)
một giá trị phản ứng:
library(shiny) ui <- fluidPage( shiny::numericInput(inputId = 'n',label = 'n',value = 2), shiny::textOutput('nthValue'), shiny::textOutput('nthValueInv') ) fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2)) server<-shinyServer(function(input, output, session) { myReactives <- reactiveValues() observe( myReactives$currentFib <- fib(as.numeric(input$n)) ) output$nthValue <- renderText({ myReactives$currentFib }) output$nthValueInv <- renderText({ 1/myReactives$currentFib }) }) shinyApp(ui = ui, server = server)
Khi bạn hoàn tất, hãy đặt chủ đề này thành tài liệu tham khảo http://stackoverflow.com/documentation/shiny/topics –