Top 10 Tips in R Studio to Boost Your Work

R
RStudio
Author

Burak Demirtas

Published

March 19, 2023

Source: Pixabay

Usually we learn things from others and their way becomes our way until we explore and find more. When I started learn R, I was seeing many people using the R studio in a different way. Yet, I wish they also showed me these tips long before and I could have saved a lot of time maybe.

That’s why I chose for you some of the most useful tips which are not commonly practiced especially within the beginners. Let’s see if you know them or learning today! 😅

Tip 10 - Jumping to the Help

Do you still going to help search when you need a help about a function? Or still writing “?” with the function to the console? I don’t! I just double click on the function and hit F1!

Tip 9 - Keep the Workspace Clean

If you are new to Rstudio, maybe you didn’t realize yet but with the default setup, Rstudio always asking to save your previous workspace and try to keep workspace data. Even when you open a new script, the values are there and I always go and clean the environment by broom icon from the right top. But, no more! Just follow the steps below:

Tip 8 - Getting the File Path

One of the easiest ways when you are uploading some data, to get the file path from import dataset. But without clicking “import”.

But there is also another way which is less known:

Tip 7 - Changing Working Directory in a Second!

For the ones who don’t know this yet, if you are working with external files in your script (for example you have an excel file which you import the data) , the file path descriptions can be annoying to implement. There are several ways to change the working directory and I usually prefer putting the below code directly in the beginning of my file:

Hide / show the code
# Setting the Working directory---------------------------------------------
WorkingDirectory <- getwd()
if (!is.null(WorkingDirectory)) setwd(WorkingDirectory)

Yet, sometimes , especially if there are several files open, this can fail. What I also do is the one below:

Tip 6 - Renaming as Batch

Do you still rename the variables one by one? Let me show you 2 ways:

Way 1:

This is some times failing. If it fails, try this:

Way 2: Replace and Find

Tip 5 - Keyboard Shortcuts List

Just hit Alt + Shift + K :

Tip 4 - Pacman!

Pacman name is coming from “package manager” but I guess also refers to the famous video game. Why it’s a tip in here? Because I hate library function since I’ve discovered pacman. What is the benefit of pacman? Let’s say you have a package in your code and you wanted to run it in another computer, but just because it’s not installed on the other computer, library gives error and you need to call for install.packages() .

Instead of library on the other hand, if you use pacman’s p_load , even if the mentioned library is not installed, it just doesn’t give up. It finds it from CRAN and install automatically.

That’s why, I include the below line to all my starting part of any R script:

Hide / show the code
# Loading the libraries-----------------------------------------------------
if (!require("pacman")) install.packages("pacman") #Installs pacman if needed
Loading required package: pacman
Hide / show the code
p_load(pacman, readr, tidyverse) #Needed libraries

Tip 3 - Code Snippets

This is absolutely something you are gonna love! You have some code and are you copy pasting from somewhere all the time? What about just calling it like it’s a function? Check this out 🤭:

Tip 2 - Rmarkdown Thingsss…

There are many cool tricks on working with Rmarkdown files but I only wanted to choose 2 of them in here. (Maybe there will be an extensive markdown post for all the tips about specifically rmarkdown and quarto!

1- Use visual editor while making aesthetic changes, it’s much better than source code editor! Also, check out other buttons which appear around the texts and above besides the “Visual” button to explore by yourself. 😉

2- Use chunk options instead of memorizing code!

Tip 1 - Add-Ins

Arguably the best tip is the Add-ins in R studio. There are many add-ins created by other users to make your life easier and error-free! For example, you want to create a plot with ggplot but you forgot the syntax? No problem, just open the Esquisse, choose your data frame and graph style by few clicks then just hit to the “insert the code” button.

There is definitely a huge potential in Add-ins but I guess the add-in library has still a long way ahead. For the moment, I usually use addins called “ColourPicker”, “dpylrAssist”, “ESQUISSE”, “GGTHEMEASSIST” and “GPTSTUDIO”. I strongly recommend you to check other add-ins, too.

I’m not gonna go into more details since you can already watch this to see them more in action:

You can even write your own Add-in by following a 5 minutes video as below:

Did you like the tips? Do you need more details? Put in comments and let’s discuss together! 😎