diff --git a/R/add_resource.R b/R/add_resource.R index 50a7c73..235bcae 100644 --- a/R/add_resource.R +++ b/R/add_resource.R @@ -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`. @@ -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", @@ -120,6 +120,8 @@ add_resource <- function(package, resource_name, data, schema = NULL, ), class = "frictionless_error_resource_already_exists" ) + } else if (replace && !(resource_name %in% resources(package))) { + replace <- FALSE } # Check data (data frame or path), content of data frame is checked later diff --git a/frictionless.Rproj b/frictionless.Rproj index ef6fa31..7ddcd6e 100644 --- a/frictionless.Rproj +++ b/frictionless.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: b6f8a5ef-2bc2-47c8-a75f-0603379777aa RestoreWorkspace: No SaveWorkspace: No diff --git a/tests/testthat/test-add_resource.R b/tests/testthat/test-add_resource.R index df91460..17e0cc2 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(resources(p), resources(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"))