【问题标题】:Data frame won't update using observeEvent in Shiny R数据框不会在 Shiny R 中使用 observeEvent 更新
【发布时间】:2021-02-13 09:19:21
【问题描述】:

我是一个新手程序员,如果我不清楚或缺少相关信息,请原谅。我编写了一个闪亮的应用程序,它从另一组代码中导入数据帧。我想使用应用程序中的用户输入来更新该数据框。我已经使用下面的代码将数据帧作为反应变量上传了:

数据

current.shiny <- data.frame("Task" = as.character(c("Task 1", "Task 2", "Task 3")), "Completed" = as.character(c("Yes", "NO", "Yes")),"Date.Completed" = as.Date(c("2020-10-19","2020-10-20", "2020-10-21")))

用户界面

ui<- fluidPage(
  
  # Application title
  titlePanel("Week of 11.02.2020"),
  
  # Sidebar with reactive inputs
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "task.choice", label = "Task",
                  choices =  c(as.list(current.shiny$Task))),
      selectInput(inputId = "completed", label = "Completed?",
                  choices = c("Yes" = "Yes", "No" = "No")),
      dateInput(inputId = "date.completed", label ="Date Completed"),
      actionButton("update","Update Sheet")
      
    ),
    
    # a table of reactive outputs
    mainPanel(
      mainPanel(
        
        #DT::dataTableOutput("dt_table", width = 500)
        
      )
    )
  ),
  # column(12,
  #        DT::dataTableOutput("data", width = "100%")),
  column(12,
         DT::dataTableOutput("xchange", width = "100%"))
)

服务器 1


server <- function(input, output) {
# # Re-read data for any changes, write to csv new changes, ignore startup
 observeEvent(input$update,{
   test.data<-current.shiny
   test.data$Completed[test.data$Task == input$task.choice]<-as.character(input$completed)
   ignoreInit=T
 })

 # #Reactive variable xchange that updates the values of data
 xchange<-reactive({
   test.data<-current.shiny
   test.data$Completed[test.data$Task == input$task.choice]<-as.character(input$completed)
   test.data$Date.Completed[test.data$Task == input$task.choice]<-as.Date(input$date.completed)
   test.data
 })

 #Display the most recent file, with the most recent changes
 output$xchange <- renderDataTable({
   datatable(xchange(), options = list(dom = "t"))
 })
}
shinyApp(ui,server)

这在一定程度上有效。但是,它反应过度,因为我需要它仅在按下按钮后更新表格。上面代码中的 observeEvent 函数似乎没有做任何事情(它主要是从另一个堆栈溢出线程复制/粘贴的)。我尝试在下面进行设置,但无法运行。

服务器 2

server <- function(input, output, session) {
  rxframe <- reactiveVal(
    as.data.frame(current.shiny)
  )
  observeEvent(input$update, {
    rxframe$Completed[rxframe$Task == input$task.choice]<-as.character(input$completed)
  })
  output$xchange <- shiny::renderTable( rxframe() )
}

shinyApp(ui, server)

任何人都可以看到我错误地调用 observeEvent 导致它无法正常运行的某种方式吗?任何见解将不胜感激,我已经坚持了一段时间。

【问题讨论】:

  • 请向我们提供一个最小的应用程序(用户界面/服务器/示例数据),以便我们重现问题。
  • @ismirsehregal 我已更新帖子以包含相关信息。

标签: r shiny reactive


【解决方案1】:

因为您使用的是reactive,所以您的代码会直接对每个更改做出反应。

如果您想延迟反应,您可以使用observeEvent 以及reactiveValueseventReactive

这是一个使用reactiveValobserveEvent 的示例:

library(shiny)
library(DT)

current.shiny <- data.frame(
    "Task" = as.character(c("Task 1", "Task 2", "Task 3")),
    "Completed" = as.character(c("Yes", "NO", "Yes")),
    "Date.Completed" = as.Date(c("2020-10-19", "2020-10-20", "2020-10-21"))
  )

ui <- fluidPage(
  # Application title
  titlePanel("Week of 11.02.2020"),
  
  # Sidebar with reactive inputs
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "task.choice",
        label = "Task",
        choices =  c(as.list(current.shiny$Task))
      ),
      selectInput(
        inputId = "completed",
        label = "Completed?",
        choices = c("Yes" = "Yes", "No" = "No")
      ),
      dateInput(inputId = "date.completed", label = "Date Completed"),
      actionButton("update", "Update Sheet")
    ),
    mainPanel(column(
      12,
      DT::dataTableOutput("xchangeOut", width = "100%")
    ))
  ))

server <- function(input, output) {
  xchange <- reactiveVal(current.shiny)
  observeEvent(input$update, {
    test.data <- xchange()
    test.data$Completed[test.data$Task == input$task.choice] <-input$completed
    test.data$Date.Completed[test.data$Task == input$task.choice] <- input$date.completed
    xchange(test.data)
    # write.csv
  })
  
  #Display the most recent file, with the most recent changes
  output$xchangeOut <- renderDataTable({
    datatable(xchange(), options = list(dom = "t"))
  })
}

shinyApp(ui, server)

【讨论】:

  • 只是我个人的意见:在这种情况下我更喜欢使用eventReactive,因为这样你就可以直接推断出预期的行为(因为这是专门针对这种情况制作的);但是使用observeEvent/reactiveValues 也可以正常工作(对于更复杂的架构,您可能需要reactiveValues
  • @starja 你是对的,一般来说应该使用reactive / eventReactive 而不是observe / observeEvent(未来的读者可能会找到一些关于此here 的信息)。然而,在这种特殊情况下,我不认为使用eventReactive 非常方便,因为我们正在处理自引用反应式,与reactiveValues 相比,它导致我们无限递归。但是,也许我遗漏了一些东西,所以请随时举个例子来告诉我你是如何解决这个问题的。干杯
  • 嗨,你能帮我看看这个吗? stackoverflow.com/questions/71301712/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2017-11-06
  • 2022-01-04
  • 1970-01-01
  • 2019-06-04
  • 2022-01-19
  • 2021-11-26
相关资源
最近更新 更多