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 9193005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:03:40+00:00 2026-06-17T21:03:40+00:00

I would like to write a function that takes any user-provided mathematical function (e.g.,

  • 0

I would like to write a function that takes any user-provided mathematical function (e.g., x^2) and do different things with it, for example:

#-----------------nonworking code---------------------
foo <- function(FUN, var){
      math_fun <- function(x){
           FUN
      }
   curve(math_fun, -5, 5)     #plot the mathematical function
   y = math_func(var)         #compute the function based on a user provided x value.
   points(x=var, y=y)         #plot the value from the last step.
}

#A user can use the function defined above in a way as shown below:
Function <- x^2 + x
foo(FUN=Function, var = 2)

But obviously this function doesn’t work:
First of all, if I run this function, I get Error in math_fun(x) : object 'x' not found.

Second of all, even if the function did work, I am assuming that the variable is x, but the user can make use of any letter.

For this second problem, one potential solution is to also ask the user to specify the letter they use as the variable.

foo <- function(FUN, var, variable){
      math_fun <- function(variable){
           FUN
      }
   curve(math_fun, -5, 5) 
   y = math_func(var)     
   points(x=var, y=y)     
}

But I am at loss as to how exactly I can implement this… If someone can help me solve at least part of the problem, that would be great. Thanks!

  • 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-06-17T21:03:41+00:00Added an answer on June 17, 2026 at 9:03 pm

    It is a lot simpler than that. The user defined function should contain the arguments in its definition, e.g. function(x) x^2 + x instead of x^2 + x. Then it can be passed and called directly:

    foo <- function(math_fun, var){
    
       curve(math_fun, -5, 5)  #plot the mathematical function
       y = math_fun(var)       #compute the function based on a user provided x value
       points(x=var, y=y)      #plot the value from the last step.
    }
    
    #A user can use the function defined above in a way as shown below:
    Function <- function(x) x^2 + x
    foo(Function, var = 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a function that would have some optional code to be
I would like to write a wrapper around a custom function that takes some
I would like to write a function that takes in 3 characters and increments
I would like to write a function that creates a plot of a set
The following is for Python 3.2.3. I would like to write a function that
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
In Python, I'd like to write a function that would pretty-print its results to
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write a small tool that takes a C++ program (a
I would like to write an OCaml function which takes a URL and returns

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.