【发布时间】:2020-12-29 05:59:11
【问题描述】:
我有一个闪亮的应用程序,用户可以在其中上传自己的数据。我的目标是显示一个带有 DT 的交互式表格,允许用户控制显示哪些列和哪些行。最终,用户应该能够下载他们上传的所有数据(在实际应用程序中完成了一些处理步骤)或仅下载他们在当前选择中看到的数据。因此,我需要复制上传的数据框,而不是就地编辑它。
我的问题是我可以使列可选,也可以删除选定的行,但我找不到在两者之间保存选定行的方法。例如:当用户首先选择第 1,2 和 3 行并点击“排除行”时,这些行会消失,但是当他们随后点击第 4 和 5 行并点击“排除行”时,第 4 和 5 行会消失 但 1,2 和 3 会弹回。
这是我目前尝试过的:
# Reproducible example
# Define UI
ui <- fluidPage(
navbarPage("Navbar",
tabPanel("Upload Data",
fileInput(inputId = "file", label = "Upload your .csv file",
accept = "text/csv"),
actionButton("submit","Use this dataset")
),
tabPanel("Check Table",
sidebarPanel("Settings",
checkboxGroupInput("show_vars", "Select Columns to display:",
choices = c("type",
"mpg",
"cyl",
"disp",
"hp",
"drat",
"wt",
"qsec",
"vs",
"am",
"gear",
"carb"
),
selected = c("type",
"mpg",
"cyl",
"disp",
"hp",
"drat",
"wt",
"qsec",
"vs",
"am",
"gear",
"carb"
)),
tags$br(),
tags$br(),
actionButton("excludeRows", "Exlcude selected Rows")),
mainPanel(DTOutput("frame"))),
tabPanel("Show Selection",
textOutput("selection"))
)
)
# Define server logic
server <- function(input, output, session) {
# Parsing the uploaded Dataframe according to the right input
data <- eventReactive(input$submit, {read.csv(input$file$datapath)})
# Render the whole dataframe when a new one is uploaded
observeEvent(input$submit, {output$frame <- renderDT(datatable(data()[,c(input$show_vars)]))})
# Making an internal copy for selection purposes
CopyFrame <- eventReactive(data(),{data()})
# excluding selected rows
observeEvent(input$excludeRows,{
if (exists("SelectFrame()")) {
# Updating SelectFrame from SelectFrame
SelectFrame <- eventReactive(input$excludeRows,{SelectFrame()[-c(input$frame_rows_selected),c(input$show_vars)]})
} else {
# creating SelectFrame for the first time from CopyFrame
SelectFrame <- eventReactive(input$excludeRows,{CopyFrame()[-c(input$frame_rows_selected),c(input$show_vars)]})
}
# updating plot
output$frame <- renderDT(datatable(SelectFrame()))
})
# show Selection
output$selection <- renderText(input$frame_rows_selected)
}
# Run the application
shinyApp(ui = ui, server = server)
您可以轻松地为这个可重现的示例创建一个示例文件:
names(mtcars)[1] <- "type"
write.csv(mtcars, file = "testfile.csv")
【问题讨论】: