Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: [WARN] In ‘sym_cond > ass_cond’: longer object length is not a multiple of shorter object length #235

Closed
3 tasks done
m7pr opened this issue Nov 20, 2024 · 3 comments · Fixed by #236
Closed
3 tasks done
Labels
bug Something isn't working core

Comments

@m7pr
Copy link
Contributor

m7pr commented Nov 20, 2024

Potentially caused by teal.code:::get_code_dependency

What happened?

When running below on this branch insightsengineering/teal.modules.general#795

devtools::load_all("../teal")
devtools::load_all(".")
footnote_regression <- teal_transform_module(
  server = make_teal_transform_server(expression(
    plot <- plot + labs(caption = deparse(summary(fit)[[1]]))
  ))
)

data <- teal_data()
data <- within(data, {
  require(nestcolor)
  ADSL <- rADSL
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

app <- init(
  data = data,
  modules = modules(
    tm_a_regression(
      label = "Regression",
      response = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = "BMRKR1",
          selected = "BMRKR1",
          multiple = FALSE,
          fixed = TRUE
        )
      ),
      regressor = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variables:",
          choices = variable_choices(data[["ADSL"]], c("AGE", "SEX", "RACE")),
          selected = "AGE",
          multiple = TRUE,
          fixed = FALSE
        )
      ),
      decorators = list(footnote_regression)
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}

sessionInfo()

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@m7pr m7pr added bug Something isn't working core labels Nov 20, 2024
@averissimo
Copy link
Contributor

averissimo commented Nov 20, 2024

Smaller reproducible example2 of the problem

From: sym_cond <- sym_cond[sym_cond > ass_cond] # NOTE 1

pkgload::load_all("../teal.code")
#> ℹ Loading teal.code

testthat::test_that("", {
  q <- within(qenv(), {
    a <- 1
    b <- 2
  }) |> within({
    for (x in c(1, 2)) {
      b <- a
      b <- b + a + 1
    }
  })
  
  testthat::expect_setequal(
    strsplit(get_code(q, names = "b"), "\n")[[1]],
    c(
      "a <- 1",
      "b <- 2",
      "for (x in c(1, 2)) {",
      "    b <- a",
      "    b <- b + a + 1",
      "}"
    )
  )  
})
#> ── Warning:  ───────────────────────────────────────────────────────────────────
#> longer object length is not a multiple of shorter object length
#> Backtrace:
#>      ▆
#>   1. ├─base::within(...)
#>   2. └─teal.code:::within.qenv(...)
#>   3.   ├─teal.code::eval_code(object = data, code = as.expression(calls)) at teal.code/R/qenv-within.R:70:3
#>   4.   └─teal.code::eval_code(object = data, code = as.expression(calls)) at teal.code/R/qenv-eval_code.R:31:25
#>   5.     └─base::Reduce(eval_code, init = object, x = code) at teal.code/R/qenv-eval_code.R:102:5
#>   6.       ├─teal.code (local) f(init, x[[i]])
#>   7.       └─teal.code (local) f(init, x[[i]]) at teal.code/R/qenv-eval_code.R:31:25
#>   8.         ├─teal.code::eval_code(...) at teal.code/R/qenv-eval_code.R:94:3
#>   9.         └─teal.code::eval_code(...) at teal.code/R/qenv-eval_code.R:31:25
#>  10.           └─teal.code:::extract_dependency(current_call) at teal.code/R/qenv-eval_code.R:85:5
#>  11.             └─teal.code:::extract_occurrence(reordered_pd[[1]]) at teal.code/R/utils-get_code_dependency.R:332:5

Created on 2024-11-20 with reprex v2.1.1

pkgload::load_all("/home/averissimo/work/roche🟦/packages📦/teal.code")
#> ℹ Loading teal.code

testthat::test_that("", {
  q <- within(qenv(), {
    a <- 1
    b <- 2
  }) |> eval_code("
    for (x in c(1, 2)) {
      b <- a
      b <- b + a + 1
      b + 3 -> b # yada
    }
  ")
})
#> ── Error:  ─────────────────────────────────────────────────────────────────────
#> Error in `if (unique(x$text[ass_cond]) == "->") {
#>     sym_cond <- rev(sym_cond)
#> }`: the condition has length > 1
#> Backtrace:
#>     ▆
#>  1. ├─teal.code::eval_code(...)
#>  2. └─teal.code::eval_code(...) at teal.code/R/qenv-eval_code.R:31:25
#>  3.   └─teal.code:::extract_dependency(current_call) at teal.code/R/qenv-eval_code.R:85:5
#>  4.     └─teal.code:::extract_occurrence(reordered_pd[[1]]) at teal.code/R/utils-get_code_dependency.R:332:5
#> Error:
#> ! Test failed

@averissimo
Copy link
Contributor

Related problem:

pkgload::load_all("/home/averissimo/work/roche🟦/packages📦/teal.code")
#> ℹ Loading teal.code

testthat::test_that("", {
  q <- within(qenv(), {
    a <- 1
    b <- 2
  }) |> within({
    for (x in c(1, 2)) {
      b <- a
      b <- b + a + 1
      b + 3 -> b
    }
  })
  
  testthat::expect_setequal(
    strsplit(get_code(q, names = "b"), "\n")[[1]],
    c(
      "a <- 1",
      "b <- 2",
      "for (x in c(1, 2)) {",
      "    b <- a",
      "    b <- b + a + 1",
      "    b + 3 -> b",
      "}"
    )
  )  
})
#> ── Failure:  ───────────────────────────────────────────────────────────────────
#> strsplit(get_code(q, names = "b"), "\n")[[1]] (`actual`) and c(...) (`expected`) don't have the same values.
#> * Only in `actual`: "a <- 1", "for (x in c(1, 2)) {", "    b <- a", "    b <- b + a + 1", "    b + 3 -> b", "}"
#> Error:
#> ! Test failed

Created on 2024-11-20 with reprex v2.1.1

@averissimo
Copy link
Contributor

Here's a better snippet that shows the problem where the for loop is not included in the code if it has more than 1 assignment

pkgload::load_all("../teal.code")
#> ℹ Loading teal.code
code <- "
some_other_dataset <- mtcars
original_dataset <- iris[, 1:4]
count <- 1
for (x in colnames(original_dataset)) {
  original_dataset[, x] <- original_dataset[, x] * 2
  count <- count + 1
}
output <- rlang::list2(x = original_dataset)
"


q <- eval_code(qenv(), code)
#> Warning in sym_cond > ass_cond: longer object length is not a multiple of
#> shorter object length
q |> get_code() |> cat()
#> 
#> some_other_dataset <- mtcars
#> original_dataset <- iris[, 1:4]
#> count <- 1
#> for (x in colnames(original_dataset)) {
#>   original_dataset[, x] <- original_dataset[, x] * 2
#>   count <- count + 1
#> }
#> output <- rlang::list2(x = original_dataset)
q |> get_code(names = "output") |> cat()
#> original_dataset <- iris[, 1:4]
#> output <- rlang::list2(x = original_dataset)

q$output$name[1,1]
#> NULL
iris[1, 1]
#> [1] 5.1

Created on 2024-11-27 with reprex v2.1.1

@m7pr m7pr transferred this issue from insightsengineering/teal.modules.general Nov 27, 2024
@m7pr m7pr closed this as completed in 9cf1128 Nov 29, 2024
@m7pr m7pr closed this as completed in #236 Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants