【问题标题】:How do I upload a R dataframe as a CSV file on Azure blob storage?如何将 R 数据帧作为 CSV 文件上传到 Azure Blob 存储?
【发布时间】:2019-08-02 14:53:29
【问题描述】:

我正在尝试将 R 中的数据帧转换为 Azure Blob 存储上的 CSV 文件。我使用了AzureStor 包,但它没有正确转换数据帧。我希望有 16 列包含数据,但它返回一列,其中所有数据随机拆分到各行中。

我使用了下面的 R 代码:

library(AzureStor)


bl_endp_key <- storage_endpoint("url", key="key")
cont <- storage_container(bl_endp_key, "containername")
csv <- serialize(dataframe, connection = NULL, ascii = TRUE)
con <- rawConnection(csv)
upload_blob(cont, src=con, dest="output.csv")

谁能告诉我我需要改变什么,或者给我你如何成功做到这一点的示例代码?

提前致谢!

【问题讨论】:

    标签: r azure azure-blob-storage


    【解决方案1】:

    这是我的示例代码,它工作正常。

    library(AzureStor)
    
    df <- data.frame(Column1 = c('Value 1', 'Value 2', 'Value 3'),
                     Column2 = c('Value 1', 'Value 2', 'Value 3'))
    
    account_endpoint <- "https://<your account name>.blob.core.windows.net"
    account_key <- "<your account key>"
    container_name <- "<your container name>"
    bl_endp_key <- storage_endpoint(account_endpoint, key=account_key)
    cont <- storage_container(bl_endp_key, container_name)
    w_con <- textConnection("foo", "w")
    write.csv(df, w_con)
    r_con <- textConnection(textConnectionValue(w_con))
    close(w_con)
    upload_blob(cont, src=r_con, dest="df.csv")
    close(con)
    

    df.csv的内容如下。

    "","Column1","Column2"
    "1","Value 1","Value 1"
    "2","Value 2","Value 2"
    "3","Value 3","Value 3"
    

    希望对你有帮助。

    【讨论】:

    • 谢谢彼得潘,它帮助了我。你能帮我理解这部分代码吗w_con &lt;- textConnection("foo", "w") write.csv(df, w_con) CSV 是在哪里创建的
    • 嗨彼得潘,有没有办法将 RData 文件上传到 blob 存储?非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2019-08-13
    • 2019-12-01
    • 2017-01-24
    • 2017-08-19
    • 2022-07-21
    相关资源
    最近更新 更多