2016-02-07 95 views
0

我正在嘗試使用postForm從網上下載csv數據,但是,下載的文件似乎是一個html文檔而不是csv文件。 這是我的代碼:使用postForm函數在R中下載csv文件

############################ 
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") 
+0

我同時看到拼寫錯誤和缺乏你們的名字腳本的名稱一致的。花更多時間查看頁面上的代碼。 –

+0

我用你推薦的代碼,但仍然有相同的結果 –

+0

我認爲這個問題與編碼有關 –

回答

0

稍微更現代的方法是使用httr包:

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 
+0

非常感謝! –