5-Minute Guide to Calling Functions from R Scripts: R Code Tutorial Using Google Trends Data

Emily Halford 13/10/2020 7

You’ve likely heard the popular guideline that if you find yourself copying and pasting code more than 3 times, you should write it as a function.

While you can write and store these functions at the top of your R Markdown files, this approach can get messy and is counterproductive if you end up copying and pasting the functions into multiple files. Often, the best way to stay organized is to write your functions in a script and to call them from any additional files where they’re needed.

To demonstrate this process, I will use 3 functions to conduct a very simple change point analysis of Google searches containing the words “supreme court” over the past month.

First, Create Your Functions


I’ve also checked this “Source on Save” box. If you check this box, then the file will be sourced automatically to the global environment when you save changes to your functions in the script.

Connect to Your Functions

 


When we run this line of code, the functions contained within the script automatically appear in the Global Environment. The connection was successful!

Use Your Functions


Now we have data to use in our functions! We use the google_graph() function the same way we would use any other function, allowing us to easily plot the data:


The plot looks good! Unsurprisingly, there is a massive surge in searches containing “supreme court” following the death of Supreme Court Justice Ruth Bader Ginsburg. Let’s use the bcp_analysis() and bcp_plot() functions to see if this spike represents a significant change point in this time-series object. Again, we use the functions the same way we would if we were using functions from a loaded package:

 


Several days following the death of Ruth Bader Ginsburg have posterior probabilities of 1.00, indicating that searches containing the words “supreme court” likely changed in a statistically meaningful way around this time.

It’s as simple as that! If we wanted to plot and analyze several different sources of data in separate Rmd files, it would be as easy as connecting those files to our “functions” script with a source() statement and using our functions.

Share this article

Leave your comments

Post comment as a guest

  • Glenn Z

    This article is gold !

  • Liam Symonds

    Straight to the point and well explained

  • Kyle Healy

    Truly instructional, no fluff and no BS. Cheers !!!

  • Robert Rowles

    Good explanation. Lots of doubts cleared.

  • William Carole

    In reply to: Robert Rowles

    Mine as well. Thanks a lot !!

  • Gerard Powel

    So helpful!

  • João Henrique Oliveira

    Good tips! Definitely a good read! Earned a new reader for the quality of this tutorial!