0

I'm trying to get a csv-file into R. I have made the following code which creates the link to the csv-file. The code works perfectly when I use the generated link in my browser. But doesn't work properly when I run the code. Hope somebody can help.

Jakob

library(httr)
library(tidyverse)
library(stringr)
library(jsonlite)


metadata.dst <- function(tableid){
link.metadata <- "http://api.statbank.dk/v1/tableinfo/"
GET(str_c(link.metadata,tableid)) %>%
  content("text") %>%
  fromJSON()     
}

download.link.dst <- function(tableid){
 table <- tolower(tableid)
 base.link_start <- "http://api.statbank.dk/v1/data/"
 base.link_end <- "/CSV?delimiter=Semicolon"
 link_start <- str_c(base.link_start, table,base.link_end)

variables <- metadata.dst(tableid)$variables$id
 link_end <- str_c("&",variables) %>%
 str_c("=*") %>%
 str_c(collapse = "")

download.link <- str_c(link_start, link_end) %>%
 str_replace("Å","%C3%85") %>%
 str_replace("å", "%C3%A5") %>%
 str_replace("Ø", "%C3%98") %>%
 str_replace("ø", "%C3%B8") %>%
 str_replace("Æ", "%C3%86") %>%
 str_replace("æ", "%C3%A6")
 download.link

}

read_csv2(download.link.dst("FOLK1B"))
  • Your R code generates this for me: `http://api.statbank.dk/v1/data/folk1b/CSV?delimiter=Semicolon&OMR%C3%85DE=*&K%C3%98N=*&ALDER=*&STATSB=*&Tid=*` When I open it I get this error: `{"errorTypeCode":"EXTRACT-NOTFOUND","message":"Tabel findes ikke: Folk1"}` (and it sometimes takes a minute to load) – chrki Apr 20 '17 at 19:47
  • Looks like it converts `FOLK1B` to lower-case `folk1b` which doesn't work – chrki Apr 20 '17 at 19:49
  • If I type the link into a browser it starts to download a very large CSV file. Your code seems to be doing something similar (not finished yet!) You just need to save the file as a variable rather than try to display it on screen (it crashed using your final line as it stands) - e.g. `df <- read_csv2(download.link.dst("FOLK1B"))`. Is there a way of doing it in smaller chunks first to check that it is working? By the way, you can replace your chain of `str_replace`s with `download.link <- str_c(link_start, URLencode(link_end))`. – Andrew Gustar Apr 20 '17 at 20:06

0 Answers0