plotly / plotly/plotly.R

NSE Arguments in functions calling plotly raise an error

Offen
#2,249 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
R
Sterne
2.7k
Forks
641
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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).

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem mit dem bereitgestellten Beispiel nse_arg_raises_error und verfolge dann plotly:::eval_attr bis zu lazyeval:::clone_env und dem Aufruf von base:::as.list.environment zurück. Prüfe, wie der plotly-NSE-Pfad mit einer Funktionsumgebung umgeht, die ein unbenutztes NSE-Argument enthält. Als erledigt gilt die Aufgabe, wenn der demonstrierte plot_ly-Aufruf nicht mehr den as.list.environment-Fehler auslöst; der Payload nennt keine Testdatei.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.