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]: Transformer enters infinite recursion when lapply used to transform data. #1474

Closed
3 tasks done
chlebowa opened this issue Jan 31, 2025 · 0 comments · Fixed by insightsengineering/teal.code#244
Closed
3 tasks done
Assignees
Labels
bug Something isn't working core

Comments

@chlebowa
Copy link
Contributor

chlebowa commented Jan 31, 2025

What happened?

Following the transformer vignette, I am trying to build a transformer module that will modify my data by substituting some columns with their tranformed version.
I want to use this pattern:

data[index] <- lapply(data[index], function)

In particular, I am using scale, which is itself vectorized, so data[index] <- scale[data] is sufficient.

While this works in the console, the falls into infinite recursion.

Using lapply with any funciton has the same effect.

A for loop does works as expected.

code
library(teal)

data <- within(teal_data(), {
  iris <- iris
  mtcars <- mtcars
})

transformator_iris_scale <- teal_transform_module(
  label = "Scaling transformator for iris",
  ui = function(id) {
    ns <- NS(id)
    uiOutput(ns("scaled_columns_container"))
  },
  server = function(id, data) {
    moduleServer(id, function(input, output, session) {
      ns <- session$ns
      
      scalable_columns <- reactive({
        names(Filter(is.numeric, data()[["iris"]]))
      })

      output$scaled_columns_container <- renderUI({
        selectInput(
          inputId = ns("scaled_columns"),
          label = "Columns to scale",
          choices = scalable_columns(),
          selected = input$scaled_columns,
          multiple = TRUE
        )
      })

      # BUG HERE
      reactive({
        within(
          data(),
          {
            iris[scaled_columns] <- scale(iris[scaled_columns])
            # # these also fail:
            # iris[scaled_columns] <- lapply(iris[scaled_columns], scale)
            # iris[scaled_columns] <- lapply(iris[scaled_columns], function(x) x^2)
          },
          scaled_columns = input$scaled_columns
        )
      })
      # END BUG
      # LOOP WORKS
      # reactive({
      #   within(
      #     data(),
      #     {
      #       for (i in scaled_columns) {
      #         iris[[i]] <- scale(iris[[i]])
      #       }
      #       rm(i)
      #     },
      #     scaled_columns = input$scaled_columns
      #   )
      # })
      # END LOOP
    })
  }
)

app <- init(
  data = data,
  modules = teal::example_module(transformators = list(transformator_iris_scale))
)

shinyApp(app$ui, app$server)

sessionInfo()

> packageVersion("teal")
[1] '0.15.2.9117'

Relevant log output

Warning: Error in : evaluation nested too deeply: infinite recursion / options(expressions=)?
  97: <Anonymous>
  96: stop
  95: value[[3L]]
  94: tryCatchOne
  93: tryCatchList
  92: tryCatch
  91: do
  90: hybrid_chain
  89: renderFunc
  88: output$teal-teal_modules-example_teal_module-validate_datanames-message
   3: runApp
   2: print.shiny.appobj
   1: <Anonymous>
Warning: Error in renderUI: Error occurred during data processing. See details in the main panel.
  104: stop
  103: renderUI
  102: func
   89: renderFunc
   88: output$teal-teal_modules-example_teal_module-data_summary-table
    3: runApp
    2: print.shiny.appobj
    1: <Anonymous>
Warning: Error in : evaluation nested too deeply: infinite recursion / options(expressions=)?   
  3: runApp
  2: print.shiny.appobj
  1: <Anonymous>
Warning: Error in : evaluation nested too deeply: infinite recursion / options(expressions=)?   
  137: <Anonymous>
  136: stop
  135: data
  133: <reactive>
  113: table_data
  106: renderPrint
  105: func
   89: renderFunc
   88: output$teal-teal_modules-example_teal_module-module-text
    3: runApp
    2: print.shiny.appobj
    1: <Anonymous>

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.
@chlebowa chlebowa added the bug Something isn't working label Jan 31, 2025
@chlebowa chlebowa changed the title [Bug]: <title> [Bug]: Transformer enters infinite recursion when lapply used to transform data. Jan 31, 2025
@gogonzo gogonzo self-assigned this Feb 3, 2025
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.

3 participants