Skip to content

Commit

Permalink
Merge pull request #72 from phuse-org/hotfix-dynamic-referencing-df
Browse files Browse the repository at this point in the history
extend dynamic referencing for data.frame
  • Loading branch information
mariev authored Mar 18, 2021
2 parents fb42035 + ffb6270 commit 585de77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions R/dynamic_referencing.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ vt_dynamic_referencer <- R6::R6Class("vt_dynamic_referencer",
scrape_references = function(text){

## Drop roxygen comment headers from text for scraping references.
text <- text[!grepl("^#'", text)]
text <- unname(unlist(text[!grepl("^#'", text)]))

reference_locations <-
gregexpr(
Expand Down Expand Up @@ -133,13 +133,21 @@ vt_dynamic_referencer <- R6::R6Class("vt_dynamic_referencer",
for(ref in names(references)){
ref_value <- references[[ref]]

if("data.frame" %in% class(text)){
text <- as.data.frame(lapply(text, gsub,
pattern = paste0(private$reference_indicator, ref),
replacement = ref_value,
fixed = TRUE))
} else {
text <- gsub(
pattern = paste0(private$reference_indicator,ref),
replacement = ref_value,
text,
fixed = TRUE,
)
}


text = gsub(
pattern = paste0(private$reference_indicator,ref),
replacement = ref_value,
text,
fixed = TRUE,
)
}

text
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-dynamic_referencing.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,21 @@ test_that("Dynamic Number Referencing across rmarkdown chunks", {
))

})

test_that("Dynaming referencing within a data.frame",{

cov_matrix_content <- data.frame( req_id = "##req:first",
tc_id = c("##tc:first", "##tc:second"))

references <- vt_dynamic_referencer$new()
references$initialize(reference_indicator = "##")
references$scrape_references(cov_matrix_content)
expect_equal(references$reference_insertion(cov_matrix_content),
data.frame(req_id = "1",
tc_id = c("1", "2")))

expect_equal(references$reference_insertion(cov_matrix_content$tc_id),
c("1", "2"))


})

0 comments on commit 585de77

Please sign in to comment.