R 4 Quick Syntax Reference by Margot Tollefson

R 4 Quick Syntax Reference by Margot Tollefson

Author:Margot Tollefson
Language: eng
Format: epub
ISBN: 9781484279243
Publisher: Apress


source(

"ch9.dg2.txt"

)

cat(

"function ( x = 1:3 )
print( x )",

file="ch9.dg3.txt"

)

system(

"cat ch9.dg3.txt"

)

ch9.dg3=dget(

"ch9.dg3.txt"

)

ch9.dg3

ch9.dg3()

ch9.sc3 = source(

"ch9.dg3.txt"

)

ch9.sc3

ch9.sc3$value()

cat(

"function( x=4 )

print( round( rnorm( x ), 2 ) )",

file="ch9.dg4.txt"

)

system(

"cat ch9.dg4.txt"

+ )

dget(

"ch9.dg4.txt",

keep.source=TRUE

)

dget(

"ch9.dg4.txt"

)

Note that in the first example, the text is entered at the console. In the first part of the example, the text is parsed and evaluated. In the second part, a function is entered and assigned to an object, so the text is parsed and evaluated, but the function is not run.

In the second example, dget() and source() read from the ch9.dg2.txt file on the computer, which is parsed and evaluated. Numbers are returned, since the text in the file is not a function definition.

In the third example, a function that is stored in the ch9.dg3.txt file on the computer is first read by dget() and saved to the ch9.dg3 object. Then ch9.dg3() is output and run. A vector of numbers is returned when the ch9.dg3() is run. Next, the file is read by source(). The result (which is a list) is saved to the ch9.sc3 object. The list is output, and the value element of the list (which contains the function) is run. The same vector of numbers is returned as with dget(). Note that dget() and source() give different results when the object that is read is a function definition.

In the fourth example, a function in a file on the computer is read with keep.source equal to TRUE, then with keep.source equal to FALSE (the default value). The first call returns the function as written. The second call simplifies the format of the code.

For more information about dget() and source(), use the RStudio Help tab or enter ?dget and ?source, respectively, at the R prompt.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.