1

我希望能够上传数据集,选择一组列,转换选定的列(即应用函数),然后下载修改后的文件。我一直在尝试使用以下代码:

library(shiny)
library(DT)
library(shinyWidgets)
library(plyr)
library(dplyr)
library(RecordLinkage)
library(readxl)

cleanup <- function(x){
  x <- as.character(x) # convert to character
  x <- tolower(x) # make all lowercase
  x <- trimws(x, "both") # trim white space
  return(x)
}

ui <- fluidPage(
  h2("Record Linkage Data"),
  fileInput("file1", "Upload file for cleaning", accept = c("xls", "csv"), multiple = F),
  actionButton(inputId = "clean", label = "Clean Data"),
  downloadButton("download1", "Download file1"),
  pickerInput(width = "75%",
              inputId = "pick_col1",
              label = "Select columns to display",
              choices = colnames(file1),
              selected = colnames(file1),
              options = list(
                `actions-box` = T,
                `selected-text-format` = paste("count > ", length(colnames(file1)) - 1),
                `count-selected-text` = "Alle",
                liveSearch = T,
                liveSearchPlaceholder = T
              ),

              multiple = T),
  DT::dataTableOutput("mytable")
)

load_path <- function(path) {
    req(input$file)
    ext <- tools::file_ext(path)

    if (ext == "csv"){
      read.csv(path, header = T)
    } else if (ext == "xls" || ext == "xlsx"){
      read_excel(path)
    } else{
      stop("Unknown extension: '.", ext, "'")
    }
}

server <- function(input, output, session){

file1 <- reactive(load_path(input$selection$datapath[[1]]))

#file2 <- reactive(load_path(input$selection$datapath[[2]]))
  eventReactive(input$clean, {
    output$mytable <- DT::renderDataTable({
      data.frame(lapply(select(file1, input$pick_col1), cleanup))
    })


  })

  output$download <- downloadhandler(
    filename = function(){
      paste0(tools::file_path_sans_ext(input$filename), ".csv")

    },

    content = function(file){
      write.csv(data(), file)
    }
  )

}  

shinyApp(ui, server)

当我运行上面的代码时,我得到了错误: Error in is.data.frame(x) : object 'file1' not found。我不确定为什么会这样,但我一直在努力理解在shiny. 例如:我想上传file1,然后转换它。file1想下载的时候继续参考吗?这些可能看起来像愚蠢的问题,但我问是因为我不知道,我正在努力学习。似乎有很多不同的方法。

我想: 1. 加载文件 2. 选择列(pickerInput这是我一直在尝试的,但selectInput我想就足够了) 3. 通过操作按钮,将预先指定的功能应用于选定的列 4. 下载转换后的数据集作为 .csv

4

1 回答 1

4

我遇到了一些问题

  1. 这是一个非常愚蠢的问题(它发生在我们所有人身上)。你应该写downloadHandler而不是downloadhandler.
  2. file1主要问题:您的 pickerInput在不存在时尝试选择数据框的列名。当您运行应用程序时,代码会尝试查找file1数据框并查看其列名,但由于那时您尚未上传任何内容,因此会引发错误。
  3. 关于你如何阅读文件:我不熟悉你如何阅读文件,我建议你做一些类似于这个例子中所做的事情。https://shiny.rstudio.com/gallery/file-upload.html。请注意,在示例中,您需要使用一个read.*函数并将结果指向另一个名称。df

我将如何解决它: 1. 默认情况下将choicesselected选项设置为 NULL。像下面这样的东西应该可以工作:

  pickerInput(width = "75%",
              inputId = "pick_col1",
              label = "Select columns to display",
              choices = NULL,
              selected = NULL,
              options = list(
                `actions-box` = T,
                # `selected-text-format` = paste("count > ", length(colnames(file1)) - 1),
                `count-selected-text` = "Alle",
                liveSearch = T,
                liveSearchPlaceholder = T
              ),
              multiple = T)
  1. updatePickerInput在服务器端添加一个observeEvent. 像这样的东西应该工作。
observeEvent(input$file1, {

  req(input$file1) # ensure the value is available before proceeding

  df <- read.csv(input$file1$datapath)

  updatePickerInput(session = session, 
                    inputId = "pick_col1", 
                    choices = colnames(df), 
                    # ... other options) 
})

如果代码还有其他问题,我没有看太多。我建议你从共享链接中的示例开始,然后开始修改它,直到你得到你想要的。

如果这不起作用,请告诉我,我可以尝试解决

祝你好运!

于 2020-02-08T23:40:13.567 回答