This article is part of a series.
- Part 1 - HMIS, R, and SQL -- Introduction
- Part 2 - HMIS, R, and SQL -- Basics
- Part 3 - Preamble to Mixing R and SQL
- Part 4 - HMIS, R, SQL -- Work Challenge One
- Part 5 - Read and Write CSVs in R
- Part 6 - Working with Dates in SQLdf
- Part 7 - Filter to Most Recent HUD Assessment
- Part 8 - HMIS, R, SQL -- Work Challenge Two
- Part 9 - Comparing Values in R and SQL
- Part 10 - Working with R Excel Libraries
- Part 11 - HMIS, R, SQL -- Work Challenge Three
- Part 12 - This Article
- Part 13 - SQL CASE and R Paste
Creating Reusable Code
Writing report code which can be reused is critical to being an effective report specialist. By now, hopefully, you see the power of SQL-R, especially around HMIS data. But you may still feel slow. Or have thoughts like, “If I pulled these data into Excel I could manually filter them in 1/10th the time.” That’s probably true. But, after manually filtering dataset after dataset it becomes apparent finding a way to automate some tasks would save many hours in the long-run. Thus, writing an R scripts for routine work would save countless hours of monotony.
However, one problem remains, each task will usually have a slight variation from the one before it. This causes you to write 95% of the same code with a slight tweak for the current project. And that doesn’t save time at all. In the programming world, the 95% code which is the same is known as bolierplate code.
Ok, that’s the problem. The solution? Functions.
A function is nothing more than a section of code you save into a variable for easy reuse.
Defining a function looks like this:
Then, whenever you want to use this code it can be called like this:
If you want to pass the function something to use:
And the coolest thing about functions is being able to
return data. Functions return whatever data is on the last line of the function. This can be a tricky concept, but at its root it is simple.
clientDf will be returned.
The result is then passed back out of the function, where it can be assigned to a new variable.
You may notice, this is similar to a lot of code we have been using. Like
read.csv. That’s because
read.csv is a function written by the makers of R and included for our use.
This is how R has become powerful tool. Many smart people have written sets of functions, which are called libraries. Feel the power of open-source.
Time to give back to community and write some of our own functions
For this work challenge you will need:
Write functions which will do the following:
exitDfand return the combined dataframe.
- Make the following columns readable:
- Get most recent HUD Assessment per PersonalID
- Filter to clients who are active in programs (except Night-by-Night and Street Outreach projects)
- Write a function to filter
enrollmentDfbased upon a user defined parameter.
- Write a function which returns a list of Chronically Homeless individuals.
For the last function, here’s an example,
The function you’d write would be
getSubpopulation(). The first parameter would be the dataframe the user is passing into your function. Second parameter is the column to look at. The last is which response the user wants in the column to look in.
Below are the resources which should help for each step:
- R Programming A-Z – Video 21 – Functions in R