-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwrite_resource.R
76 lines (71 loc) · 2.45 KB
/
write_resource.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#' Write a Data Resource to disk
#'
#' Helper function used by [write_package()] to write Data Resources to disk.
#'
#' @inheritParams read_resource
#' @inheritParams write_package
#' @return Updated list describing a Data Resource, ready to be included in a
#' `datapackage.json`.
#' @family write functions
#' @noRd
write_resource <- function(package, resource_name, directory = ".",
compress = FALSE) {
# Get resource, includes check_package()
resource <- get_resource(package, resource_name)
# Resource contains new data
if (resource$read_from == "df") {
if (compress) {
file_name <- paste(resource_name, "csv", "gz", sep = ".")
} else {
file_name <- paste(resource_name, "csv", sep = ".")
}
readr::write_csv(resource$data, file.path(directory, file_name), na = "")
# Save schema and reassign all resource properties (in correct order)
# This also removes $data and $read_from
schema <- resource$schema
resource <- list(
name = resource_name,
path = file_name,
profile = "tabular-data-resource",
# title: not set
# description: not set
format = "csv",
mediatype = "text/csv",
encoding = "utf-8", # Enforced by readr::write_csv()
# dialect: not set, default
# bytes: not set
# hash: not set
# sources: not set
# licenses: not set
schema = schema
)
# Resource originally had data property
} else if (resource$read_from == "data") {
resource$read_from <- NULL
# Resource has local paths (optionally mixed with URLs)
} else if (resource$read_from == "path") {
# Download or copy file to directory, point path to file name (in that dir)
# Note that existing files will not be overwritten
out_paths <- vector()
for (path in resource$path) {
file_name <- basename(path)
destination <- file.path(directory, file_name)
if (is_url(path)) {
if (!file.exists(destination)) {
message(glue::glue("Downloading file from {path}"))
utils::download.file(path, destination, quiet = TRUE)
}
} else {
file.copy(path, destination, overwrite = FALSE)
}
out_paths <- append(out_paths, file_name)
}
resource$read_from <- NULL
resource$path <- out_paths
# Resource has URL paths (only)
} else if (resource$read_from == "url") {
# Don't touch file, leave URL path as is
resource$read_from <- NULL
}
return(resource)
}