Download a file
Usage
ckan_fetch(
x,
store = "session",
path = "file",
format = NULL,
key = get_default_key(),
...
)Arguments
- x
URL of the file
- store
One of session (default) or disk. The session option stores the file in the R session, and the disk option saves the file to disk.
- path
If
store="disk", give a path to store the file.- format
Format of the file. If the file URL does not show the format, pass
format.- key
A CKAN API key (optional, character)
- ...
Extra arguments. If
store="session", the function passes them to the function that reads the file. See details.
Value
With store = "session" (default), the file contents, usually a
data.frame (Excel files with multiple sheets and ZIP files return a named
list). With store = "disk", a list with download details including the
file path.
Details
You can pass additional arguments with ... to the function
that reads the file. The function uses ... only when store="session"
and the file is not a ZIP file.
This list shows the function that reads each file format. You can see what additional arguments are available:
csv: read.csv
xls, xlsx: read_excel
xml: read_xml
html: read_html
json: fromJSON
shp, geojson: st_read
txt: read.table
parquet: read_parquet
Examples
if (FALSE) { # \dontrun{
# CSV file
ckanr_setup("https://datamx.io")
res <- resource_show(
id = "6145a539-cbde-4b0d-a3d3-d1a5eb013f5c",
as = "table"
)
head(ckan_fetch(res$url))
ckan_fetch(res$url, "disk", "myfile.csv")
# CSV file, format not available
ckanr_setup("https://ckan0.cf.opendata.inter.prod-toronto.ca")
res <- resource_show(
id = "c57c3e1c-20e2-470f-bc82-e39a0264be31",
as = "table"
)
res$url
res$format
head(ckan_fetch(res$url, format = res$format))
# Excel file - requires readxl package
ckanr_setup("https://datamx.io")
res <- resource_show(
id = "e883510e-a082-435c-872a-c5b915857ae1",
as = "table"
)
head(ckan_fetch(res$url))
# Excel file, multiple sheets - requires readxl package
ckanr_setup()
res <- resource_show(
id = "ce02a1cf-35f1-41df-91d9-11ed1fdd4186",
as = "table"
)
x <- ckan_fetch(res$url)
names(x)
head(x[["Mayor - Maire"]])
# XML file - requires xml2 package
ckanr_setup("https://data.ottawa.ca")
res <- resource_show(id = "380061c1-6c46-4da6-a01b-7ab0f49a881e",
as = "table")
ckan_fetch(res$url)
# HTML file - requires xml2 package
ckanr_setup("https://open.canada.ca/data/en")
res <- resource_show(
id = "80321bac-4283-487c-93bd-c65acaa660f5",
as = "table"
)
ckan_fetch(res$url)
library("xml2")
xml_text(xml_find_first(xml_children(ckan_fetch(res$url))[[1]], "title"))
# JSON file, by default reads in to a data.frame for ease of use
ckanr_setup("https://data.surrey.ca")
res <- resource_show(
id = "8d07c662-800d-4977-9e3e-5a3d2d1e99ab",
as = "table"
)
head(ckan_fetch(res$url))
# SHP file (spatial data, ESRI format) - requires sf package
ckanr_setup("https://ckan0.cf.opendata.inter.prod-toronto.ca")
res <- resource_show(
id = "27362290-8bbf-434b-a9de-325a6c2ef923",
as = "table"
)
x <- ckan_fetch(res$url)
class(x)
plot(x[, c("AREA_NAME", "geometry")])
# GeoJSON file - requires sf package
ckanr_setup("https://datamx.io")
res <- resource_show(
id = "b1cd35b7-479e-4fa0-86e9-e897d3c617e6",
as = "table"
)
x <- ckan_fetch(res$url)
class(x)
plot(x[, c("mun_name", "geometry")])
# ZIP file - packages required depends on contents
ckanr_setup("https://ckan0.cf.opendata.inter.prod-toronto.ca")
res <- resource_show(
id = "bb21e1b8-a466-41c6-8bc3-3c362cb1ed55",
as = "table"
)
x <- ckan_fetch(res$url)
names(x)
head(x[["ChickenpoxAgegroups2017.csv"]])
# TXT file
ckanr_setup("https://ckan0.cf.opendata.inter.prod-toronto.ca")
res <- resource_show(
id = "e4211f49-611f-438c-a444-aaa7f3f84117",
as = "table"
)
x <- ckan_fetch(res$url)
head(x)
# TXT file, semicolon used as separator
ckanr_setup("https://data.coat.no")
res <- resource_show(
id = "384fe537-e0bd-4e57-8a0d-420b7a745196",
as = "table"
)
x <- ckan_fetch(res$url, sep = ";")
head(x)
# Parquet file - requires arrow package
ckanr_setup("https://data.coat.no")
res <- resource_show(
id = "b7d1c192-0f44-4bab-bd51-eea0d408a09f",
as = "table"
)
x <- ckan_fetch(res$url)
head(x)
} # }
