Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Packages: Only the standard packages, coming with a default installation of the R application, are allowed. Project s description This project is about analyzing a
Packages: Only the standard packages, coming with a default installation of the R application, are allowed.
Projects description
This project is about analyzing a function definition. More precisely, it is about taking a functions definition and returning a data frame with two columns. The first one contains all symbols used in the functions definition, and the second one has either the environments name containing the definition of the symbol or a description of the symbols type, eg locally defined, given as argument, etc.
Technically, you must define a function listSymbols taking a single argument f and returning a data frame. It is assumed that the function f is a closure not a builtin, as those are compiled functions and we cannot see their code from the level of pure R language The following examples present the particular functionalities of the required function.
Example
Let us start with a straightforward example. First, the function is defined, and then we list all symbols used in defining the function.
g function
printHello world!"
g
"Hello world!"
When we run the function, it prints the string "Hello world!" to the console. There are two symbols used in the function's definition. One is print, and the other is Both symbols are defined in the package base. We can see it using the function listSymbols as shown in the following snippet.
listSymbolsf g
symbol definition
base
print base
The output of the function is a data frame containing two columns. The first column shows symbols, while the second column shows an environment where the symbol is defined. Here, both symbols are defined in the base package.
Example
Let's introduce another type of symbol used in defining a function, namely, arguments. Here is a similar function to the previous one, taking a single argument.
g functions
prints
gHello world!"
"Hello world!"
There are three symbols used in the definition of the above function. The first two are the same as before, but the new one is s This symbol is defined as an argument. We can see it below using the project's function.
listSymbolsf g
symbol definition
base
print base
s arg
In the output, symbol s is designated a definition as arg, meaning it is defined as an argument.
Example
Beyond symbols defined in other environments or as arguments, we can have symbols defined locally. Here is an example of such a definition.
g functions
x
prints
gHello world!"
"Hello world!"
We can see this new type of symbol by applying the listSymbols function.
listSymbolsf g
symbol definition
base
base
print base
s arg
x locally defined
Five symbols are used to define the function g In addition to the previous ones, we have assignment and a variable x The variable x is designated as "locally defined. However, while dealing with locally defined variables, you should exercise caution. Examine the following function.
g functions
x y
prints
gHello world!"
"Hello world!"
There are two locally defined variables: x and y They are defined using a single statement. Even in this case, the function listSymbols should correctly list these symbols.
listSymbolsf g
symbol definition
base
base
print base
s arg
x locally defined
y locally defined
Example
The last type of symbol used in function definitions is a temporary symbol. These are symbols used to iterate over. Here is a simple example.
g functions n
for j in :n
prints
gHello world!",
"Hello world!"
"Hello world!"
"Hello world!"
"Hello world!"
"Hello world!"
There are seven symbols used in the definition of the above function. Note the symbol j used as an iterator. In the following snippet, the data frame returned by the listSymbols function describes it as the "temp symbol" as it is not explicitly defined or passed as an argument.
listSymbolsf g
symbol definition
: base
base
for base
j temp symbol
n arg
print base
s arg
Example
Finally, it is possible to have symbols that are none of the above. These symbols are not defined; if the function is actually called, it will return an error. Consider the following function.
g function
printy
g
Error in printy : object y not found
listSymbolsf plot.default
e new.envparent globalenv
e$y "Hello from the new environment"
environmentg e
listSymbolsg
symbol definition
base
print base
y
listSymbolsf plot.default
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started