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]: Error in srv_transform_teal_data when data during initialisation #1485

Closed
3 tasks done
vedhav opened this issue Feb 6, 2025 · 1 comment · Fixed by #1487
Closed
3 tasks done

[Bug]: Error in srv_transform_teal_data when data during initialisation #1485

vedhav opened this issue Feb 6, 2025 · 1 comment · Fixed by #1487
Assignees
Labels
bug Something isn't working core

Comments

@vedhav
Copy link
Contributor

vedhav commented Feb 6, 2025

What happened?

devtools::load_all("teal")

tm_decorated_plot <- function(label = "module", decorators = list()) {
  checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)

  module(
    label = label,
    ui_args = list(decorators = decorators),
    server_args = list(decorators = decorators),
    ui = function(id, decorators) {
      ns <- NS(id)
      div(
        selectInput(ns("dataname"), label = "select dataname", choices = NULL),
        selectInput(ns("x"), label = "select x", choices = NULL),
        selectInput(ns("y"), label = "select y", choices = NULL),
        ui_transform_teal_data(ns("decorate"), transformators = decorators),
        plotOutput(ns("plot")),
        verbatimTextOutput(ns("text"))
      )
    },
    server = function(id, data, decorators) {
      moduleServer(id, function(input, output, session) {
        observeEvent(data(), {
          updateSelectInput(inputId = "dataname", choices = names(data()))
        })

        dataname <- reactive({
          req(input$dataname)
          input$dataname
        })

        observeEvent(dataname(), {
          req(input$dataname)
          updateSelectInput(inputId = "x", choices = colnames(data()[[dataname()]]))
          updateSelectInput(inputId = "y", choices = colnames(data()[[dataname()]]))
        })
        x <- reactive({
          req(input$dataname)
          input$x
        })
        y <- reactive({
          req(input$dataname)
          input$y
        })

        data_with_plot <- reactive({
          req(dataname(), x(), y())
          within(data(),
            {
              plot <- ggplot2::ggplot(dataname, ggplot2::aes(x = x, y = y)) +
                ggplot2::geom_point()
            },
            dataname = as.name(dataname()),
            x = as.name(x()),
            y = as.name(y())
          )
        })

        # Data without decorator has this error
        data_with_plot_decorated <- srv_transform_teal_data(
          "decorate",
          data = data_with_plot,
          transformators = list(static_decorator)
        )

        # Data without decorator does not have this error
        # data_with_plot_decorated <- data_with_plot

        plot_r <- reactive({
          data_with_plot_decorated()[["plot"]]
        })

        reproducible_code <- reactive({
          within(data_with_plot_decorated(), expr = plot) |>
            teal.code::get_code()
        })

        output$plot <- renderPlot(plot_r())
        output$text <- renderText(reproducible_code())
      })
    }
  )
}

static_decorator <- teal_transform_module(
  label = "Static decorator",
  server = function(id, data) {
    moduleServer(id, function(input, output, session) {
      reactive({
        within(data(), {
          plot <- plot +
            ggtitle("This is a better title") +
            xlab("the real x axis")
        })
      })
    })
  }
)
app <- init(
  data = teal_data(iris = iris, mtcars = mtcars),
  modules = modules(
    tm_decorated_plot("static", decorators = list(static_decorator))
  )
)

shinyApp(app$ui, app$server)

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.
@vedhav vedhav added bug Something isn't working core labels Feb 6, 2025
@vedhav vedhav self-assigned this Feb 6, 2025
@gogonzo
Copy link
Contributor

gogonzo commented Feb 9, 2025

Having NULL here is a consequence of this. I think replacing NULL return with silent.shiny.error makes sense.

vedhav added a commit that referenced this issue Feb 10, 2025
Closes #1485

---------

Signed-off-by: Vedha Viyash <[email protected]>
Co-authored-by: Dawid Kałędkowski <[email protected]>
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