捕获反应值的实例?

Capturing instance of reactive values?

在 R Shiny 中,是否有一种方法可以捕获反应值的特定实例,以便该实例完全不反应? 所以我有一个 table 由反应值组成,当用户点击提交按钮时,这些值被复制到一个非反应性 table 然后我可以继续操作等

所以在下面的代码中,用户将他们的值输入到来自 rhandsontable 包的 table 中(顺便说一句,这很棒),而我想要做的就是将其转换为基本数据框 tabplot 应该是非反应性的,所以我可以继续对它进行任何类型的操作。

library(shiny)
library(rhandsontable)

seq1 <- seq(1:6)
mat1 <- matrix(seq1, 2)

tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))

did_recalc <- FALSE

ui <- fluidPage(
  rHandsontableOutput('table'),
  tableOutput('result'),
  tableOutput('kl'),
  textOutput('ca'),
  actionButton("goButton","Confirm"),

  actionButton("checkButton","Apply"),
  br(),
  actionButton("recalc", "Return to original values")

)

server <- function(input,output,session)({

  tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))


  seq1 <- seq(1:6)
  mat1 <- matrix(seq1, 2)
  mat1<-data.frame(mat1)

  #creates reactive values for the data frame
  #obviously they have to be reactive values to function with the rhandsontable which is being continuously updated
  #as the documentation says "values taken from the reactiveValues object are reactive but the object itself is not
  values <- reactiveValues(data=mat1)

  #if recalc --- which connects to an action button in the ui is hit, values goes back to original data frame
  observe({
    input$recalc
    values$data<-mat1
  })

  #Where the magic happens
  output$table <- renderRHandsontable({
    rhandsontable(values$data,selectCallback = TRUE)
  })

  #this changes the handsontable format to an r object
  observe({
    if(!is.null(input$table))
      values$data <-hot_to_r(input$table)
  })

  #Here we create a reactive function that creates a data frame of the rhandsontable output but it is a reactive function
  fn<-reactive({
    co<-data.frame((values$data))                                
    return(co)
  })

  #Bit of testing, this demonstrates that the fn() is only updated after the button is pressed
  output$result<-renderTable({
    input$goButton
    isolate({
      fn()
    })
  })   

  isolate({
#  tabplot<-reactive({                              #Format co[desired row:length(colums)][desired column] 
    tabplot[1,1:3][1]<-fn()[1,1:3][1]
    tabplot[1,1:3][2]<-fn()[1,1:3][2]
    tabplot[1,1:3][3]<-fn()[1,1:3][3]

    tabplot[2,1:3][1]<-fn()[2,1:3][1]
    tabplot[2,1:3][2]<-fn()[2,1:3][2]
    tabplot[2,1:3][3]<-fn()[2,1:3][3]
  })

  output$kl<-renderTable({

    tabplot

  })  


  observe({
    input$goButton
    output$ca<-renderText({
      tabplot$car 
      cat('\nAccessing Subset with $:', tabplot$car)
      cat('\nAccessing specific cell:',tabplot[1,3])
      cat('\noperations on specific cell:',tabplot[1,3]*2)
    })
  })



})
shinyApp(ui = ui, server = server)

这可能就是您想要的。它利用了备受鄙视的 <<- 运算符,但当我需要颠覆闪亮的 "lazy reactive" 架构时,它就是我所做的。

请注意,我设置了一个并行数据框 tabplot1 并将其显示在您显示 tabplot 的下方。

library(shiny)
library(rhandsontable)

seq1 <- seq(1:6)
mat1 <- matrix(seq1, 2)

tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))

did_recalc <- FALSE

ui <- fluidPage(
  rHandsontableOutput('table'),
  tableOutput('result'),
  tableOutput('kl'),
  tableOutput('kl1'),
  textOutput('ca'),
  actionButton("goButton","Confirm"),

  actionButton("checkButton","Apply"),
  br(),
  actionButton("recalc", "Return to original values")

)

server <- function(input,output,session)({

  tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))
  tabplot1 <- tabplot


  seq1 <- seq(1:6)
  mat1 <- matrix(seq1, 2)
  mat1<-data.frame(mat1)

  #creates reactive values for the data frame
  #obviously they have to be reactive values to function with the rhandsontable which is being continuously updated
  #as the documentation says "values taken from the reactiveValues object are reactive but the object itself is not
  values <- reactiveValues(data=mat1)

  #if recalc --- which connects to an action button in the ui is hit, values goes back to original data frame
  observe({
    input$recalc
    values$data<-mat1
  })

  #Where the magic happens
  output$table <- renderRHandsontable({
    rhandsontable(values$data,selectCallback = TRUE)
  })

  #this changes the handsontable format to an r object
  observe({
    if(!is.null(input$table))
      values$data <-hot_to_r(input$table)
  })

  #Here we create a reactive function that creates a data frame of the rhandsontable output but it is a reactive function
  fn<-reactive({
    co<-data.frame((values$data))                                
    return(co)
  })

  #Bit of testing, this demonstrates that the fn() is only updated after the button is pressed
  output$result<-renderTable({
    input$goButton
    tabplot1 <<- data.frame(values$data)
    colnames(tabplot1) <<- colnames(tabplot)
    isolate({
      fn()
    })
  })   

  isolate({
    #  tabplot<-reactive({                              #Format co[desired row:length(colums)][desired column] 
    tabplot[1,1:3][1]<-fn()[1,1:3][1]
    tabplot[1,1:3][2]<-fn()[1,1:3][2]
    tabplot[1,1:3][3]<-fn()[1,1:3][3]

    tabplot[2,1:3][1]<-fn()[2,1:3][1]
    tabplot[2,1:3][2]<-fn()[2,1:3][2]
    tabplot[2,1:3][3]<-fn()[2,1:3][3]
  })

  output$kl<-renderTable({

    tabplot

  })  
  output$kl1<-renderTable({
    input$goButton
    tabplot1

  })  



  observe({
    input$goButton
    output$ca<-renderText({
      tabplot$car 
      cat('\nAccessing Subset with $:', tabplot$car)
      cat('\nAccessing specific cell:',tabplot[1,3])
      cat('\noperations on specific cell:',tabplot[1,3]*2)
    })
  })



})
shinyApp(ui = ui, server = server)

产量: