Answered step by step
Verified Expert Solution
Link Copied!

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, e.g., 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 1
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(){
print("Hello world!")
}
g()
[1] "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.
listSymbols(f = g)
symbol definition
1{ base
2 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 2
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 <- function(s){
print(s)
}
g("Hello world!")
[1] "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.
listSymbols(f = g)
symbol definition
1{ base
2 print base
3 s arg
In the output, symbol s is designated a definition as arg, meaning it is defined as an argument.
Example 3
Beyond symbols defined in other environments or as arguments, we can have symbols defined locally. Here is an example of such a definition.
g <- function(s){
x <-5
print(s)
}
g("Hello world!")
[1] "Hello world!"
We can see this new type of symbol by applying the listSymbols() function.
listSymbols(f = g)
symbol definition
1{ base
2<- base
3 print base
4 s arg
5 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 <- function(s){
x <- y <-5
print(s)
}
g("Hello world!")
[1] "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.
listSymbols(f = g)
symbol definition
1{ base
2<- base
3 print base
4 s arg
5 x locally defined
6 y locally defined
Example 4
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 <- function(s, n){
for (j in 1:n){
print(s)
}
}
g("Hello world!", 5)
[1] "Hello world!"
[1] "Hello world!"
[1] "Hello world!"
[1] "Hello world!"
[1] "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.
listSymbols(f = g)
symbol definition
1 : base
2{ base
3 for base
4 j temp symbol
5 n arg
6 print base
7 s arg
Example 5
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(){
print(y)
}
g()
Error in print(y) : object 'y' not found
listSymbols(f = plot.default)
e <- new.env(parent = globalenv())
e$y <- "Hello from the new environment"
environment(g)<- e
listSymbols(g)
symbol definition
1{ base
2 print base
3 y
listSymbols(f = plot.default)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

=+4. Does the source have the ability to investigate this audience?

Answered: 1 week ago

Question

Use FreeMind to brainstorm a mind map for your smart home system

Answered: 1 week ago

Question

explain the concept of strategy formulation

Answered: 1 week ago