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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:48:41+00:00 2026-05-27T20:48:41+00:00

The substitute function in R creates a language object in the form of a

  • 0

The substitute function in R creates a language object in the form of a tree that one can parse. How can I create the tree from scratch using list or else to then give it to eval?

# substitute gives a tree representation of the expression
a=1; b=2;
e1 = substitute(a+2*b)
eval(e1)      #gives 5 as expected
e1            # is type language
e1[[1]]       # this is `+`
e1[[2]]       # this is 'a' type symbol
e1[[3]]       # this is type language
e1[[3]][[1]]  # this is `*`  etc....

I would like to know how I can reconstruct the e1 object programmatically. Ideally I create an object of intricated lists with the correct object in them and maybe I call some as.language on the list object. However that does not work. For instance:

# how to construct the tree?
eval(list(as.symbol('+'),1,1))                # does not return 2
eval(as.expression(list(as.symbol('+'),1,1))) # does not return 2

One way is to just generate the string ‘1+1’ and then parse it, but it does not seem elegant to generate strings to parse them again when you have the tree in the first place!

eval(parse(text='1+1')) # does return 1, but not elegant if tree is 
                        # large and already in memory 

Thanks for your help!

  • 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-27T20:48:41+00:00Added an answer on May 27, 2026 at 8:48 pm
    > plus <- .Primitive("+")
    > plus
    function (e1, e2)  .Primitive("+")
    > times=.Primitive("*")
    > eval(call("plus", b, call("times",2, b)))
    [1] 6
    > eval(call("plus", a, call("times",2, b)))
    [1] 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse some JSON object strings that I'm getting using gson-1.6.jar I
I'm using preg_replace_callback to substitute particular tokens within the string. But apart from actual
I want to create a function that will retry an expression if it fails.
I want to create a function that takes a dataset name and a package
I'm trying to create a user defined function in Oracle that will return a
I am looking to create a function that could toggle the ability to recurse
Suppose I am writing a substitute function on an abstract syntax tree data type:
I want to substitute build-in send_mail function, that works only with plain-text emails, with
I wonder if there exists any add-on for VS that can substitute/tag some lines
how to call a function from the substitute string in vim? When i have:

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.