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

rlang interferes with dcast ...~metric API #4913

Closed
l-jaye opened this issue Feb 20, 2021 · 5 comments
Closed

rlang interferes with dcast ...~metric API #4913

l-jaye opened this issue Feb 20, 2021 · 5 comments

Comments

@l-jaye
Copy link

l-jaye commented Feb 20, 2021

Hello!

I am not sure if this is a Shiny issue, or a data.table issue, but a recent update to either package causes an error Error in check_formula: '...' used in an incorrect context when using ... in a formula for dcast.data.table() inside a shiny app. Outside of the shiny app, no issue. Please see a reproducible example below:

library(data.table)
library(shiny)

shinyApp(
  
  ui = fluidPage(
    actionButton("do.btn","Go!")
  ),
  
  server = function(input, output){
    
    observeEvent(input$do.btn, {
      
      metrics = c("col.x","col.y")
      dt = data.table(x = 1:10, y = "test", col.x = 3, col.y = 4)
      melt.dt = melt(dt, measure.vars = metrics, variable.name = "metric")
      dt = dcast(melt.dt, ... ~ metric, value.var = "value", fill = NA) # this throws error
      
    })
    
  }
)
@l-jaye
Copy link
Author

l-jaye commented Feb 20, 2021

Update:

Replacing ... ~ metric with as.formula("...~metric") works:

library(data.table)
library(shiny)

shinyApp(
  
  ui = fluidPage(
    actionButton("do.btn","Go!")
  ),
  
  server = function(input, output){
    
    observeEvent(input$do.btn, {
      
      metrics = c("col.x","col.y")
      dt = data.table(x = 1:10, y = "test", col.x = 3, col.y = 4)
      melt.dt = melt(dt, measure.vars = metrics, variable.name = "metric")
      
      dt = dcast(melt.dt, as.formula("...~metric"), value.var = "value", fill = NA)
      print(dt)
      
    })
    
  }
)

@avimallu
Copy link
Contributor

avimallu commented Feb 27, 2021

From the linked issue, it looks like this is not something that can be fixed on data.table's end, correct? The comment indicates that it's an interaction between rlang and data.table. Might want to close if my interpretation is accurate.

@ColeMiller1
Copy link
Contributor

ColeMiller1 commented Feb 28, 2021

I would leave it open in case we would want to provide an additional API as a workaround. Maybe .SD ~ metric which would not interfere with rlang - I know it took me awhile to remember that all variables was ....

Also note, it's not interaction between rlang and data.table. It's rlang and base R, it's just that data.table NSE happens to use the .... Thanks to the shiny maintainers for helping isolate the issue.

rlang::eval_tidy(...~x)
## Error in rlang::eval_tidy(... ~ x) : '...' used in an incorrect context

As far as workarounds, quoting just the formula works. E.g.

dt = dcast(melt.dt, "...~metric", value.var = "value", fill = NA)

@ColeMiller1 ColeMiller1 changed the title Recent update causes error in data.table dcast() in Shiny rlang interferes with dcast ...~metric API Feb 28, 2021
@lionel-
Copy link

lionel- commented Mar 1, 2021

This is a bug in rlang that we will fix on our side.

@MichaelChirico
Copy link
Member

Thanks Lionel! Closing this side then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants