我希望我在R中的shiny 应用程序读取Excel文件,添加空行,用户可以在其中添加文本输入,单击按钮后,它会使用添加的输入更新表,表顶部的一行将再次为空,等待用户输入.我已经设法使它工作的第一次,例如,我可以添加输入,点击按钮,表更新和第一行是等待另一个输入.但是,当我再次添加新的输入时,表格将使用我第一次输入的相同文本进行更新. 看起来问题出在我更新ReactiveEvent()中的reactiveValues的那一行,因为当我删除那一行时,print语句会打印正确的输入.当我保留这一行时,只有第一个输入总是被打印出来.
非常感谢您的帮助!
library(shiny)
library(plyr)
library(dplyr)
library(DT)
library(xlsx)
library(lubridate)
ui <- fluidPage(fluidRow(column(4, offset = 0, actionButton("add_button", "Add", width = 200))),
br(),
DT::dataTableOutput("outtable")
)
server <- function(input, output) {
excel_file <- data.frame(Month = c("2024-01", "2023-12"),
Date = c("2024-01-10", "2023-12-15"),
Reviewer = c("A", "B"),
Status = c("OK", "OK"),
Comment = c("bla", "blabla"), stringsAsFactors = FALSE)
excel_file <- excel_file %>%
arrange(desc(Month))
df_input <- data.frame(
Month = as.character(format(ym(max(excel_file$Month)) %m+% months(1), "%Y-%m")),
Date = as.character(textInput("date", label = NULL)),
Reviewer = as.character(textInput("reviewer", label = NULL)),
Status = as.character(textInput("status", label = NULL)),
Comment = as.character(textInput("comment", label = NULL)))
RV <- reactiveValues(dat = bind_rows(df_input, excel_file))
observeEvent(
eventExpr = c(input$add_button),
handlerExpr = {
new_line <- data.frame(
Month = as.character(format(ym(max(RV$dat$Month)), "%Y-%m")),
Date = input$date,
Reviewer = input$reviewer,
Status = input$status,
Comment = input$comment)
print(new_line)
temp <- bind_rows(RV$dat, new_line) %>%
arrange(desc(Month))
RV$dat <- temp # THIS LINE
},ignoreNULL = FALSE , ignoreInit = TRUE
)
output$outtable <- DT::renderDataTable({
datatable(
RV$dat,
selection="none",
escape = FALSE,
options = list(
dom = 'rtpi',
autoWidth=TRUE,
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)
})
}
shinyApp(ui = ui, server = server)