# Here you create a section in the comment ------------------------------6 Code organization
This section intends to summarize a few tips and tricks to make your life easier when using R.
6.1 What is a “project” in RStudio?
Throughout this tutorial, we have created various R scripts that have been collected in a folder called “R Training.” When you code in R, a good practice is to create a “project” that points to the folder containing scripts that are related to each other (a research project, for example), the relevant data, etc.
This project folder can be created by clicking on “Project: (None)” in the top right corner, then on “New Project”, and finally “Existing Directory” (select the Training R folder).
This folder can contain several files:
Script
Data
Output (this is where you may want to gather the beautiful graphs and tables you have created throughout your code)
The advantage of working on a “project” in RStudio is that Rstudio will then automatically point to the root folder (R Training) when the project is opened, making it easier to access files in Files and by writing the path in scripts.
6.2 Organizing your code and files
It’s quite common to start a project and come back to it months or even years later (welcome to the wonderful world of academia!).
What should you do when you’ve forgotten everything?
If you’ve structured your code well, you can add sections to your script to make it easier to find your way around:
A good practice is to extensively comment your code and the your various operations you are doing in it. It will be very useful to help you find your way around and understand your code when you come back to it after some time - when you have completely forgotten what you were doing!:
#The hash symbol allows you to start a comment.
# When you need to write a long comment,
# you can write without the hash symbol, then highlight everything,
# and press Cmd (or Ctrl)+Shift+C, and the block will be turned into
# a comment!It is also quite common to create different scripts for different types of operations (but this depends on your habits), for example:
A script (or several scripts) where you perform all the recoding necessary for analysis and save your database with the new variables (in R format, rds rather).
A script (or several scripts) where you perform all the analysis operations.
Another good practice that we strongly recommend you to do is to keep track of your the progress of your work by saving a new file - with the date indicated in the title - each new day you are working on a script. For example, the recoding file could be named “20250602Recodages.R” (the date is in YYYYMMDD format to ensure that the files are displayed from oldest to newest in the folder where they are stored).
6.3 Get help
Here are a few ways to get help when the script crashes:
- The first reflex is to look up how to use a function in R:
?mean #The question mark opens the function description page.You can also type your problem directly on internet (for example on Google, etc.). This will often get you to the Stackoverflow forum (among others) where the problem you are facing has been discussed (usually in English).
You can also look into various R training guides (for example, the one mentioned in the preface section of this tutorial).
…