Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Adding labels to points plotted on world map in R
Next article icon

Adding an element to a call object in R

Last Updated : 11 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In R Programming Language a call object represents an unevaluated function call. This type of object is a language construct that captures the function name and its arguments in a way that can be manipulated programmatically before actually executing the function. Call objects are typically used in metaprogramming, where you want to construct, inspect, or modify code dynamically.

Understanding Call Objects

A call object in R Programming Language can be created using the call or quote functions. Here’s a basic example:

R
# Create a call object my_call <- call("sum", 1, 2, 3) my_call 

Output:

sum(1, 2, 3)

This call object represents the unevaluated call to sum(1, 2, 3).

Adding an Element to a Call Object

To add an element to a call object, you need to modify it by either replacing existing arguments or appending new ones. Since a call object is a type of list in R, you can manipulate it using list-like operations.

1. Using Base R Manipulation

You can directly manipulate the call object as if it's a list. Here's how you can add an argument to the call object created above:

R
# Create a call object for the sum function my_call <- call("sum", 1, 2, 3)  # Display the original call print("Original call:") print(my_call)  # Add a new argument to the call object my_call[[length(my_call) + 1]] <- 4  # Display the modified call print("Modified call with additional argument:") print(my_call) 

Output:

[1] "Original call:"
sum(1, 2, 3)

[1] "Modified call with additional argument:"
sum(1, 2, 3, 4)

2. Using the append Function

Another way to add elements is by using the append function, which is more readable:

R
# Create a call object my_call <- call("sum", 1, 2, 3)  # Use append to add an element my_call <- as.call(append(as.list(my_call), list(4)))  # Print the updated call print(my_call) 

Output:

sum(1, 2, 3, 4)

This method converts the call to a list, appends the new element, and then converts it back to a call object.

Advanced method to Building a Call

Suppose you are writing a function that constructs a dynamic call to a statistical function, and you need to add arguments based on certain conditions:

R
# Function to create a dynamic call to mean() create_mean_call <- function(data, na_rm = FALSE) {   # Start with the basic call   call_obj <- call("mean", data)      # Conditionally add na.rm argument   if (na_rm) {     call_obj <- as.call(append(as.list(call_obj), list(na.rm = TRUE)))   }      return(call_obj) }  # Use the function dynamic_call <- create_mean_call("data_vector", na_rm = TRUE) print(dynamic_call) 

Output:

sum(1, 2, 3, 4)

mean("data_vector", na.rm = TRUE)

This function constructs a call to mean that dynamically includes the na.rm argument only if requested.

Conclusion

Manipulating call objects in R allows for flexible and dynamic function calls, which is particularly useful in advanced programming scenarios such as package development or dynamic statistical modeling. By treating call objects as lists, you can easily modify or extend them as needed.


Next Article
Adding labels to points plotted on world map in R

P

poojashu00qn
Improve
Article Tags :
  • R Language
  • R Basics

Similar Reads

  • Adding elements in a vector in R programming - append() method
    append() method in R programming is used to append the different types of integer values into a vector in the last. Syntax: append(x, value, index(optional)) Return: Returns the new vector after appending given value. Example 1: x <- rep(1:5) # Using rep() method gfg <- append(x, 10) print(gfg
    1 min read
  • Create an Object of mode call in R Programming - call() Function
    call() function in R Language is used to create or test for objects of mode "call". Syntax: call(name, ...) Parameters: name: a non-empty character string naming the function to be called ...: arguments to be part of the call Example 1: # R program to illustrate # call function # Calling call() func
    1 min read
  • Check if an Object is a Call in R Programming - is.call() Function
    is.call() function in R Language is used to determine whether x is a call or not. Syntax: is.call(x) Parameters: x: an arbitrary R object Example 1: # R program to illustrate # is.call function # Calling is.call() function is.call(call) Output: [1] FALSE Example 2: # R program to illustrate # is.cal
    1 min read
  • How to Create, Access, and Modify Vector Elements in R ?
    In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements. Creating a vectorIt can be done in these ways: Using c() function.Using: operator.Using the seq
    5 min read
  • Adding labels to points plotted on world map in R
    In this article, we are going to see how to add labels to points plotted on the world map in R Programming Language.  Method 1: Using maps package Maps: The "maps" package in R is used to draw and display geographical maps. It contains various databases for denoting countries, continents and seas. T
    3 min read
  • Explain call() and apply() methods in JavaScript
    Call() Method: The call method is basically used to invoke the function with different this object. In JavaScript, this refers to an object. It depends on how we are calling a particular function. In the global scope, this refers to the global object window. Inside function also this refers to the g
    3 min read
  • Convert an Object to Data Frame in R Programming - as.data.frame() Function
    as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters:  object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic exam
    2 min read
  • How to add a specific point to legend in ggvis in R?
    In this article, we will be going through the approach to add a specific point to a legend in ggvis package in the R programming language.  The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. T
    5 min read
  • Adding Data Points to world map in R
    In this article, we are going to see how to add DataPoint to the world map in R Programming Language. The world map in R can be plotted on a graphical device using various external packages in R Programming. The points can be marked and data can be annotated in these plots using various methods.  Da
    2 min read
  • R - Creating, Listing, and Deleting Objects in Memory
    One of the most interesting facts about R is, what is known as objects in R are known as variables in many other programming languages. Depending on the context objects and variables can have drastically different meanings. In every computer language variables provide a means of accessing the data s
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences