Press "Enter" to skip to content

How do I create a script in R?

Zigya Acadmey 0

An R script is simply a text file containing (almost) the same commands that you would enter on the command line of R.

Coding in R

1. You can get the R console using the default shortcut in the start menu and start R interact coding.

2. You can go to command prompt, type R and get the console and start.

3. You can write a script with . R extension and run it in command prompt using Rscript <filename> command.

4. You can install R studio IDE and start coding there.

R is an interpreting language, so when you use R in the R console, it works fundamentally by the question and answer model. You enter a line with commands and press the ‘Enter’ key. Then the program executes that line, prints the results if there are any, and if necessary ask for more input.

This means you can see your output right after you write a code line and move to the next. That is an advantage because then you are less likely to have errors in the middle of the program.

when using Rstudio IDE you can run your scripts using ctrl+Enter

Creating your first R script

You can open an R script in RStudio by going to File > New File > R script in the menu bar. RStudio will then open a fresh script above your console pane

Saving your R script

I strongly encourage you to write and edit all of your R code in a script before you run it in the console. Why? This habit creates a reproducible record of your work. When you’re finished for the day, you can save your script and then use it to rerun your entire analysis the next day. Scripts are also very handy for editing and proofreading your code, and they make a nice copy of your work to share with others. To save a script, click the scripts pane, and then go to File > Save As in the menu bar.

Executing your R script

RStudio comes with many built-in features that make it easy to work with scripts. First, you can automatically execute a line of code in a script by clicking the Run button

R will run whichever line of code your cursor is on. If you have a whole section highlighted, R will run the highlighted code. Alternatively, you can run the entire script by clicking the Source button. Don’t like clicking buttons? You can use Control + Return as a shortcut for the Run button. On Macs, that would be Command + Return.

Now you are all set to write scripts in R. lets just try the hello world here.

Type your first R script

msg = "Hello World!"
print(msg)

Now remember that you should save the script file first, and then you can execute / run this file.

To run the above script hit the Run button on top panel. And you will see the output in the console panel.

Conclusion

Hence we saw how to create a R script, and there is your very own first R script.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *