Coming from a C / Python / Java background, I have trouble understanding some R syntax, where literals look like variables, but seem to behave like strings. For example:
library(ggplot2) library("ggplot2")
The two lines behave equivalently. However, I would expect the first line to mean "load the library whose name is stored in the ggplot2 variable" and give an error like object 'ggplot2' not found
.
Speaking of ggplot2:
ggplot(data, aes(factor(arrivalRate), responseTime, fill=factor(mode))) + geom_violin(trim=FALSE, position=dodge)
The variables arrivalRate
, responseTime
and mode
do not exist, but somehow R knows to look them up inside the data
data frame. I assume that aes
actually receives strings, that are then processed using something like eval
.
How does R parse code that it ends up interpreting some literals as strings?
Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string. In all other cases the variable isn't a string.
A Variable is a store of information, and a String is a type of information you would store in a Variable. A String is usually words, enclosed with "" Eg String x ="Welcome to SoloLearn" X is the Variable, and we declared it as a String, use the single = to assign the text to it.
Yes, @Dgrin91, a String is an object.
A string literal is where you specify the contents of a string in a program. Here 'A string' is a string literal. The variable a is a string variable, or, better put in Python, a variable that points to a string.
When an argument is passed to a function it is not passed as a value but is passed as a promise which consists of
The pryr package can show the info in a promise:
library(pryr) g <- function(x) promise_info(x) g(ggplot2)
giving:
$code ggplot2 <-- the promise x represents the expression ggplot2 $env <environment: R_GlobalEnv> <-- if evaluated it will be done in this environment $evaled [1] FALSE <-- it has not been evaluated $value NULL <-- not filled in because promise has not been evaluated
The only one of the above slots in the pryr output that can be accessed at the R level without writing a C function to do it (or using a package such as pryr that accesses such C code) is the code slot. That can be done using the R function substitute(x)
(or other means). In terms of the pryr output substitute
applied to a promise returns the code slot without evaluating the promise. That is, the value slot is not modified. Had we accessed x
in an ordinary way, i.e. not via substitute
, then the code would have been evaluated in the promise's environment, stored in the value slot and then passed to the expression in the function that accesses it.
Thus either of the following result in a character string representing what was passed as an expression, i.e. the character representation of the code slot, as opposed to its value.
f <- function(x) as.character(substitute(x)) f("ggplot2") ## [1] "ggplot2" f(ggplot2) ## [1] "ggplot2"
In fact, library
uses this idiom, i.e. as.character(substitute(x))
, to handle its first argument.
The aes
function uses match.call
to get the entire call as an expression and so in a sense is an alternative to substitute
. For example:
h <- function(x) match.call() h(pi + 3) ## h(x = pi + 3)
One cannot tell without looking at the documentation or code of a function how it will treat its arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With