【问题标题】:Capturing instance of reactive values?捕获反应值的实例?
【发布时间】:2016-09-17 05:31:44
【问题描述】:

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

所以在下面的代码中,用户将他们的值从 rhandsontable 包中输入到一个表中(顺便说一句,这很棒),我要做的就是将它转换为一个基本数据框,即 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)

【问题讨论】:

  • 如果您能提供视觉效果,那就太好了。我会很感激的。
  • 谢谢@InfiniteFlashChess 我已经附上了我的脚本,它运行没有错误只是没有做我想要的!
  • 我想要做的就是通过单击按钮将反应值复制到普通的非反应数据框 tabplot 一次,然后能够像我习惯的任何其他数据框一样使用 tabplot。请让我知道这是否可能,对我来说,这似乎是一个非常必要的功能。 isolation 功能提供的功能不完全是吗?
  • @InfiniteFlashChess 当我将output$kl&lt;-renderTable({ tabplot }) 更改为output$kl&lt;-renderTable({ tabplot$car }) 时,我得到没有适用于“xtable”的方法应用于类“c('double','numeric')”的对象 能够像这样访问 tabplot 确实是我想要实现的目标。
  • 我假设你想访问一个特定的变量,所以不要使用tabplot$car(它是一个向量),而是使用tabplot["car"](它仍然是一个data.frame)。还是你真的需要tabplot$car

标签: r shiny rhandsontable


【解决方案1】:

这可能是你想要的。它利用了备受鄙视的&lt;&lt;- 运算符,但当我需要颠覆闪亮的“惰性反应”架构时,我会这样做。

请注意,我设置了一个并行数据框 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)

产量:

【讨论】:

  • 从一个迈克到另一个迈克,感谢您的帮助。您能否给我更多关于
  • 还有 Mike,我想对 tabplot1 中的值进行操作。我在哪里可以做这些,它总是必须在反应输出函数中吗?因此,为什么我希望 tabplot1 没有反应。请注意,我对闪亮的反应值的理解可能存在根本缺陷。
  • 据我现在所知,我认为不可能做到以下几点:static_truck&lt;-tabplot$truck where static_truck = 5,66 并且当 tabplot 中的值改变时不会改变。
  • 好的,所以我对反应值的所有操作都可以进入反应函数内部,其中声明的值即 fn( )。如果有意义的话,让整个应用程序响应式似乎有点烦人。
  • 嘿,现在才看到这个。很多问题。要了解 adv-r.had.co.nz/Environments.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 2013-11-29
相关资源
最近更新 更多