plotly / plotly/plotly.R

NSE Arguments in functions calling plotly raise an error

Aperta
#2,249 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
R
Stelle
2.7k
Fork
641
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

While toying with different NSE approaches I stumbled upon the following behaviour which seems to me like a bug. Any NSE argument (even if not used in plotly) results in an error:

library(plotly)

nse_arg_raises_error <- function(xx) {
   mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
   plot_ly(mtcars, x = ~ cyl, y = ~ mpg, type = "scatter",
           mode = "markers")
}
nse_arg_raises_error()    ## works
nse_arg_raises_error(cyl) ## does not work
#> Error in as.list.environment(x, all.names = TRUE): object 'cyl' not found

This behaviour is unique to plotly, b/c if I leave out the plot_ly call, the error does not happen.

it_is_a_plotly_problem <- function(xx) {
   mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
   1 + 1
}
it_is_a_plotly_problem()     ## works
#> [1] 2
it_is_a_plotly_problem(cyl)  ## also works
#> [1] 2

However, some preliminary debugging shows that the culprit sits (deep) in base:::as.list.environment (which is called from lazyeval:::clone_env, which in turn together with its brethren from lazyeval is responsible for the NSE part in plot_ly)

it_is_a_as_list_environment_issue <- function(xx) {
   mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
   cp <- as.list.environment(environment())
   1 + 1
}
it_is_a_as_list_environment_issue(cyl)
#> Error in as.list.environment(x, all.names = TRUE): object 'cyl' not found

So basically, when lazyeval:::clone_env copies the function environment which contains an NSE argument this error from within the C code implementation of base::as.list.environment bubbles up.

I am inclined to say that this a feature and not a bug of as.list.environment and the user of it should make sure that environments using it are in fact able to be safely transformed into a list. Thus, I am posting the issue here as it touches the whole NSE strategy of plotly - coming from plotly:::eval_attr (especially since lazyeval is retired in favor of rlang).

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il problema con l’esempio fornito nse_arg_raises_error, quindi segui plotly:::eval_attr fino a lazyeval:::clone_env e alla chiamata a base:::as.list.environment. Verifica come il percorso NSE di plotly gestisce un ambiente di funzione contenente un argomento NSE inutilizzato. Il lavoro è completato quando la chiamata plot_ly mostrata non genera più l’errore as.list.environment; il payload non indica alcun file di test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
r
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.