*Photo by olia danilevich from Pexels*


Introduction

I was recently writing a function for an R package I was working on in which I wanted to return the label for a given column in a given data object. The issue was that I had to also check if the data object existed, which meant using a character string argument for the data object name as I was using the exists() function. This meant, if I were to give a column name as a second argument and asked the function to check for that name in the first argument, then it would check if that second argument existed in the character string of the first argument. What I needed to do was find a way of getting the data object of the same name as the first character string argument.

The solution to my problem is in the following code chunk. Which I break down afterwards.

Note: this function uses the function get_var() from the labelled package.

## create function to get label of column in labelled data type
get_label <- function(.data, .variable){
  ## if data object exists
  if(exists(.data)){
    ## if the data object has the given variable label
    if(hasName(get(.data), .variable)){
      ## print the variable label to the console
      cat(labelled::var_label(get(.variable, get(.data))))        
    }
    ## else if the given variable label is not in data object
    else{
      ## print this to console
      cat("The column does not exist in the given data object.") 
    }
  }
  ## else if the given data object does not exist
  else{
    ## print this to console
    cat("The given data object does not exist.")                 
  }
}

Breakdown

The solution to getting the actual data object instead of the given character string is the get() function.

Here is a break down of each line of code:

To begin, we create the function that will be used to return the requested label. In this case, I’ve called it get_label(). This function takes two arguments, .data (data object) and .variable (variable name). These need to be given as character strings as the functions used within the get_label() function will only take character strings.

## create function to get label of column in labelled data type
get_label <- function(.data, .variable){
}


The second line, which is the if statement, checks if the given data object argument exists in the Global Environment. The function exists() must take a character string, which is why the .data argument needs to be a character string.

## if data object exists
if(exists(.data)){
}


The second if statement uses the function hasName() and checks if the given data object (.data) has the variable name given by .variable. Like exists(), hasName() also requires a character string argument which is why .variable needs to be a character string. However, using only the hasName() function with the character string arguments creates an issue, as the function would just check if the given .data character string contains the given .variable character string. We want to check the actual data object. This can be done by using the get() function with the .data argument, which will return the actual object for a given character string.

## if the data object has the given variable label
if(hasName(get(.data), .variable)){
}


The reason I wanted to do this in the first place was to print the label associated with a column in a labelled dataset. Which can be done by using the var_label() function from the labelled package. However, this once again needs the actual objects and not the character strings. So, we use the get() function again to get both the actual data object and the actual variable in the data object and not just the character string arguments.

Note: I used the cat() function instead of the print() function just because I prefer how the given message comes out in the console.

## print the variable label to the console
cat(labelled::var_label(get(.variable, get(.data))))

Roundup

So, by using a combination of the exists() function, hasName() function, and the get() function I was able to check if a given character string matched an existing data object. And, if that data object contained a variable also given as a character string.


Note: This function is used in the cesR package, which I wrote about in this post.