Shiny 如何使用reactiveValues保存以前的交互

Shiny 如何使用reactiveValues保存以前的交互,shiny,Shiny,我正在开发一个表格,每次使用动作按钮时都会进行一些计算。表中的一列取决于其先前的值 C_new <- C_old + B_new - A_new 如中所述,我尝试使用reactiveValue保存C列的上一个值,但它不起作用。我不知道我错在哪里 这是我的密码 library(shiny) ui <- fluidPage( sidebarPanel(textInput("c1","Example"), actionButton("update", "Upda

我正在开发一个表格,每次使用动作按钮时都会进行一些计算。表中的一列取决于其先前的值

C_new <- C_old + B_new - A_new
如中所述,我尝试使用reactiveValue保存C列的上一个值,但它不起作用。我不知道我错在哪里

这是我的密码

library(shiny)
ui <- fluidPage(
  sidebarPanel(textInput("c1","Example"),
           actionButton("update", "Update Table")),
  mainPanel(tableOutput("example"))
)

server <- function(input, output) {

  C_old <- reactive(x=3)

  values <- reactiveValues(df = data.frame(A=1, B=2, C=3)) 

  newEntry <- observeEvent(input$update,{

    A_new <- as.numeric(input$c1)
    B_new <- A_new + 2
    C_new <- isolate (C_old$x + B_new - A_new) 
    C_old$x <<- C_new

  new <- data.frame(A=A_new,B=B_new, C=C_new)

   # attach the new line to the old data frame here:
   values$df <- rbind(values$df, new)

  })

  # Print the content of values$df
  output$example <- renderTable({
   return(values$df)
  })

}

shinyApp(ui = ui, server = server)
重要的是要知道,与观察者类似的观察者事件没有输出。你只是观察一个变化,在他们的身体里做一些事情,但是不应该返回任何东西。这与反应性{}不同,反应性{}也观察变化,但有返回值,很有用

observeEvent中不需要隔离,因为除了输入$update之外,没有任何东西会触发更新。这与观察和反应不同,主体中的所有可变项都会触发更新

下面是您问题的解决方案。我使用了存储一个可更新值的reactiveVal。有关帮助,请参阅?reactiveVal。单击操作按钮后,我通过调用值检索旧表,计算所有新值。我需要使用tail仅获取最后一个C值,并将结果附加到旧值,然后通过调用valuesnew_df将扩展表存储为值:


是的,你是对的。根据你的建议,我改变了我的例子。谢谢,哇!我不知道reactiveVal的事,谢谢。我将看看是否可以将其实现到原始表中。如果我想要C=C-2+B-A而不是C=C-1+B-A,你知道我可以用什么函数吗?最后一件事,你能帮我提高我的声誉吗?如果我没有至少15分,我就不能投票。C的最后两个值是tailC,2,这些值的第一个条目是head…,1,所以最后第二个是headtailC,2,1
library(shiny)
ui <- fluidPage(
  sidebarPanel(textInput("c1","Example"),
           actionButton("update", "Update Table")),
  mainPanel(tableOutput("example"))
)

server <- function(input, output) {

  C_old <- reactive(x=3)

  values <- reactiveValues(df = data.frame(A=1, B=2, C=3)) 

  newEntry <- observeEvent(input$update,{

    A_new <- as.numeric(input$c1)
    B_new <- A_new + 2
    C_new <- isolate (C_old$x + B_new - A_new) 
    C_old$x <<- C_new

  new <- data.frame(A=A_new,B=B_new, C=C_new)

   # attach the new line to the old data frame here:
   values$df <- rbind(values$df, new)

  })

  # Print the content of values$df
  output$example <- renderTable({
   return(values$df)
  })

}

shinyApp(ui = ui, server = server)
library(shiny)
ui <- fluidPage(
  sidebarPanel(numericInput("c1","Example", 0),
               actionButton("update", "Update Table")),
  mainPanel(tableOutput("example"))
)

server <- function(input, output) {

  # stores the current data frame, called by values() and set by values(new_data_table)
  values <- reactiveVal(data.frame(A=1, B=2, C=3))

  # update values table on button click
  observeEvent(input$update,{

    old_values <- values()

    A_new <- input$c1
    B_new <- A_new + 2
    C_new <- tail(old_values$C, 1) + B_new - A_new  # tail to get the last C value

    new_values <- data.frame(A=A_new, B=B_new, C=C_new)

    # attach the new line to the old data frame here:
    new_df <- rbind(old_values, new_values)

    #store the result in values variable
    values(new_df)

  })

  # Print the content of values$df
  output$example <- renderTable({
    return(values())
  })

}

shinyApp(ui = ui, server = server)