【发布时间】:2024-04-13 05:00:02
【问题描述】:
我正在构建一个闪亮的应用程序,并使用此问题中的代码作为示例:How to download editable data table in shiny。但是,在我的代码中 df <- reactiveVal(dat) 不起作用,因为 dat 本身已经是来自 eventReactive({}) 函数的反应值。这是我正在使用的代码,如果我在服务器外部定义dat,它就可以工作,但在闪亮的服务器函数内部创建它时就不行。如何复制它以便在新表格中显示它(并可能进一步处理并在应用程序的后续步骤中下载)?
library(shiny)
library(DT)
library(shinyWidgets)
# if the data frame is just an object, it works
#dat <- iris[1:3, ]
ui <- fluidPage( actionBttn(
inputId = "btnProcess",
label = "Process",
size = "sm",
color = "success"
),
DTOutput("my_table"),
DTOutput("table2")
)
server <- function(input, output){
# if the dataframe is a reactive variable, this doesnt work.
dat <- eventReactive(input$btnProcess, {
iris[1:3, ]
})
output[["my_table"]] <- renderDT({
datatable(dat(), editable = "cell")
})
#############################
#### none of these work #####
#############################
#df <- reactiveVal(dat)
#df <- reactiveVal(dat())
#df <- dat()
#df <- dat
observeEvent(input[["my_table_cell_edit"]], {
cell <- input[["my_table_cell_edit"]]
newdf <- df()
newdf[cell$row, cell$col] <- cell$value
df(newdf)
})
output[["table2"]] <- renderDT({
datatable(df())
})
}
shinyApp(ui, server)
【问题讨论】: