61 R Tips
R is a flexible programming language that is used heavily in our lab, both for statistical analysis and also for interacting with our data.
- An R program is called a Script and most of our scripts were written by our PI John Curtin or by Susan.
- We use a GUI called RStudio to write and execute R code. This is already installed on any computer you will be using.
- You don’t need to know the programming language to use an R script. You just need to click your cursor on a line (or highlight multiple lines) and hit ctrl-Enter (or hit the Run button) to execute the code.
- The commented lines (usually green, start with #) provide an explanation for what the code is doing.
61.1 R Training resources
If you’ve never had any programming, R can feel intimidating! We encourage you to work through all of these materials to become more comfortable with R.
- https://bookdown.org/daniel_dauber_io/r4np_book/ - R for Non-Programmers
- https://moderndive.netlify.app/1-getting-started.html - Getting Started with Data in R
- https://rladiessydney.org/courses/ryouwithme/01-basicbasics-0/ - RYouWithMe; The BasicBasics
- https://rstudio-education.github.io/hopr/basics.html - Hands-On Programming with R: The Very Basics
- https://r4ds.had.co.nz/introduction.html - R for Data Science
- https://rmarkdown.rstudio.com/lesson-1.html - Introduction to RMarkdown
61.3 Useful Scripts
61.3.1 visit_followup.Rmd
This script is run before a follow-up visit. It can also be used to open the enrollment database, if you need to look up a participant’s email or phone number
To just get the enrollment database, run everything above line 93, then click the enrollment database when it appears in the Environment pane (upper right-hand pane).
61.3.2 data_report_by_subid.Rmd
This script provides a table of participant data, by day, with columns for completing their daily survey, monthly survey, number of GPS points, SMS messages, and voice messages, and a list of actions they took within the app on that day.
Run everything above line 520 to get the tibble. The first time, it will take a while to load all the dataframes. After that, it goes much more quickly. HOWEVER, you should start each day by clearing your environment and re-running it so it’s not using old data.
This script also loads many useful dataframes:
- all_tokens = can be used to check participant’s last login, and whether they are using Chrome
- chunk 10, dynamic_survey_dates, can be run to get the dates for doing dynamic survey uploads
- all_training = can be used to check if the participant has completed the training videos