Moving from working in the console to working from the R script.
As a beginner, I started working in the console.
Like this. 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
But you ended up doing this:
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).
To open a new R script file,
Go to file –> New file –> R script
Clicking this opens a new script file for you and is untitled.
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.
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.
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
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.
In a script file, add your comments after #
sign, R understands that is not a command but a comment.
Like this:
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!
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.
Read more on the atomic essay i wrote on reproducible and readable codes below:
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} }