diff --git a/R/add_resource.R b/R/add_resource.R index bd98d29..8d9e1e6 100644 --- a/R/add_resource.R +++ b/R/add_resource.R @@ -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 @@ -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", @@ -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) diff --git a/man/add_resource.Rd b/man/add_resource.Rd index cc6d7f4..6e9fd35 100644 --- a/man/add_resource.Rd +++ b/man/add_resource.Rd @@ -36,8 +36,8 @@ and properties. Schema for the \code{data}. If not provided, one will be created using \code{\link[=create_schema]{create_schema()}}.} -\item{replace}{If \code{TRUE}, the added resource will replace an existing -resource with the same name.} +\item{replace}{If \code{TRUE}, allows an existing resource of the same name to be +replaced.} \item{delim}{Delimiter for the CSV file(s) referenced in \code{data} (e.g. \verb{\\t} for a tab-separated file). @@ -99,9 +99,9 @@ package <- add_resource( # 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", diff --git a/tests/testthat/test-add_resource.R b/tests/testthat/test-add_resource.R index 53158ae..fcc8244 100644 --- a/tests/testthat/test-add_resource.R +++ b/tests/testthat/test-add_resource.R @@ -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"))