如何在列表内的数据框中保存输入?

How to save the inputs, inside Data Frame which is inside the list?

我试图让 reprex 尽可能简单。

我想用 ADD 按钮保存当前选择的输入,在数据框内 (由 userId 输入传递的索引选择),它在列表中,稍后使用此数据框呈现 table(在最终应用程序中绘制)。

在这里我想通了,如何在数据框中保存值。 (不是列表中的数据框)
How to save input to data frame, and use it later in Shiny?

现在添加按钮returns这个:
警告:choosen_user 中的错误:未使用的参数 (rbind(choosen_user(), new_day_rate())) <- 这可能是因为我使用了reactive() 不是 reactiveVal() 但是 reactiveVal() 有这个错误:

警告:.getReactiveEnvironment()$currentContext 中出错:没有活动的反应性上下文则不允许操作。 您试图做一些只能从反应性消费者内部完成的事情。

library(shiny)

# Saved_users_list normally came from external file
saved_users_list <- list(data.frame(date = c(as.Date("2022-04-18"),
                                       as.Date("2022-04-19")),
                              rate = c(8,1),
                              day_comment = c("Found a gf",
                                              "Broke my arm")),
                   data.frame(date = c(as.Date("2022-04-18"),
                                       as.Date("2022-04-19")),
                              rate = c(10,1),
                              day_comment = c("Found a job",
                                              "They fired me")))


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userId", "userId", choices = c(1:2)),
      sliderInput("day_rate", "Rate your day", min = 0, max = 10, value = 5, step = 0.5),
      dateInput("date", "Pick a date"),
      textAreaInput("comment", "Comment", placeholder = "Add a description (OPTIONAL)"),
      actionButton("add", "Add"),
      actionButton("test", "Test values") # Button to test inputs values
      ),
    mainPanel(
      tableOutput("test_table")
    )
    )
  )

server <- function(input, output, session) {
  
  users_list <- reactiveVal(saved_users_list)
  selected_user <- reactive(as.numeric(input$userId))
  
  output$test_table <- renderTable({
    users_list()[selected_user()]
  })
  
    new_day_rate <- reactive(list(data.frame(date = input$date,
                                             rate = input$day_rate,
                                             day_comment = input$comment)))
    
    choosen_user <- reactive(users_list()[[selected_user()]])
    
    # Button to add values to the data frame inside users_list
    observeEvent(input$add, {
    # users_list()[[selected_user()]]  <- rbind(users_list()[[selected_user()]], as.data.frame(new_day_rate())) # Error in <-: invalid (NULL) left side of assignment

    choosen_user(rbind(choosen_user(), new_day_rate())) # Here I tried to implement a solution from linked question 
  })
  
  # Button to test inputs values
  observeEvent(input$test, {
    message("userId: ", input$userId, " ", class(input$userId))
    message("selected_user(): ", selected_user())
    message("new_day_rate(): ", new_day_rate())
    message("str(new_day_rate()): ", str(new_day_rate()))
    message("users_list()[[selected_user()]]: ",users_list()[[selected_user()]])
  })
}

shinyApp(ui, server)

我想你是在追求 reactiveValues?类似于:

library(shiny)

# Saved_users_list normally came from external file
saved_users_list <- list(
  data.frame(
    date = c(as.Date("2022-04-18"), as.Date("2022-04-19")),
    rate = c(8,1),
    day_comment = c("Found a gf", "Broke my arm")
  ),
  data.frame(
    date = c(as.Date("2022-04-18"), as.Date("2022-04-19")),
    rate = c(10,1),
    day_comment = c("Found a job", "They fired me")
  )
)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userId", "userId", choices = c(1:2)),
      sliderInput("day_rate", "Rate your day", min = 0, max = 10, value = 5, step = 0.5),
      dateInput("date", "Pick a date"),
      textAreaInput("comment", "Comment", placeholder = "Add a description (OPTIONAL)"),
      actionButton("add", "Add"),
      actionButton("test", "Test values") # Button to test inputs values
    ),
    mainPanel(
      tableOutput("test_table")
    )
  )
)

server <- function(input, output, session) {
  cache <- reactiveValues(saved_users = saved_users_list)
  selected_user <- reactive(as.numeric(input$userId))

  output$test_table <- renderTable({
    cache$saved_users[selected_user()]
  })

  new_day_rate <- reactive(
    data.frame(
      date = as.Date(input$date),
      rate = input$day_rate,
      day_comment = input$comment
    )
  )

  observeEvent(input$add, {
    cache$saved_users[[selected_user()]] <- rbind(
      cache$saved_users[[selected_user()]], new_day_rate()
    )
  })

  observeEvent(input$test, {
    message("userId: ", input$userId, " ", class(input$userId))
    message("selected_user(): ", selected_user())
    message("new_day_rate(): ", new_day_rate())
    message("str(new_day_rate()): ", str(new_day_rate()))
    message("users_list()[[selected_user()]]: ", cache$saved_users[[selected_user()]])
  })
}

shinyApp(ui, server)