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

Error with enquo: the argument has already been evaluated #300

Closed
jrosen48 opened this issue Nov 27, 2017 · 4 comments
Closed

Error with enquo: the argument has already been evaluated #300

jrosen48 opened this issue Nov 27, 2017 · 4 comments

Comments

@jrosen48
Copy link

@jrosen48 jrosen48 commented Nov 27, 2017

I am trying to use enquo in a summary() generic function. I'm having some trouble reproducing this - I know this is a red flag - but perhaps there is something missing about how I understand how this function works that someone can address.

A function in a package I'm developing outputs a list such as the following list, with three elements, the first a tibble, and the second and third ggplot2 plot objects:

> x
$df
# A tibble: 1 x 8
         action   inference percent_bias_to_change_inference
          <chr>       <chr>                            <dbl>
1 to_invalidate reject_null                           60.557
# ... with 5 more variables: replace_null_cases <dbl>,
#   unstd_beta <dbl>, beta_threshhold <dbl>,
#   omitted_variable_corr <dbl>, itcv <dbl>

$thresh_plot

$corr_plot

attr(,"class")
[1] "konfound"

My goal is to use the name of the object as a string; I'm trying to use a function like the following to walk people through how to print each of the three list elements, but this fails on the 1st line - with rlang::enquo(x):

summary.konfound <- function(konfound_object){
    x_quo <- rlang::enquo(konfound_object)
    x_text <- rlang::quo_text(x_quo)

    cat("Created", length(konfound_object), "forms of output. To access type: \n")
    cat("\n")

    for (name in names(konfound_object)) {
        cat(x_text, "$", name, "\n", sep = "")
        # cat(name, "\n")
    }
}

When I run summary, as in summary(x), the error is:

Error in (function (x, strict = TRUE)  : 
  the argument has already been evaluated

It does not seem to me that the argument is evaluated until the length() function on the function's 7th line. Can you help me to understand why I cannot use rlang::enquo() on the name of the object passed to summary()?

There are a number of Stack Overflow questions on this topic (here, here, and here), but their answers were either inconclusive or did not seem pertinent in this case.

@jrosen48 jrosen48 changed the title Error when using enquo with list output: the argument has already been evaluated Error with enquo: the argument has already been evaluated Nov 27, 2017
@lionel-
Copy link
Member

@lionel- lionel- commented Nov 27, 2017

Already fixed in branch #297, duplicate of #295.

Note that while you won't get an error, you will capture the evaluated argument rather than its expression. This is not good for labelling. The mechanism for capture is tied to lazy evaluation so if you force an argument it is no longer possible to capture it.

In this case the argument has been forced because it needs to be evaluated in order to see what class it has and dispatch appropriately. If you just want a label, you don't need quosures anyway so you can just use expr_text(substitute(x)) or expr_label(substitute(x)).

@hadley I wonder if we need enlabel() and entext() functions for this sort of things?

@jrosen48
Copy link
Author

@jrosen48 jrosen48 commented Nov 27, 2017

thanks, expr_text(substitute(x)) did the trick (I also installed the development version). I'm not sure if this means I'm using an unconventional pattern, but I do find myself doing this (trying to both evaluate an argument and use it as a string / as text).

@hadley
Copy link
Member

@hadley hadley commented Nov 28, 2017

@lionel- hmmmm interesting thought. Maybe create an issue to discuss? (There are two scenarios: where you need quasiquotation for some other purpose, and where you don't)

@lionel-
Copy link
Member

@lionel- lionel- commented Dec 5, 2017

This is now fixed on master.

@lionel- lionel- closed this Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants