rstudio / shinymeta

Record and expose Shiny app logic using metaprogramming
https://rstudio.github.io/shinymeta
223 stars 14 forks source link

expandChain breaks when code contains a `parse()` statement #81

Open daattali opened 4 years ago

daattali commented 4 years ago

Minimal example:

library(shinymeta)
input <- list(title = "paste(5, alpha)")
code <- metaExpr(parse(text = input$title))
expandChain(code)

Results in an error

Error: <text>:1:50: unexpected '<'
1: structure(expression(paste(5, alpha)), srcfile = <
                                                     ^

Example in a shiny app:

library(shiny)
library(ggplot2)
library(shinymeta)

ui <- fluidPage(
  textInput("text", "text", "paste(text, alpha)"),
  checkboxInput("parse", "Parse axis", FALSE),
  plotOutput("plot"),
  actionButton("code", "Show Code")
)

server <- function(input, output, session) {
  output$plot <- metaRender2(renderPlot, {

    xtitle <- if (input$parse) parse(text = input$text) else input$text

    metaExpr({
      ggplot(mtcars, aes(mpg,wt)) +
        geom_point() +
        xlab(..(xtitle))
    })
  })

  observeEvent(input$code, {
    code <- expandChain(output$plot())
    displayCodeModal(code)
  })
}

shinyApp(ui, server)
daattali commented 4 years ago

Note that it's possible to bypass the error by replacing parse(text = ...) with str2expression(...) but that's only a workaround for my situation and not a fix

cpsievert commented 4 years ago

Another workaround is to set parse(..., keep.source = FALSE) (it appears that styler is choking on the srcref)