Using arguments in R function calls

Hello,
I am running into issues while using arguments
in R function calls. The code below shows a minimum working example
to give an idea of the problem that I am facing. Would it be possible for anyone to have a
look and tell me why it is the case that the arguments are working as intended
in the first case and not in the last case?
[ This is a silly question and I do apologize in advance.
I guess that having extensively worked in Stata I am thinking about
the way local Stata macros work as I am writing the code below and that is what
is causing the error.]

# MWE
# ====================
library(ggplot2)
data(mpg)

# When I define the function this way :
grapher <- function(data, xvar, yvar){
attach(data)
plot(xvar, yvar)
hist(xvar)
hist(yvar)
detach(data)
}
grapher(mpg, displ,  cyl) # the code works

# Explicitly specifying  the vectors using $ notation works as well :
grapher2 <- function(xvar, yvar){
  plot(xvar, yvar)
}
grapher2(mpg$displ, mpg$cyl)


# Why does this piece of code fail?
grapher <- function(data, xvar, yvar){
plot(data$xvar, data$yvar)
}
grapher(data=mpg, xvar=displ,  yvar=cyl)

Many thanks for your assistance.

Hello aninjaboy.

Not a silly question at all!

Objects in function calls are evaluated when you call the functions. This means that in your last example, when calling

grapher(data=mpg, xvar=displ,  yvar=cyl)

R will search for objects named "displ" and "cyl" in your environement, as it has no idea that you are referring to column names of mpg.

This also means, that if these objects existed in your environment, they would be passed on to your function, causing problems there.

# ERRONEOUS CODE

displ <- mpg$displ
cyl <- mpg$cyl

grapher <- function(data, xvar, yvar){
plot(data$xvar, data$yvar)
}

grapher(data=mpg, xvar=displ,  yvar=cyl)

You can see, that instead of the column names, "displ" and "cyl", the column values are passed on to the function. So in order to get your code fixed, we instead need to pass the column names in character form:

grapher_fixed <- function(data, xvar, yvar){
  
  xvals <- data[[xvar]]
  yvals <- data[[yvar]]
  
  plot(xvals, 
       yvals,
       xlab = xvar,
       ylab = yvar)
}

grapher_fixed(data=mpg, 
              xvar="displ",  
              yvar="cyl")

I've added the "xlab" and "ylab" definitions for more useful column names.
Notice, that you extract the values of a column of name "my_column_name" by using double brackets:

data[["my_column_name"]]

Hope this helps.
Cheers!
Valentin

Many thanks for your response and explanation, Valentin.
Best,
aninjaboy

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.