SimRock
SimRock

Reputation: 239

How to pass the unquoted name of a variable (and not its value) into a function dynamically?

I wish to write a function to analyze several identical variables in several datasets I built the function below but it does not work well. I am not sure how to pass a name in a function dynamically. Could someone help?

There are 10 identical variables (testvar1, testvar2,..., testvar10, etc...) in 15 different datasets (mydata1, mydata2,...mydata15, etc...)

library(readxl)

to_analyze <- function (data="mydata1", var = testvar1) {

  #reading my file in
  excelfile <- paste(`data`, "xlsx", sep = ".")
  dataset_name <- read_excel(excelfile)

  #populating the testvar1, testvar2,...
  dataset_name$var_interest <-  dataset_name$var #this does not work
  #I was hoping it would give dataset_name$var_interest <- dataset_name$testvar1

  #creating a smaller dataset
  eco <- dataset_name %>% 
    select(id, var_interest) #I want var_interest to be testvar1 (not the value but the name)

  ##doing some analysis on that dataset
}


#creating another function for all the datasets (15 total)

fct_all <- function(x){
  for(i in 1:15){
    iq <- as.double(i)
    dsn <- paste("mydata", deparse(iq), sep="")
    to_analyze(data=dsn, var = x)
  }
}

#applying the function for all the variables

all_var <- c(testvar1, testvar2, testvar3)
fct_all(all_var)```

Upvotes: 1

Views: 707

Answers (2)

SimRock
SimRock

Reputation: 239

Thank you @Romain. Your code worked well. I just changed the single quote to backticks

var <- `var`

to_analyze <- function (data = "mydata1", var = "testvar1") {
  eco <- paste(data, "xlsx", sep = ".") %>%
    read_excel() %>%
    select(id, var) %>%
    rename(var_intereset = var)
  ...
}

Upvotes: 0

Romain
Romain

Reputation: 2151

You can use equivalently data$var1 or data[['var1']]. Which means that with the second form, as you provide the variable name as a string, you can easily replace the string by a dynamical variable name :

var <- 'var1'
dataset_name$var_interest <- dataset_name[[var]]

Note that dplyr is very clever and does accept string or symbols. Which means you can further simplify your function with one of the following forms :

library(dplyr)

# Using select then rename with the var given as a string
to_analyze <- function (data = "mydata1", var = "testvar1") {
  eco <- paste(data, "xlsx", sep = ".") %>%
    read_excel() %>%
    select(id, var) %>%
    rename(var_intereset = var)
  ...
}

For the sake of interest, you could also use quasiquotation, but given that you'll eventually wrap your variables into a vector, I guess it's not that useful. Would be something of the form :

library(dplyr)

to_analyze <- function (data = "mydata1", var = quo(testvar1)){
  quo_var <- enquo(var)
  eco <- paste(data, "xlsx", sep = ".") %>%
    read_excel() %>%
    select(id, !!quo_var) %>%
    rename(var_intereset = !!quo_var)
  ...
}

This form allows you to call your variable with the raw variable name rather than a string : to_analyse(data = "mydata1", var = testvar1). But as said before, probably not the most useful in your case.

Upvotes: 2

Related Questions