简体   繁体   中英

How do I upload a R dataframe as a CSV file on Azure blob storage?

I'm trying to convert a dataframe in R to a CSV file on Azure Blob storage. I used the AzureStor package, but it doesnt convert the dataframe properly. I expect 16 columns with data but it returns one columns where all the data is randomly split over the rows.

I used the R code below:

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")

Can anyone tell me what I need to change or give me example code of how you successfully did it?

Thanks in advance!

Here is my sample code, it works fine.

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)

The content of df.csv is as below.

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM