R Flashcards
How to view a function’s code?
Press F2 with cursor on a function, or use View(function)
What function is used to check if a name is appropriate/valid to assign to a new R package?
available::available(“my_package_name”)
What tool is used to create documentation for the R package?
“Roxygen” - turn it on under Build - More - Configure Build Tools. To start, pur the curser on a function and go to Code - insert Roxygen skeleton.
Roxygen generate the .Rd file and the NAMESPACE file for you
Open Source Licenses - Apache, MIT, GPL
GPL-2, GPL-3
What is vignette in R
TBD
What function is used to join a set of strings without adding space in between?
paste0(“my name is “, my_name)
What’s the purpose of the NAMESPACE file in the package folder?
It outlines functions imported from other packages for users to be able to use, and functions you wrote available for people who loads the package to use
- export(function_name)
What are S3, S4 classes/methods in R?
In R, classes/methods are different from what it is in the traditional OOP sense. S3 and S4 in R are a way to implement ploymorphism for static functions. They are dispatching schemes.
- S3 is the old-style, more informal and easier to implement (quick and dirty). S4 is newer, more rigorous.
Quote: “If you really need inheritance and more complicated polymorphism, you probably want to look into S4 classes and Reference and R6 classes. But only after you have understood the concept of environments and generic S3 functions. You might also want to reconsider whether Python isn’t the better language for your task.”
What are the R object systems?
S3, S4, R6, RC aka “Reference Classes”
“S” refers to the S programming language.
What is a class, an object, and a method in R?
- A class is the thing itself (think of a list, a tibble).
- An object is an instance of a class.
- A method is a function that only operates on a certain class.
What’s are generic functions? How are they related to the concept of classes in R?
Generic functions, such as “plot”, “mean”, “predict”, “print”, find out the class of the object it’s called upon and execute the method available under that class.
This is an application of its encapsulation because its ability to match methods with the object class is encapsulated within this generic function.
How to see the methods available of a S3 generic functions?
> methods(“mean”)
[1] mean,ANY-method mean,Matrix-method mean,sparseMatrix-method
[4] mean,sparseVector-method mean.Date mean.default
[7] mean.difftime mean.POSIXct mean.POSIXlt
[10] mean.quosure*
How to see the methods available of a S4 generic functions?
> showMethods(“print”)
Function “print”:
What are default methods for generic functions?
Usually generic functions have default methods, to be used when no other methods are found suitable.
For S3 methods (print), you can view the default code by:
> getS3method(“print”,”default”)
Functional OOP vs Encapsulated OOP in R
In functional OOP, methods belong to generic functions. Think of the methods as being located in a global lookup table. The method to execute is found by the runtime system based on the name of the function and the type (or object class) of one or more arguments passed to that function (this is called “method dispatch”). Examples are S3, S4 object systems.
In Encapsulated OOP, method calls typically look like object.method(arg1, arg2). This is called encapsulated because the object encapsulates both data (fields) and behaviour (methods). Think of the method as being located in a lookup table attached to the object or the object’s class description. Examples are Reference Classes and R6.