如何将R数据帧写入Google Cloud Storage存储桶文件夹

时间:2018-12-11 22:50:44

标签: r google-cloud-platform google-cloud-storage

我想将R数据帧写入Google Cloud Storage存储桶。我在R中使用googleCloudStorageR库,这是我的代码-

################### START ###############################

options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/cloud-platform")
# load the libraries
library("googleAuthR", lib.loc="/home/user/packages" )
library("googleCloudStorageR", lib.loc="/home/user/packages")
gar_gce_auth()


name <- c("john","doe")
id <- c(1,2)
results = as.data.frame(cbind(name,id))
print("writing results to GCS")

# Set bucket name
bucket <- "my-bucket"
gcs_global_bucket(bucket)
print("bucket set.")


# Upload that file to the global bucket
gcs_upload(file = results , name = "results.csv")

################## END ################################

这会将数据帧加载到存储区my-bucket中。

现在,我想保存相同的数据帧,但保存到不同的文件夹结构,例如-my-bucket\my-folder\results.csv。如何将数据框保存到GCS中的此新路径?

https://cran.r-project.org/web/packages/googleCloudStorageR/googleCloudStorageR.pdf

1 个答案:

答案 0 :(得分:0)

您将名称更改为文件夹结构(unix样式),因此更改最后一行:

# Upload that file to the global bucket
gcs_upload(file = results , name = "my-folder/results.csv")