Using R scripts effectively, even as a beginner

R scripts Beginner Reproducibility

Moving from working in the console to working from the R script.

Soundarya Soundararajan true
08-03-2021

As a beginner, I started working in the console.

Like this. Working in the console Many of you might relate to this habit. Once you type in console, you just have to hit enter and the command works. So, what is the trouble? The problem in this approach is, your commands are temporary , unsearchable and gone forever.

Also, this happens: We are so conditioned to hit the “enter” key when we want to move to the next line, and as we do that in the console, it makes a premature ending of the command line without completion. You wanted to do this

expectation

But you ended up doing this:

You missed typing the last line but the code ended

This is because you did not finish what you wanted to write but accidentally hit enter and the console took your word for it.

This is just one example of why you should start using R scripts even as you start working in RStudio. I realized the ease and importance of working from R script a bit late in my R journey. Once mastered, working from R scripts and R markdown files are real game changers. (more on R markdown/Rmd in a later post).

How to set up an R script file?

To open a new R script file,

Go to file –> New file –> R script Opening a new R script file

Clicking this opens a new script file for you and is untitled.

An untitled R script file

Save the script file

Now rename the file and save it in your project folder. I will write about opening a project folder and maintaining it in a later post.

Click the save symbol to save the R script

I saved my R script as a temp(orary) file. Note that it saves as a file with a .R extension. Now you are good to write your commands and execute from the R script file.

What makes an R script different from working in the console?

So what changes when you work from the .R file?

– Well, you have your codes documented.

– Writing in R script is like writing in a word/text file. It captures until you want to run the command.

– When you want to run the command, either highlight the command line and hit command+enter or simply keep the cursor in the command line and hit command+enter

Select the command line you want to run

Whatever you execute from the R script produces both the command and the result in the console, if it is a plot it opens in another pane. Working from the R script gives you enough space to organize yourself, to know what you are doing and most importantly it lets you add comments. Why should you comment? You are more likely to revisit the code and as a beginner, it perplexed me when I visited them even a couple of days later unless I commented.

Adding comments to your codes

In a script file, add your comments after # sign, R understands that is not a command but a comment.

Like this: Adding comments after # symbol is an important step as a beginner

When run, the comments are ignored from the codes, but they are still printed in the console. Comment heavily for the future you!

Until I see you with another post tomorrow, Happy scripting!

Questions to ponder

What if you have coded from console and you seem to have lost them? Can you find what you ran? even if you have not used R scripts? Comment your answer below.

Atomic essay on coding effectively

Read more on the atomic essay i wrote on reproducible and readable codes below:

Atomic essay on Effective R scripting

Citation

For attribution, please cite this work as

Soundararajan (2021, Aug. 3). My R Space: Using R scripts effectively, even as a beginner. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2021-08-03-using-r-scripts-effectively-even-as-a-beginner/

BibTeX citation

@misc{soundararajan2021using,
  author = {Soundararajan, Soundarya},
  title = {My R Space: Using R scripts effectively, even as a beginner},
  url = {https://github.com/soundarya24/SoundBlog/posts/2021-08-03-using-r-scripts-effectively-even-as-a-beginner/},
  year = {2021}
}