downloading csv file using postForm function in R

344 views Asked by At

I am trying o download csv data from the web using postForm, however, the file downloaded seems to be a html document instead of a csv file. These are my code:

############################
library(RCurl)

url <- "https://research.stlouisfed.org/fred2/series/MKTGDPSAA646NWDB/downloaddata"
params <- list(
  "native_frequency" = "Annual", units = "lin",
  "frquency" = "Annual", "aggregation" = "Average", "obs_start_date" = "1968-01-01",
  "obs_end_date" = "2014-01-01", "file_format" = "csv", "download_data_2" = "", format="csv")

result <- postForm( url, .params = params)
writeBin(as.vector(result),"doc2.txt")
1

There are 1 answers

0
hrbrmstr On

A slightly more modern approach is to use the httr package:

library(httr)

res <- POST("https://research.stlouisfed.org/",
            path="fred2/series/MKTGDPSAA646NWDB/downloaddata",
            body=list(`form[native_frequency]` = "Annual", 
                      `form[units]` = "lin", 
                      `form[frequency]` = "Annual", 
                      `form[aggregation]` = "Average", 
                      `form[obs_start_date]` = "1968-01-01", 
                      `form[obs_end_date]` = "2014-01-01", 
                      `form[file_format]` = "csv", 
                      `form[download_data_2]` = ""),
            write_disk("doc2.txt"))

warn_for_status(res)

##         DATE       VALUE
## 1 1968-01-01  4187777711
## 2 1969-01-01  4485777644
## 3 1970-01-01  5377333333
## 4 1971-01-01  7184853037
## 5 1972-01-01  9664157065
## 6 1973-01-01 14947391339