Get Function Name as Character String in R (2 Examples)
For several reasons, one might be interested in learning how to convert functions to character strings . In this tutorial, I will show two common ways to implement it 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.
typeof(allf_ex1) # [1] "character"
Please check now how the function name can be used as a string input in the paste() function.
report1 <- paste(allf_ex1, sep="", "() is one of the functions of the base package.") report1 # [1] "all() is one of the functions of the base package."
The same code line wouldn’t work before this transformation. See below.
report <- paste(all, sep="", "() is one of the functions of the base package.") report # Error in paste(all, sep = "", "() is one of the functions of the base package.") : # 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)) typeof(allf_ex2) # [1] "character"
Please see how this conversion is put to use below.
report2 <- paste(allf_ex2, sep="", "() is one of the functions of the base package.") report2 # [1] "all() is one of the functions of the base package."
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.
- Convert Name of Data Object to Character String
- Import Text File as Single Character String
- Convert Character String to Variable Name in R
- Get Frequency of Words in Character String
- All R Programming Examples
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.
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.
Statistics Globe Newsletter