DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Tips and Tricks for Efficient Coding in R
  • How to Get a Non-Programmer Started with R
  • Python vs. R: A Comparison of Machine Learning in the Medical Industry
  • 8 Awesome Source Code Management Tools to Keep In Check

Trending

  • Why Database Migrations Take Months and How to Speed Them Up
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  1. DZone
  2. Coding
  3. Languages
  4. How to Write R Script Explained with an Awesome Example

How to Write R Script Explained with an Awesome Example

If you have a long analysis to perform in R, and you want to be able to recreate it later, a good idea is to type it into a script.

By 
Srini Pesala user avatar
Srini Pesala
DZone Core CORE ·
Dec. 05, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
126.8K Views

Join the DZone community and get the full member experience.

Join For Free

A script is a good way to keep track of what you're doing. If you have a long analysis, and you want to be able to recreate it later, a good idea is to type it into a script. If you're working in the Windows R GUI (also in the Mac R GUI), there is even a built-in script editor. To get to it, pull down the File menu and choose New Script (New Document on a Mac). A window will open in which you can type your script. R Script is a series of commands that you can execute at one time and you can save lot of time. script is just a plain text file with R commands in it.

How to Create R Script

  1. You can prepare a script in any text editor, such as vim, TextWrangler, or Notepad.
  2. You can also prepare a script in a word processor, like Word, Writer, TextEdit, or WordPad, PROVIDED you save the script in plain text (ASCII) format.
  3. This should (!) append a ".txt" file extension to the file.
  4. Drop the script into your working directory, and then read it into R using the source() function.
  5. Just put the .txt file into your working directory
  6. Now that you've got it in your working directory one way or another, do this in R.

 > source(file = "sample_script.txt")  # Don't forget those quotes!

A note: This may not have worked. And the reason for that is, your script may not have had the name "sample_script.txt".

if you make sure the file has the correct name, R will read it. If the file is in your working directory, type  dir() at the command prompt, and R will show you the full filename.

Also, R does not like spaces in script names, so don't put spaces in your script names! (In newer versions of R, this is no longer an issue.)

What's Happening in That Script You Just Wrote?

Example:

# A comment: this is a sample script.

y=c(12,15,28,17,18)

x=c(22,39,50,25,18)

mean(y)

mean(x)

plot(x,y)

What happened to the mean of "y" and the mean of "x"?

The script has created the variables "x" and "y" in your workspace (and has erased any old objects you had by that name).

You can see them with the  ls( ) function.

Executing a script does everything typing those commands in the Console would do, EXCEPT print things to the Console. Do this.

> x

[1] 22 39 50 25 18

> mean(x)

[1] 30.8

See? It's there. But if you want to be sure a script will print it to the Console, you should use the print() function.

> print(x)

[1] 22 39 50 25 18

> print(mean(x))

[1] 30.8

When you're working in the Console, print() is understood (implicit) when you type a command or data object name. This is not necessarily so in a script.

  • Hit the Enter key after the last line. Now, in the editor window, pull down the Edit menu and choose Run All. (On a Mac, highlight all the lines of the script and choose Execute.) The script should execute in your R Console.
  • Pull down the File Menu and choose Save As... Give the file a nice name, like "script2.txt". R will NOT save it by default with a file extension, so be sure you give it one. (Note: On my Mac, the script editor in R will not let me save the script with a .txt extension. It insists that I use .R. Fine!) Close the editor window. Now, in the R Console, do this:

 > source(file = "script2.txt") # or source(file = "script2.R") if that's how you saved it

The "aov.out" object was created in your workspace. However, nothing was echoed to your Console because you didn't tell it to print().

Go to File and choose New Script (New Document on a Mac). In the script editor, pull down File and choose Open Script... (Open Document... on a Mac). In the Open Script dialog that appears, change Files Of Type to all files (not necessary on a Mac). Then choose to open "script2.txt" (or "script2.R", whatever!). Edit it to look like this.

print(with(PlantGrowth, tapply(weight, group, mean)))

with(PlantGrowth, aov(weight ~ group)) -> aov.out

print(summary.aov(aov.out))

print(summary.lm(aov.out))

Pull down File and choose Save. Close the script editor window(s). And FINALLY...

 > source(file = "script2.txt") # or source(file = "script2.R") if necessary

Finally, writing scripts is simple.

R (programming language) file IO Awesome (window manager)

Published at DZone with permission of Srini Pesala. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Tips and Tricks for Efficient Coding in R
  • How to Get a Non-Programmer Started with R
  • Python vs. R: A Comparison of Machine Learning in the Medical Industry
  • 8 Awesome Source Code Management Tools to Keep In Check

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!