plotly / plotly/plotly.R

NSE Arguments in functions calling plotly raise an error

Ouverte
#2,249 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
R
Étoiles
2.7k
Forks
641
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez le problème avec l’exemple fourni nse_arg_raises_error, puis suivez plotly:::eval_attr jusqu’à lazyeval:::clone_env et à l’appel de base:::as.list.environment. Vérifiez comment le chemin NSE de plotly gère un environnement de fonction contenant un argument NSE inutilisé. C’est terminé lorsque l’appel plot_ly démontré ne génère plus l’erreur as.list.environment ; le payload ne nomme aucun fichier de test.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
r
Domaine
data-visualization
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.