【发布时间】: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 我已更新帖子以包含相关信息。