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

raogr8fs  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(133)

使用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(或任何其他可选参数)?

zfycwa2u

zfycwa2u1#

s3write_using手册上说:
分别传递给put_object或保存_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")
s8vozzvw

s8vozzvw2#

看起来你可以在最后添加你想要的选项,给予一试:

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

相关问题