sách gpt4 ai đã đi

r - 有没有办法将几个 excel 文件从 Dropbox 文件夹加载到 R-shiny 应用程序中?

In lại 作者:行者123 更新时间:2023-12-04 09:57:51 26 4
mua khóa gpt4 Nike

我在 R Shiny 中工作还是比较新的,我正在尝试将几个 excel 文件加载到 R-shiny 应用程序中。部分问题是我需要能够从保管箱文件夹中提取多个文件,而无需指定数据文件的名称。所以我需要能够告诉 R 从 Dropbox 文件夹中读取所有文件。我正在使用的文件也是 .xlsx 格式,我需要将它们读入 R 中。

我首先尝试使用计算机桌面上的文件夹来执行此操作。我设法使用我的本地目录和下面的代码让它工作:

library(readxl)
library(tidyverse)

files <- list.files(path = "~/Desktop/data", pattern = "*.xlsx", full.names = TRUE) #read files from folder on desktop
df <- sapply(files, read_excel, simplify = FALSE) %>% #read files from the path, and bind them together
bind_rows()

我尝试调整上面的代码以使用 rdrop2 中的 drop_dir 函数。我试过的代码如下:
library(rdrop2)
library(tidyverse)
library(readxl)

token <- drop_auth()
files <- drop_dir("!dropbox_folder", dtoken = token) #List all files in Dropbox folder MPD_03_Test
f <- files$path_display #list directory to dropbox
df <- sapply(f, read_excel, simplify = FALSE) %>% #runs the read function for all the files that are pulled
bind_rows() # .id="id creates a unique ID for each row and then binds them all together based on the ID.

当我运行它时,代码不会将数据文件从保管箱加载到 R 中。当我运行保管箱代码时,它只会创建一个空对象。任何关于去哪里解决这个问题的帮助将不胜感激!此外,如果这有助于构建您可能对如何解决我的问题提出的任何建议,我打算将其用作我将数据读入和 R-shiny 应用程序的方式。

谢谢你!

1 Câu trả lời

@MrGumble 在他的评论中是正确的。这些文件需要在阅读之前下载。 drop_dir()函数列出了保管箱服务器上的文件路径,我们只能读取本地保存到我们机器的数据。如果您有 .csv 文件,则可以使用 drop_read_csv() 一步完成。功能。但是由于您有 excel 文件,因此需要首先使用 drop_download() 显式下载这些文件。然后用 read_excel() 读入.

library(rdrop2)
library(tidyverse)
library(readxl)
#install.packages("xlsx")
library(xlsx)

token <- drop_auth()

#make a few excel file with iris dataset, save locally, and upload to dropbox root
iris_filenames <- paste0("iris", 1:3, ".xlsx")
walk(iris_filenames, ~write.xlsx(iris, file = .x, row.names = FALSE))
walk(iris_filenames, drop_upload)

#list all files on dropbox root and filter for only iris ones
iris_files_on_dropbox <- drop_dir(dtoken = token) %>%
filter(str_detect(name, 'iris'))
#make new filenames so we can see that the download worked correctly
#you could do overwrite = TRUE and not pass through new filenames
#see ?drop_download for all options
new_iris_filenames <- paste0("iris", 1:3, "-from-dropbox.xlsx")

#download the files first
walk2(iris_files_on_dropbox$name, new_iris_filenames, ~drop_download(path = .x, local_path = .y))

#then read them all in
df <- bind_rows(map(new_iris_filenames, read_xlsx))

此外,我们可以创建自己的自定义函数来完成 1 步骤的下载和阅读,就像 drop_read_csv()通过更改此函数的源代码来实现。我们需要做的就是更改 read...()从 read.csv 到 read_excel 的函数以及对 dtoken 默认值的引用 get_drop_token()ĐẾN rdrop2:::get_drop_token()这是 rdrop2 包中未导出的函数,因此我们需要三个 ':::'。
#source for drop_read_csv we can rewrite for excel files
# drop_read_csv <- function(file, dest = tempdir(), dtoken = get_dropbox_token(), ...) {
# localfile = paste0(dest, "/", basename(file))
# drop_download(file, localfile, overwrite = TRUE, dtoken = dtoken)
# utils::read.csv(localfile, ...)
# }


drop_read_excel <- function(file, dest = tempdir(), dtoken = rdrop2:::get_dropbox_token(), ...) {
localfile = paste0(dest, "/", basename(file))
drop_download(file, localfile, overwrite = TRUE, dtoken = dtoken)
readxl::read_excel(localfile, ...)
}

df2 <- bind_rows(map(iris_files_on_dropbox$name, drop_read_excel))

要在 Shiny 的应用程序中工作,我们首先需要保存 drop_auth token ,这样我们就可以在使用 Shiny 的应用程序时进行身份验证。将其保存到 Shiny 的应用程序目录中。
saveRDS(token, file = "token.rds")

现在这是一个 Shiny 的应用程序。单击“开始”按钮时,将下载 iris excel 文件并显示在 UI 中。我们需要调用 drop_auth()在全局环境或 global.R 以及自定义 drop_read_excel()使用它的功能。
library(shiny)
library(rdrop2)
library(tidyverse)


#saveRDS(token, file = "token.rds") into shiny app directory
#authenticate in global.R or outside of ui/server
drop_auth(rdstoken = "token.rds")
drop_read_excel <- function(file, dest = tempdir(), dtoken = rdrop2:::get_dropbox_token(), ...) {
localfile = paste0(dest, "/", basename(file))
drop_download(file, localfile, overwrite = TRUE, dtoken = dtoken)
readxl::read_excel(localfile, ...)
}


ui <- fluidPage(
actionButton("go", "go"),
tableOutput("table")

)

server <- function(input, output, session) {
df <- eventReactive(input$go, {
withProgress(message = 'Downloading from dropbox',
detail = 'This may take a while...', value = 0.5, {
iris_files_on_dropbox <- drop_dir() %>%
filter(str_detect(name, 'iris'))
setProgress(value = 0.75)
df <- bind_rows(map(iris_files_on_dropbox$name, drop_read_excel))
setProgress(value = 1)
})
return(df)
})

output$table <- renderTable({
df()
})

}

shinyApp(ui, server)

nhập mô tả hình ảnh ở đây

关于r - 有没有办法将几个 excel 文件从 Dropbox 文件夹加载到 R-shiny 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61884677/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com