Get Function Name as Character String in R (2 Examples)


The ability to convert function names to character strings is a powerful tool for advanced debugging and dynamic code analysis. In this tutorial, I will show two common ways to implement this conversion in the R programming language.

The included topics in this tutorial are as follows:

Let’s get started!


Example 1: Get the Function Name as a Character String via as.character() & substitute()

One way of changing the function name to a character string is to use the as.character() and substitute() functions together. For the demonstration, the method is applied to the all() function of the base() package of R.

allf_ex1 <- as.character(substitute(all))

The success of the transformation can be checked via the typeof() function, as shown below.

# [1] "character"

Please check now how the function name can be used as a string input in the paste() function.

report1 <- paste(allf_ex1,                       
               "() was successfully called.")  
# [1] "all() was successfully called."

The same code line wouldn’t work before this transformation. See below.

report <- paste(all,
              "() was successfully called.") 
# Error in paste(all, sep = "", "() was successfully called.") :
# cannot coerce type 'builtin' to vector of type 'character'


Example 2: Get the Function Name as a Character String via deparse() & substitute()

Another way of converting the function name into a string is to use the deparse() and substitute() functions together. The method is applied to the all() function for illustrative purposes like in the previous case.

allf_ex2 <- deparse(substitute(all))                                              
# [1] "character"

Please see how this conversion is put to use below.

report2 <- paste(allf_ex2,
               "() was successfully called.")
# [1] "all() was successfully called."

Incorporating these string conversion techniques into your R functions can greatly simplify debugging and enhance code clarity. This practice allows for effective function call tracking, making your programming more efficient and manageable.


Video & Further Resources

We have recently published a video on our YouTube channel, which explains the R syntax of this tutorial. Please find the video instruction below:


The YouTube video will be added soon.


Besides that, you might read the related articles on our website. You can find a selection of other tutorials on related topics such as character strings, data objects, text elements, and variables below.


You have learned in this article how to transform functions to strings in the R programming language. In case you have additional questions, let me know in the comments.


Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.