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

  • Home
  • SEARCH
  • 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 6902085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:45:16+00:00 2026-05-27T07:45:16+00:00

I would like to write a wrapper around a custom function that takes some

  • 0

I would like to write a wrapper around a custom function that takes some vectors as input (like: mtcars$hp, mtcars$am etc.) to take input as data frame name (as data parameter, eg.: mtcars) and variable names (like: hp and am), as usual in most standard function.

But I have some problems, my proposed ‘demo’ function (a wrapper around mean does not work.

Code:

f <- function(x, data=NULL) {
    if (!missing(data)) {
        with(data, mean(x))
    } else {
        mean(x)
    }
}

Running against a vector works of course:

> f(mtcars$hp)
[1] 146.69

But with fails unfortunatelly:

> f(hp, mtcars)
Error in with(d, mean(x)) : object 'hp' not found

While in global environment/without my custom function works right:

> with(mtcars, mean(hp))
[1] 146.69

I have tried to do some experiment with substitute, deparse and others, but without any success. Any hint would be welcomed!

  • 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-27T07:45:16+00:00Added an answer on May 27, 2026 at 7:45 am

    Here’s the key piece of the puzzle:

    f <- function(x,data=NULL) {
      eval(match.call()$x,data) # this is mtcars$hp, so just take the mean of it or whatever
    }
    
    > f(hp,mtcars)
     [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52  65  97 150 150 245 175  66
    [27]  91 113 264 175 335 109
    
    # it even works without a data.frame specified:
    > f(seq(10))
     [1]  1  2  3  4  5  6  7  8  9 10
    

    See @Andrie’s link to @Hadley’s document for an explanation of why it works. See @Hadley’s note for a critical caveat: f() cannot be run from inside another function.

    Basically R uses lazy evaluation (e.g. it doesn’t evaluate things until they’re actually used). So you can get away with passing it hp because it remains an unevaluated symbol until it appears somewhere. Since match.call grabs it as a symbol and waits to evaluate it, all is well.

    Then eval evaluates it in the specified environment. According to ?eval, the second argument represents:

    The environment in which expr is to be evaluated. May also be NULL, a
    list, a data frame, a pairlist or an integer as specified to sys.call.

    Therefore you’re in good shape with either NULL (if you’re not passing a data.frame) or a data.frame.

    Proof of lazy evaluation is that this doesn’t return an error (since x is never used in the function):

    > g <- function(x) {
    +   0
    + }
    > g(hp)
    [1] 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a plug-in that will allow a custom written CRM
I would like to write some scripts in python that do some automated changes
I'm trying to write some wrapper class or function that allows me to execute
I would like to write some data to a file in Ruby. What is
I would like to write a program that will identify a machine( for licensing
I would like to write a utility that will provide me with a relatively
I would like to write an application that will copy MP3 files to a
Possible Duplicate: .NET file system wrapper library I would like to write a test
I'd like to write a simple wrapper for the python list type that forces
I would like to write applications that use C++ and the Cocoa frameworks because

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.