[R] Function calling a function, column name not passed properly

William Dunlap wdunlap at tibco.com
Wed Dec 2 04:17:33 CET 2015


The short answer is to provide an argument that your function does not
pass through substitute(), so standard evaluation takes place.  E.g., change
    SmallFn <- function(v,d) {
        column <- deparse(substitute(v))
        d[,column]
    }
to
    SmallFn <- function(v, d, column=deparse(substitute(v))) {
        d[,column]
    }
and call it as either
    SmallFn(firstColumn, d=data.frame(firstColumn=1:3))
or
    col <- "firstColumn"
    SmallFn(column=col, d=data.frame(firstColumn=1:3))

If you want to get fancy, check out Hadley's lazyeval package.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Dec 1, 2015 at 6:45 PM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
> David has  told me that my problem is because there is not column varscr
> in my dataframe. I know this. My question is how can I modify my code so
> that the second call to SmallFn will in fact access the column Wstscr
> which does, in fact exist in the dataframe.
> John
>
> Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
>
>> On Dec 1, 2015, at 2:57 PM, John Sorkin <jsorkin at
> grecc.umaryland.edu> wrote:> > I am trying to write a function that
> calls a function. The first call to SmallFn works without any problem,
> printing both the passed data and the column Wstscr. The second call
> does not work (error, Error in d[, column] : subscript out of bounds). >
>> The first call shows what I am trying to do with the second call. I am
> passing to the outer function doit12 the beginning of the name of the
> column that I want to access (Wst). doit12 creates the full name of the
> column (Wstscr) by using varxx <- deparse(susbstitute(variable)) and
> varscr <- paste(varxx,"scr",sep=""). I can access the column in doit12
> as seen by the results of   print(data[,varscr]).> > SmallFn works when
> it is called using SmallFn(Wstscr,data), but fails when called with
> SmallFn(varscr,data). I don't understand why the second call fails.
> varscr was shown to represent Wstscr.> > Please tell my why the second
> call is not working, please put me out of one-full day of misery!It’s
> telling you there is no column in that dataframe with the name “varscr”.—
> David.
>
>
>
>
> I am trying to write a function that calls a function. The first call to
> SmallFn works without any problem, printing both the passed data and the
> column Wstscr. The second call does not work (error, Error in d[,
> column] : subscript out of bounds).
>
> The first call shows what I am trying to do with the second call. I am
> passing to the outer function doit12 the beginning of the name of the
> column that I want to access (Wst). doit12 creates the full name of the
> column (Wstscr) by using varxx <- deparse(susbstitute(variable)) and
> varscr <- paste(varxx,"scr",sep=""). I can access the column in doit12
> as seen by the results of   print(data[,varscr]).
>
> SmallFn works when it is called using SmallFn(Wstscr,data), but fails
> when called with SmallFn(varscr,data). I don't understand why the second
> call fails. varscr was shown to represent Wstscr.
>
> Please tell my why the second call is not working, please put me out of
> one-full day of misery!
>
> Thank you,
> John
>
>
>
>
> mydata <- cbind(
> patient_id=c(10163,10987,19882,19899,20104,20105,20167,20318,20338,20392),
>
> Wstscr=c(139.00,NA,101.80,103.00,76.40,116.00,139.80,111.31,NA,150.00))
>
> mydata
>
> doit12 <-function(variable,data) {
>
>   varxx <- deparse(substitute(variable))
>   cat("varxx created from first deparse substitute=",varxx,"\n")
>   varscr <- paste(varxx,"scr",sep="")
>   cat("1varscr=",varscr,"\n")
>   cat("Data inside doit12\n")
>   print(data)
>   cat("Print the Wstscr column of data. varscr created using paste after
> deparse substitute\n")
>   print(data[,varscr])
>   cat("\n\n")
>
>   SmallFn <- function(v,d) {
>     cat("\nInside SmallFn\n")
>     zz <-match.call()
>     column <- deparse(substitute(v))
>     cat("column=",column,"\n")
>     cat("The results of match.call\n")
>     print(zz)
>     print("Hello world!")
>     print(d)
>     print(d[,column])
>   }
>   SmallFn(Wstscr,data)
>   SmallFn(varscr,data)
> }
> doit12(Wst,mydata)
>
>
>
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and
> Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
>
>
>
>
> Call
>
> Call from mobile
>
> Send SMS
>
> Add to Skype
>
> You'll need Skype CreditFree via Skype
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of MedicineBaltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:12}}



More information about the R-help mailing list