Skip to content

Commit ce5cbad

Browse files
committed
Replace read_from property with data_location attribute
We are changing to an attribute so it is easier to separate: - properties: things that are included in the datapackage.json list - attributes: things that the frictionless functions have added to pass objects from one function to the next Data frames do the same: `attributes(iris)` We still remove all attributes in write_package (write_resource), so that the invisibly returned package is as written to disk
1 parent 5d7f672 commit ce5cbad

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

R/read_resource.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ read_resource <- function(package, resource_name, col_select = NULL) {
5050
resource <- resource(package, resource_name)
5151

5252
# Read data directly
53-
if (resource$read_from == "df") {
53+
data_location <- attr(resource, "data_location")
54+
if (data_location == "df") {
5455
df <- dplyr::as_tibble(resource$data)
5556

5657
# Read data from data
57-
} else if (resource$read_from == "data") {
58+
} else if (data_location == "data") {
5859
df <- do.call(
5960
function(...) rbind.data.frame(..., stringsAsFactors = FALSE),
6061
resource$data
6162
)
6263
df <- dplyr::as_tibble(df)
6364

6465
# Read data from path(s)
65-
} else if (resource$read_from == "path" || resource$read_from == "url") {
66+
} else if (data_location == "path" || data_location == "url") {
6667
df <- read_from_path(package, resource_name, col_select)
6768
}
6869
return(df)

R/resource.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#' described `resources`.
55
#'
66
#' @inheritParams read_resource
7-
#' @return List describing a Data Resource, with new property `read_from` to
8-
#' indicate how data should be read.
7+
#' @return List describing a Data Resource, with new attribute `data_location`
8+
#' to indicate how the data are attached.
99
#' If present, `path` will be updated to contain the full path(s).
1010
#' @family accessor functions
1111
#' @noRd
@@ -47,12 +47,12 @@ resource <- function(package, resource_name) {
4747
)
4848
}
4949

50-
# Assign read_from property (based on path, then df, then data)
50+
# Assign data_location attribute (based on path, then df, then data)
5151
if (length(resource$path) != 0) {
5252
if (all(is_url(resource$path))) {
53-
resource$read_from <- "url"
53+
data_location <- "url"
5454
} else {
55-
resource$read_from <- "path"
55+
data_location <- "path"
5656
}
5757
# Expand paths to full paths, check if file exists and check path safety,
5858
# unless those paths were willingly added by user in add_resource()
@@ -62,10 +62,11 @@ resource <- function(package, resource_name) {
6262
)
6363
}
6464
} else if (is.data.frame(resource$data)) {
65-
resource$read_from <- "df"
65+
data_location <- "df"
6666
} else if (!is.null(resource$data)) {
67-
resource$read_from <- "data"
67+
data_location <- "data"
6868
}
69+
attr(resource, "data_location") <- data_location
6970

7071
return(resource)
7172
}

R/write_resource.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ write_resource <- function(package, resource_name, directory = ".",
1414
resource <- resource(package, resource_name)
1515

1616
# Resource contains new data
17-
if (resource$read_from == "df") {
17+
data_location <- attr(resource, "data_location")
18+
if (data_location == "df") {
1819
if (compress) {
1920
file_name <- paste(resource_name, "csv", "gz", sep = ".")
2021
} else {
@@ -28,15 +29,14 @@ write_resource <- function(package, resource_name, directory = ".",
2829
resource$mediatype <- "text/csv"
2930
resource$encoding <- "utf-8" # Enforced by readr::write_csv()
3031
resource$dialect <- NULL
31-
resource$read_from <- NULL
3232
resource$data <- NULL
3333

3434
# Resource originally had data property
35-
} else if (resource$read_from == "data") {
36-
resource$read_from <- NULL
35+
} else if (data_location == "data") {
36+
# Do nothing
3737

3838
# Resource has local paths (optionally mixed with URLs)
39-
} else if (resource$read_from == "path") {
39+
} else if (data_location == "path") {
4040
# Download or copy file to directory, point path to file name (in that dir)
4141
# Note that existing files will not be overwritten
4242
out_paths <- vector()
@@ -56,14 +56,15 @@ write_resource <- function(package, resource_name, directory = ".",
5656
}
5757
out_paths <- append(out_paths, file_name)
5858
}
59-
resource$read_from <- NULL
6059
resource$path <- out_paths
6160

6261
# Resource has URL paths (only)
63-
} else if (resource$read_from == "url") {
62+
} else if (data_location == "url") {
6463
# Don't touch file, leave URL path as is
65-
resource$read_from <- NULL
6664
}
6765

66+
# Remove attributes
67+
attr(resource, "data_location") <- NULL
68+
6869
return(resource)
6970
}

tests/testthat/test-write_package.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ test_that("write_package() sets correct properties for data frame resources", {
333333
expect_null(resource_written$dialect)
334334
expect_identical(resource_written$schema, schema)
335335
expect_null(resource_written$data)
336-
expect_null(resource_written$read_from)
337336
})
338337

339338
test_that("write_package() retains custom properties set in add_resource()", {

0 commit comments

Comments
 (0)