shinymeta displays a strange code when using !! in mutate

I just discovered the shinymeta package and tried to use it on a simple app. In this app, you can create a new column which takes the lagged values of an existing column. The name of the new column depends on the input chosen which means I have to use !! and := in the mutate function.

However, when I try to display the code necessary to remake the modified table (with a new column), shinymeta seems confused and mixes everything inside mutate. The code given by shinymeta works but it is different of what I expected.

  • Code expected: mutate(!!lagged_name := lag(!!sym("mpg")))
  • Code obtained : mutate(`:=`(!!lagged_name, lag(!!sym("mpg"))))

Although the code obtained works, I'm worried it confuses the users when they try to see the code behind the shiny app. Does anyone know how to display a "more usual" code in this example?

PS: since shinymeta is quite new and not on CRAN yet, I don't know if I should post this here or as an issue on github

Reproducible example:

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

ui <- fluidPage(
  selectInput("choice", "Select a column", choices = c("mpg", "drat", "hp"), multiple = F),
  checkboxInput("lag", "Compute lag value"),
  outputCodeButton(tableOutput("table"), label = "Show code")
  )

server <- function(input, output, session) {
  
  data1 <- metaReactive({
    data <- head(mtcars) 
    data$time <- rep(seq(1:3)) 
    data$ID <- rep(c("A", "B"), each = 3)
    data
  })
  
  data2 <- metaReactive2({
    if (input$lag){
      metaExpr({
      lagged_name <- paste0(..(input$choice), "_lagged")
      ..(data1()) %>%
        select(ID, time, sym(..(input$choice))) %>%
        group_by(ID) %>%
        mutate(!!lagged_name := lag(!!sym(..(input$choice))))
      })
    }
    else {
      metaExpr({
      ..(data1()) %>%
        select(ID, time, sym(..(input$choice)))
      })
    }
  })
  
  output$table <- metaRender(renderTable, {
    ..(data2())
  })
  
  observeEvent(input$table_output_code, {
    code <-  expandChain(data1(), data2())
    displayCodeModal(code)
  })   

}

shinyApp(ui, server)

The reason behind the discrepancy is that := is an infix operator.
See: https://colinfay.me/playing-r-infix-functions/

I wonder if shinymeta pretty prints in a familiar order more common infix operators like = , like arithmetic +, - etc. If so there is probably an existing solution that I'd expect could be extended to generalise, otherwise there would probably be a great deal more work to get an enhancement up and running

1 Like

@nirgrahamuk thanks for your answer, but I don't really know what to do now: should I wait for improvements and continue with the obtained code, or should I open an issue on github? I'm not advanced enough with R to help in another way

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.