使用s3write_using从R写入S3时如何提供选项?

时间:2019-07-16 07:55:40

标签: r amazon-s3

使用aws.s3库从R写入S3存储桶的过程如下

s3write_using(iris, FUN = write.csv, object = "iris.csv", bucket = "some-bucket")

但是我无法选择工作。例如。 row.names=FALSE

到目前为止我尝试过的一切

我尝试了以下

s3write_using(iris, FUN = write.csv, object = "iris.csv", bucket = "some-bucket", opts = list(row.names=FALSE))

s3write_using(iris, FUN = write.csv, object = "iris.csv", bucket = "some-bucket", opts = list("row.names"=FALSE))

# Seen here: https://github.com/cloudyr/aws.s3/issues/200
s3write_using(iris, FUN = write.csv, object = "iris.csv", bucket = "some-bucket", opts=list(headers = c('row.names' = 'FALSE')))


以上都不是错误(甚至没有警告),但是生成的.csv具有行名,而不应该这样

问题

如何使用s3write_using从R写入S3存储桶并指定row.names = FALSE(或任何其他可选参数)?

1 个答案:

答案 0 :(得分:1)

s3write_using的手册说:

  

分别传递给put_object或save_object的可选其他参数。

因此,可选参数将传递给其他aws.s3函数,而不是write.csv

但是您始终可以定义自己的函数:

mywrite <- function(x, file) {
     write.csv(x, file, row.names=FALSE)
}

然后使用此功能代替write.csv:

s3write_using(iris, FUN = mywrite, object = "iris.csv", bucket = "some-bucket")

您甚至可以创建一个函数生成器,该函数生成器返回一个环绕write.csv并提供必要参数的函数:

customize.write.csv <- function(row.names=TRUE, ...) {
    function(x, file) {
        write.csv(x, file, row.names=row.names, ...)
    }
 }

您可以这样使用:

s3write_using(iris, 
    FUN = customize.write.csv(row.names=FALSE, dec=","), 
    object = "iris.csv", 
    bucket = "some-bucket")