Skip to content

Commit a608748

Browse files
committed
Revert "Replace read_from property with data_location attribute"
This reverts commit ce5cbad.
1 parent ce5cbad commit a608748

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

R/read_resource.R

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

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

5756
# Read data from data
58-
} else if (data_location == "data") {
57+
} else if (resource$read_from == "data") {
5958
df <- do.call(
6059
function(...) rbind.data.frame(..., stringsAsFactors = FALSE),
6160
resource$data
6261
)
6362
df <- dplyr::as_tibble(df)
6463

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

R/resource.R

Lines changed: 7 additions & 8 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 attribute `data_location`
8-
#' to indicate how the data are attached.
7+
#' @return List describing a Data Resource, with new property `read_from` to
8+
#' indicate how data should be read.
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 data_location attribute (based on path, then df, then data)
50+
# Assign read_from property (based on path, then df, then data)
5151
if (length(resource$path) != 0) {
5252
if (all(is_url(resource$path))) {
53-
data_location <- "url"
53+
resource$read_from <- "url"
5454
} else {
55-
data_location <- "path"
55+
resource$read_from <- "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,11 +62,10 @@ resource <- function(package, resource_name) {
6262
)
6363
}
6464
} else if (is.data.frame(resource$data)) {
65-
data_location <- "df"
65+
resource$read_from <- "df"
6666
} else if (!is.null(resource$data)) {
67-
data_location <- "data"
67+
resource$read_from <- "data"
6868
}
69-
attr(resource, "data_location") <- data_location
7069

7170
return(resource)
7271
}

R/write_resource.R

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

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

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

3838
# Resource has local paths (optionally mixed with URLs)
39-
} else if (data_location == "path") {
39+
} else if (resource$read_from == "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,15 +56,14 @@ write_resource <- function(package, resource_name, directory = ".",
5656
}
5757
out_paths <- append(out_paths, file_name)
5858
}
59+
resource$read_from <- NULL
5960
resource$path <- out_paths
6061

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

66-
# Remove attributes
67-
attr(resource, "data_location") <- NULL
68-
6968
return(resource)
7069
}

tests/testthat/test-write_package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ 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)
336337
})
337338

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

0 commit comments

Comments
 (0)