Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions R/add_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' @param delim Single character used to separate the fields in the CSV file(s),
#' e.g. `\t` for tab delimited file.
#' Will be set as `delimiter` in the resource Table Dialect, so read functions
#'. know how to read the file(s).
#' know how to read the file(s).
#' @param ... Additional [metadata properties](
#' https://docs.ropensci.org/frictionless/articles/data-resource.html#properties-implementation)
#' to add to the resource, e.g. `title = "My title", validated = FALSE`.
Expand Down Expand Up @@ -72,9 +72,9 @@
#'
#' # Replace the resource "observations" with a file-based resource (2 TSV files)
#' path_1 <-
#' system.file("extdata", "v1", "observations_1.tsv", package = "frictionless")
#' system.file("extdata", "v1", "observations_1.tsv", package = "frictionless")
#' path_2 <-
#' system.file("extdata", "v1", "observations_2.tsv", package = "frictionless")
#' system.file("extdata", "v1", "observations_2.tsv", package = "frictionless")
#' package <- add_resource(
#' package,
#' resource_name = "observations",
Expand Down Expand Up @@ -210,12 +210,25 @@ add_resource <- function(package, resource_name, data, schema = NULL,
attr(resource, "path") <- "added"
}

# Add or replace resource (needs to be wrapped in its own list)
if (replace) {
index <- which(purrr::map(package$resources, "name") == resource_name)
package$resources[index] <- list(resource)
} else {
# Find the index of the index of the name of the resource if it exists, or 0
# otherwise
index <- purrr::detect_index(
package$resources,
.f = \(resource) {
identical(
resource$name,
resource_name
)
}
)

# Add the resource if it's missing
if (index == 0L) {
package$resources <- append(package$resources, list(resource))
} else {
# Or replace it if not, replace = FALSE on an existing resource should fail
# earlier
package$resources[[index]] <- resource
}

return(package)
Expand Down
1 change: 1 addition & 0 deletions frictionless.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: b6f8a5ef-2bc2-47c8-a75f-0603379777aa

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-add_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ test_that("add_resource() can replace an existing resource", {
expect_equal(resource_names(p), resource_names(p_replaced))
})

test_that("add_resource() can add a new resource even with replace=TRUE", {
p <- example_package()
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
expect_no_error(
add_resource(p, "new_resource", df, replace = TRUE)
)
p_replaced <- add_resource(p, "new_resource", df, replace = TRUE)
expect_equal(c(resources(p), "new_resource"), resources(p_replaced))
})

test_that("add_resource() uses provided schema (list or path) or creates one", {
p <- create_package()
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
Expand Down
Loading