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

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

Emily Halford 13/10/2020 7
5-Minute Guide to Calling Functions from R Scripts: R Code Tutorial Using Google Trends Data

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:


Google Searches for Supreme Court

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

terms and condition.
  • 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!

Share this article

Emily Halford

Data Science & Mental Health Expert

Emily is a data analyst working in psychiatric epidemiology in New York City. She is a suicide-prevention professional who is enthusiastic about taking a data-driven approach to the mental health field. Emily holds a Master of Public Health from Columbia University.

Cookies user prefences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Read more
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics