r语言 - 使用DataTable(服务器端)从数据帧中删除行



我有一个闪亮的应用程序,我在多个。csv文件中读取(本例中为虚拟。csv),并将它们存储为可以通过下拉选项卡选择的数据帧列表。当使用下拉选项卡选择文件时,将显示一个使用renderDT的表,并显示使用该表中的值的散点图。您可以使用在您的目录中创建的虚拟csv来尝试。

library(DT)
library(shiny)
library(data.table)
# create dummy CSVs -------------------------------------------------------
DF1 <- data.frame(x = 1:3, y = 2:4)
DF2 <- data.frame(x = 4:2, y = 5:7)
DF3 <- data.frame(x = 7:9, y = 8:10)
DF4 <- data.frame(x = 10:8, y = 11:13)
mapply(
write.csv,
x = list(DF1, DF2, DF3, DF4),
file = list("DF1.csv", "DF2.csv", "DF3.csv", "DF4.csv"),
row.names = FALSE
)
# shiny app ---------------------------------------------------------------
ui <- fluidPage(sidebarLayout(
sidebarPanel(
fileInput(
"files",
"Choose File",
multiple = TRUE,
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".dp_txt",
".is_txt"
)
),

selectizeInput(
inputId = "selected_table",
label = "Table Selection",
choices = NULL,
selected = NULL,
multiple = FALSE
),


),
mainPanel(DTOutput("table"), plotOutput("plot"))
))
server <- function(input, output, session) {
observeEvent(input$files, {
freezeReactiveValue(input, "selected_table")
updateSelectizeInput(session,
inputId = "selected_table",
choices = input$files$name,
server = TRUE)
})


table_list <- reactive({
req(input$files)
setNames(lapply(input$files$datapath, function(x) {
fread(x)
}),
input$files$name)
})



output$table <- renderDT({
req(table_list(), input$selected_table)
table_list()[[input$selected_table]]
}, server = FALSE)


output$plot <- renderPlot({
temp <- table_list()[[input$selected_table]]
plot(temp$x, temp$y)

})
}
shinyApp(ui, server)

如何删除单个数据帧的数据行和绘图上相应的点?基本上,当删除一行时,我希望在服务器端删除该行,以便依赖于原始表的任何其他图或表也被更新。最后,我希望这个删除是永久的,这样当切换到不同的数据帧时,删除的行/点不会在返回到该数据帧时回来。我该怎么做呢?

这似乎是我之前的回答的后续。

一旦你需要在多个地方修改reactive,我们需要使用reactiveValreactiveValues

请检查observeEvent(input$deleterow, ...呼叫:

library(DT)
library(shiny)
library(data.table)
# create dummy CSVs -------------------------------------------------------
DF1 <- data.frame(x = 1:3, y = 2:4)
DF2 <- data.frame(x = 4:2, y = 5:7)
DF3 <- data.frame(x = 7:9, y = 8:10)
DF4 <- data.frame(x = 10:8, y = 11:13)
mapply(
write.csv,
x = list(DF1, DF2, DF3, DF4),
file = list("DF1.csv", "DF2.csv", "DF3.csv", "DF4.csv"),
row.names = FALSE
)
# shiny app ---------------------------------------------------------------
ui <- fluidPage(sidebarLayout(
sidebarPanel(
fileInput(
"files",
"Choose File",
multiple = TRUE,
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".dp_txt",
".is_txt"
)
),
selectizeInput(
inputId = "selected_table",
label = "Table Selection",
choices = NULL,
selected = NULL,
multiple = FALSE
),
),
mainPanel(DTOutput("table"),
actionButton("deleterow", "Delete row(s)"),
plotOutput("plot"))
))
server <- function(input, output, session) {
observeEvent(input$files, {
freezeReactiveValue(input, "selected_table")
updateSelectizeInput(session,
inputId = "selected_table",
choices = input$files$name,
server = TRUE)
})

table_list <- reactiveVal()

observeEvent(input$files, {
req(input$files)
table_list(setNames(lapply(input$files$datapath, function(x) {
fread(x)
}),
input$files$name))
})

observeEvent(input$deleterow, {
if(!is.null(input$table_rows_selected)){
tmp_table_list <- table_list()
tmp_table_list[[input$selected_table]] <- table_list()[[input$selected_table]][-input$table_rows_selected]
table_list(tmp_table_list)
}
})

output$table <- renderDT({
req(table_list(), input$selected_table)
table_list()[[input$selected_table]]
}, server = FALSE)

output$plot <- renderPlot({
temp <- table_list()[[input$selected_table]]
req(NROW(temp) > 0)
plot(temp$x, temp$y)
})
}
shinyApp(ui, server)

最新更新