如何将R数据帧上传到谷歌硬盘?

如何将R数据帧上传到谷歌硬盘?,r,r-googlesheets,R,R Googlesheets,我使用的是来自CRAN的googledrive软件包。但是,function-drive_upload允许您上载本地文件,而不是数据帧。有人能帮忙吗?只需将有问题的数据帧保存到本地文件。最基本的选项是保存到CSV或保存RData 例如: test <- data.frame(a = 1) tempFileCon <- file() write.csv(test, file = tempFileCon) rm(test) load("test.Rds") exists("test")

我使用的是来自CRAN的googledrive软件包。但是,function-drive_upload允许您上载本地文件,而不是数据帧。有人能帮忙吗?

只需将有问题的数据帧保存到本地文件。最基本的选项是保存到CSV或保存RData

例如:

test <- data.frame(a = 1)
tempFileCon <- file()
write.csv(test, file = tempFileCon)
rm(test)
load("test.Rds")
exists("test")

test不幸的是,我找不到直接向上推数据帧的方法,但我只想为其他试图完成这个问题所涉及的基本内容的人提供一个文档,下面的代码编写了一个local.csv,然后通过
tidyverse::googledrive
将其反弹到上面,以googlesheet的形式表达自己

write_csv(iris, 'df_iris.csv')
drive_upload('df_iris.csv', type='spreadsheet')

您可以使用googlesheets包中的gs_add_行来实现这一点。此API直接接受数据帧作为输入参数,并将数据上载到指定的google工作表。本地文件不是必需的

从?gs_添加_行的“帮助”部分:


“如果输入是二维的,我们在内部为每个输入行调用一次gs_add_row。”

这可以通过两种方式完成。和其他人提到的一样,可以创建本地文件并上传。也可以在驱动器中创建新的电子表格。此电子表格将在驱动器的主文件夹中创建。如果要将其存储在其他位置,可以在创建后移动它

# install the packages
install.packages("googledrive", "googlesheets4")

# load the libraries
library(googledrive)
library(googlesheets4)


## With local storage
# Locally store the file
write.csv(x = iris, file = "iris.csv")

# Upload the file
drive_upload(media = "iris.csv", type='spreadsheet')


## Direct storage
# Create an empty spreadsheet. It is stored as an object with a sheet_id and drive_id 
ss <- gs4_create(name = "my_spreadsheet", sheets = "Sheet 1")

# Put the data.frame in the spreadsheet and provide the sheet_id so it can be found
sheet_write(data=iris, ss = ss, sheet ="Sheet 1")

# Move your spreadsheet to the desired location
drive_mv(file = ss, path = "my_creations/awesome location/")
#安装软件包
安装软件包(“googledrive”、“googlesheets4”)
#加载库
图书馆(谷歌硬盘)
图书馆(谷歌地图4)
##使用本地存储
#本地存储该文件
write.csv(x=iris,file=“iris.csv”)
#上传文件
驱动器上传(media=“iris.csv”,type='spreadsheet')
##直接存储
#创建一个空的电子表格。它存储为具有图纸id和驱动器id的对象

ss我不想将文件保存到本地。我需要直接上传到谷歌硬盘。您的方法需要我将其保存到本地如果您被迫无法编写要丢弃的临时文件,那么解决方案将需要更多的工作。你确定不能创建临时文件吗?是的,我确定。我无法创建临时文件。您可以使用file()。考虑到您的澄清,我已经更新了答案。在文件连接步骤之后,我们如何使用
drive\u upload
?上传到Google drive和Google Sheets并不完全相同:您想做什么?
# install the packages
install.packages("googledrive", "googlesheets4")

# load the libraries
library(googledrive)
library(googlesheets4)


## With local storage
# Locally store the file
write.csv(x = iris, file = "iris.csv")

# Upload the file
drive_upload(media = "iris.csv", type='spreadsheet')


## Direct storage
# Create an empty spreadsheet. It is stored as an object with a sheet_id and drive_id 
ss <- gs4_create(name = "my_spreadsheet", sheets = "Sheet 1")

# Put the data.frame in the spreadsheet and provide the sheet_id so it can be found
sheet_write(data=iris, ss = ss, sheet ="Sheet 1")

# Move your spreadsheet to the desired location
drive_mv(file = ss, path = "my_creations/awesome location/")