R闪亮:删除从.rds文件读取的列表条目

时间:2018-09-01 11:24:40

标签: r shiny rds

我正在尝试构建一个可上传csv文件的应用程序,将其另存为数据集,然后可以从其中删除,如下所示:

首先,当我上传文件时,会在其中创建一个rds文件,该文件将以以下格式保存:file,header,sep,index,并为每个人保存一个值(file中文件的名称,true /标头中的false等)

server <- function(input, output, session) {
  # Copy uploaded files to local folder
  observeEvent(input$uploadId, {
    req(input$file1)
    file.copy(
      from = input$file1$datapath,
      to = paste0('Selected_Files/', input$file1$name)
    )
    df <- list(
      file = input$file1$name ,
      header = input$header,
      sep = input$sep,
      dec = input$dec,
      quote = input$quote,
      index = input$uploadId
    )
    if (input$uploadId > 1) {
      old_df <- readRDS("File_Format.rds")
      df <-
        sapply(names(old_df), function(n) {
          c(old_df[[n]], df[[n]])
        }, simplify = FALSE)
    }
    saveRDS(df, "File_Format.rds")
  })

  # Load all the uploaded files to a list
  datasetlist <- eventReactive(input$uploadId, {
    File_Format <- readRDS("File_Format.rds")
    datalist <- list()
    datalist <-
      lapply(1:length(File_Format[[1]]), function(d)
        read.csv(
          paste0("Selected_Files/", File_Format$file[d]),
          header = File_Format$header[d],
          sep = File_Format$sep[d],
          dec = File_Format$dec[d],
          quote = File_Format$quote[d]
        ))
    names(datalist) <-
      paste(File_Format$index, File_Format$file, sep = ".")


    return(datalist)
  })

现在要删除它们:我有一个选择菜单,其中出现文件名,并且这里有一个删除按钮,因为我似乎不知道如何删除数据集,所以它变得很复杂

observeEvent(input$deleteid , {
    old_df <- readRDS("File_Format.rds")
    df <- old_df[-which(input$remove %in% old_df$file)]
    saveRDS(df, "File_Format.rds")
  })
  #Delete Selected File
  datasetlist2 <- eventReactive(input$deleteid, {
    data <- datasetlist()
    deleteDataset <- input$remove
    datalist <-data[!input$remove %in% deleteDataset]
    return(datalist)
    names(datalist) <-
      paste(File_Format$index, File_Format$file, sep = ".")
  })
  observeEvent(input$deleteid, {
    data <- datasetlist2()

    updateSelectInput(session,
                      "dataset",
                      choices = names(data),
                      selected =  names(data))
    updateSelectInput(session,
                      "remove",
                      choices = names(data),
                      selected = names(data))


  })

0 个答案:

没有答案