Skip to content

Commit 3af8c95

Browse files
m7prgithub-actions[bot]gogonzo
authored
262 Fix improper code dependency (#264)
Fixes #262 and insightsengineering/teal.gallery#218 Alternative solution: #263 Functions were kept on the left side of the dependency graph, which lead to cyclic unwanted dependencies. Now all functions, even when used on the LHS are moved to the RHS of the dependency graph. ```r devtools::load_all(".") devtools::load_all("../teal.data") data <- teal_data() data <- within(data, { library(random.cdisc.data) library(nestcolor) ADSL <- radsl(seed = 1) ADMH <- radmh(ADSL, seed = 1) ADVS <- radvs(ADSL, seed = 1) teal.data::col_labels(ADMH[c("MHDISTAT")]) <- c("Status of Disease") ADVS <- dplyr::inner_join(x = ADVS, y = ADSL[, c("STUDYID", "USUBJID"), drop = FALSE], by = c("STUDYID", "USUBJID")) }) data@code cat(get_code(data, names = "ADVS")) ``` ```r library(random.cdisc.data) library(nestcolor) ADSL <- radsl(seed = 1) ADVS <- radvs(ADSL, seed = 1) ADVS <- dplyr::inner_join(x = ADVS, y = ADSL[, c("STUDYID", "USUBJID"), drop = FALSE], by = c("STUDYID", "USUBJID")) ``` --------- Signed-off-by: Marcin <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Dawid Kałędkowski <[email protected]>
1 parent cb36b37 commit 3af8c95

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

R/utils-get_code_dependency.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ extract_occurrence <- function(pd) {
303303

304304
after <- match(min(x$id[assign_cond]), sort(x$id[c(min(assign_cond), sym_cond)])) - 1
305305
ans <- append(x[sym_cond, "text"], "<-", after = max(1, after))
306+
ans <- move_functions_after_arrow(ans, unique(x[sym_fc_cond, "text"]))
306307
roll <- in_parenthesis(pd)
307308
if (length(roll)) {
308309
c(setdiff(ans, roll), roll)
@@ -311,6 +312,29 @@ extract_occurrence <- function(pd) {
311312
}
312313
}
313314

315+
#' Moves function names to the right side of dependency graph
316+
#'
317+
#' Changes status of the function call from dependent to dependency if occurs in the lhs.
318+
#' Technically, it means to move function names after the dependency operator.
319+
#' For example, for `attributes(a) <- b` the dependency graph should look like `c("a", "<-", "b", "attributes")`.
320+
#'
321+
#' @param ans `character` vector of object names in dependency graph.
322+
#' @param functions `character` vector of function names.
323+
#'
324+
#' @return
325+
#' A character vector.
326+
#' @keywords internal
327+
#' @noRd
328+
move_functions_after_arrow <- function(ans, functions) {
329+
arrow_pos <- which(ans == "<-")
330+
if (length(arrow_pos) == 0) {
331+
return(ans)
332+
}
333+
before_arrow <- setdiff(ans[1:arrow_pos], functions)
334+
after_arrow <- ans[(arrow_pos + 1):length(ans)]
335+
c(before_arrow, unique(c(intersect(ans[1:arrow_pos], functions), after_arrow)))
336+
}
337+
314338
#' Extract side effects
315339
#'
316340
#' Extracts all object names from the code that are marked with `@linksto` tag.

tests/testthat/test-qenv_get_code.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ testthat::describe("get_code for specific names", {
290290
)
291291
}
292292
)
293+
testthat::it(
294+
"doesn't consider function called on the lhs as a dependent in this call (dependency in further calls)",
295+
{
296+
code <- c(
297+
"object_list <- list(x = iris, y = iris)",
298+
"object_list_2 <- list(x = mtcars, y = mtcars)",
299+
"object_list_2[c('x')] <- c('string')",
300+
"object_list[c('x')] <- c('string')"
301+
)
302+
q <- eval_code(qenv(), code = code)
303+
result <- get_code(q, names = "object_list")
304+
testthat::expect_identical(result, paste(code[c(1, 4)], collapse = "\n"))
305+
}
306+
)
293307
})
294308

295309

0 commit comments

Comments
 (0)