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
8 changes: 5 additions & 3 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 @@ -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
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(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"))
Expand Down