Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:56+00:00 2026-05-25T13:07:56+00:00

I’m trying to convince an S4 method to use an expression as an argument,

  • 0

I’m trying to convince an S4 method to use an expression as an argument, but I always get an error returned. A trivial example that illustrates a bit what I’m trying to do here :

setGeneric('myfun',function(x,y)standardGeneric('myfun'))

setMethod('myfun',c('data.frame','expression'),
          function(x,y) transform(x,y) )

If I now try :

> myfun(iris,NewVar=Petal.Width*Petal.Length)
Error in myfun(iris, NewVar = Petal.Width * Petal.Length) : 
  unused argument(s) (NewVar = Petal.Width * Petal.Length)

> myfun(iris,{NewVar=Petal.Width*Petal.Length})
Error in myfun(iris, list(NewVar = Petal.Width * Petal.Length)) : 
 error in evaluating the argument 'y' in selecting a method for 
 function 'myfun': Error: object 'Petal.Width' not found

It seems the arguments are evaluated in the generic already if I understand it right. So passing expressions down to methods seems at least tricky. Is there a possibility to use S4 dispatching methods using expressions?


edit : changed to transform, as it is a better example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:07:56+00:00Added an answer on May 25, 2026 at 1:07 pm

    You’ve specified “expression” as the class of the second argument in this example method. The first example returns an error because

    NewVar=Petal.Width*Petal.Length
    

    is being interpreted as a named argument to myfun with value

    Petal.Width*Petal.Length
    

    that doesn’t get the chance to be evaluated, because NewVar isn’t an argument for this method or generic.

    In the second example, I’m not sure what is going on with the closed curly braces, as my error differs from the one shown:

    Error in myfun(iris, { :
    error in evaluating the argument ‘y’ in selecting a method for function ‘myfun’: Error: object ‘Petal.Width’ not found

    However, I receive no error and get the iris data.frame as output when I force your expression to be an expression object:

    myfun(iris, expression(NewVar=Petal.Width*Petal.Length))
    

    I think this only partially answers your question, because trivially returning the iris data.frame was not what you wanted. The expression is not being evaluated properly by transform(). I suspect you want the output to match exactly the output from the following hard-coded version:

    transform(iris, NewVar=Petal.Width*Petal.Length)
    

    Here is a short example evaluating the expression using eval

    z <- expression(NewVar = Petal.Width*Petal.Length)
    test <- eval(z, iris)
    head(test, 2)
    

    [1] 0.28 0.28

    Here is a version that works for adding one variable column to the data.frame:

    setGeneric('myfun',function(x,y)standardGeneric('myfun'))
    setMethod('myfun',c('data.frame','expression'), function(x,y){
        etext <- paste("transform(x, ", names(y), "=", as.character(y), ")", sep="")
        eval(parse(text=etext))
    })
    ## now try it.
    test <- myfun(iris, expression(NewVar=Petal.Width*Petal.Length))
    names(test)
    

    [1] “Sepal.Length” “Sepal.Width” “Petal.Length” “Petal.Width” “Species” “NewVar”

    head(test)
    
        Sepal.Length Sepal.Width Petal.Length Petal.Width Species NewVar
    1          5.1         3.5          1.4         0.2  setosa   0.28
    2          4.9         3.0          1.4         0.2  setosa   0.28
    

    Again, this implementation has essentially hard-coded that one, and only one, variable column will be added to the input data.frame, although the name of that variable column and the expression are arbitrary, and provided as an expression. I’m certain there is a better, more general answer that would evaluate the expression held in y as if it were a direct call in the transform() function, but I’m stumped at the moment what to use as the appropriate “inverse” function to expression( ).

    There is always the standard … , if you don’t actually want to dispatch on y:

    setGeneric('myfun', function(x, ...) standardGeneric('myfun'))
    setMethod('myfun', 'data.frame', function(x, ...){
        transform(x, ...)
    })
    

    And this works great. But your question was about actually dispatching on an expression object.

    The following does not work, but I think it is getting closer. Perhaps someone can jump in and make some final tweaks:

    setGeneric('myfun', function(x, y) standardGeneric('myfun'))
    setMethod('myfun',c('data.frame', 'expression'), function(x, y){
        transform(x, eval(y, x, parent.frame()))
    })
    ## try out the new method
    z <- expression(NewVar = Petal.Width*Petal.Length)
    test <- myfun(iris, z)
    names(test)
    

    [1] “Sepal.Length” “Sepal.Width” “Petal.Length” “Petal.Width” “Species”

    Essentially, the “NewVar=” piece of the expression was not passed to transform() when we called myfun().

    After much trial and error, I figured out a way that works for real. First convert the expression object into a list with as.list(), then build up the call that you want with the marvelous

    do.call()
    

    The complete example looks like this:

    setGeneric('myfun', function(x, y) standardGeneric('myfun'))
    setMethod('myfun',c('data.frame', 'expression'), function(x, y){
        do.call("transform", c(list(x), as.list(y)))
    })
    # try out the new method
    z <- expression(NewVar = Petal.Width*Petal.Length)
    test <- myfun(iris, z)
    names(test)
    [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
    [6] "NewVar"
    

    And the new data.frame object “test” has the “NewVar” column we wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.