Skip to content
Merged
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
20 changes: 11 additions & 9 deletions R/add_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#' @param schema Either a list, or path or URL to a JSON file describing a Table
#' Schema for the `data`.
#' If not provided, one will be created using [create_schema()].
#' @param replace If `TRUE`, the added resource will replace an existing
#' resource with the same name.
#' @param replace If `TRUE`, allows an existing resource of the same name to be
#' replaced.
#' @param delim Delimiter for the CSV file(s) referenced in `data` (e.g. `\t`
#' for a tab-separated file).
#' Will be set as `delimiter` in the resource Table Dialect, so read functions
Expand Down Expand Up @@ -73,9 +73,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 @@ -211,12 +211,14 @@ 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 {
# Add or replace resource
index <- which(resource_names(package) == resource_name)
if (length(index) == 0) {
# Add resource if it does not exist (also for replace = TRUE)
package$resources <- append(package$resources, list(resource))
} else {
# Replace existing resource (not done for replace = FALSE, see higher)
package$resources[[index]] <- resource
}

return(package)
Expand Down
8 changes: 4 additions & 4 deletions man/add_resource.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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(resource_names(p), "new_resource"), resource_names(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