Skip to content

Code chunk with shared expansionContext disappears when input changes #126

@willgearty

Description

@willgearty

I'm splitting parts of my shinymeta output into separate chunks; however, to avoid duplicate code, I'm having all of the chunks share the same expansionContext. Here's a small reproducible example cobbled together based on existing shinymeta examples:

library(shiny)
library(shinymeta)
library(dplyr)

ui <- fluidPage(
  selectInput("dataset", "Dataset", c("iris", "pressure")),
  varSelectInput("col_x", "column for x", NULL),
  varSelectInput("col_y", "column for y", NULL),
  verbatimTextOutput("code_1"),
  verbatimTextOutput("code_2"),
  verbatimTextOutput("code_3"),
  plotOutput("plot")
)

server <- function(input, output, session) {
  df <- metaReactive({
    get(..(input$dataset), "package:datasets")
  })
  
  observeEvent(df(), {
    updateVarSelectInput(session, "col_x", data = df())
    updateVarSelectInput(session, "col_y", data = df())
  })
  
  output$plot <- metaRender(renderPlot, {
    "# This is a helpful comment"
    ggplot(..(filtered())) +
      geom_point(aes(x = !!..(input$col_x), y = !!..(input$col_y)))
  })
  
  filtered <- metaReactive({
    ..(df()) %>% distinct()
  })
  
  exp_ctx <- newExpansionContext()
  output$code_1 <- renderPrint({
    expandChain(.expansionContext = exp_ctx,
                quote(library(magrittr)),
                quote(library(ggplot2))
    )
  })
  output$code_2 <- renderPrint({
    expandChain(.expansionContext = exp_ctx,
                invisible(filtered())
    )
  })
  output$code_3 <- renderPrint({
    expandChain(.expansionContext = exp_ctx,
                output$plot()
    )
  })
}

shinyApp(ui, server)

This app loads fine and the chunks look great:

Image

However, when you then change the selected dataset, the middle chunk just disappears:

Image

Any thoughts? Am I missing something in the way I'm implementing the expansionContext?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions