473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lesson 2 – getting started with r (console and expressions)

nbiswas
149 New Member
R scripts can be executed within the R Console or outside of the R Console as batch operations.
We will examine some simple R Expressions and learn how to obtain help along the way.
The starting point for every R user is the R Console.
The R Console can be used to issue interactive R functions or expressions. The greater than (>) symbol is the default prompt for R.
When an R expression is complete, within the R Console, it can be executed.
If the expression is not complete, the R Console prompt will change to the plus (+) symbol.
Each R session has a working directory.
A working directory contains a workspace file, named .Rdata, which contains a list of R objects.
R will also keep file with the history of previously issues R commands.
The R working directory can be changed using the setwd() function.
To determine the current working directory simply issue the getwd() function.
When you complete a session of using the R Console you will be prompted to consider saving your workspace. You may want to save your workspace so you can return to your analysis at a later time, but it is always a good practice to create R scripts from your R Console sessions so you do not accidently overwrite your workspace file.
Executing R scripts outside of the R Console can help automate your data analysis activity.
Let's consider a scenario where you have created an R script that performs data analysis and generates a set of data plots as PDF files. It would be desirable to automate this task and avoid using the R Console altogether.
There are a few different methods of executing R scripts in a batch mode.
The command R CMD BATCH can be used across all platforms.
There is also a tool called Rscript that can be used across all platforms to run scripts outside of the console. This tool gives you the ability to obtain input from the user as the script executes, if required.

On Linux, R script files can be changed into an executable script by simply adding a reference to the Rscript executable in the first line of the script file. Be sure to change the file permissions of your script to be executable if you decide to use this approach.
By default, there is an output file created for any information sent to standard output.
The name of the output file is the same name as the script with the extension .Rout.
Let's consider using R as a simple calculator. R code consists of a series of expressions.
These expressions can be generally classified as: data, computation, or control flow operations.
In this first example we are storing the value 5 into an object or variable called 'a'. The data type of object is automatically determined by R.
It is possible to explicitly state the data type of a value, but for now we will let R decide. The recommended assignment operator in R is the Less Than sign (<-) followed by a dash.
A single equals sign (=) can be used, but it not recommended. The addition operator is a built-in function in R and here we see the result of adding the two numbers together
The output is displayed in the R Console, but it has not been assigned to a object and therefore it is no longer accessible. The ls(), or List objects, function is useful while using the R Console as it provides a displayed list of all of the currently active objects.
You may have noticed that the output displayed contained the value 1 inside of square brackets.
This is how R indicates that the data is the first of possibly many items.
There are various other R functions that can be used to describe an R object.
For example, here we are interested in the data type of the object 'a' and we discover that R decided that the datatype for 'a' is a double precision floating point number.
As we will learn later in the course, it is possible to ensure that the values stored in the object 'a' are limited to integers only.
As we become more familiar with R, it is important to know where to go for help.
The help.start() function will launch a local web server with links to various R reference materials.
If you know the name of an R function, but you can't remember its input arguments or its output, use the help() function or use a single question mark to obtain more information.
A set of two question marks (??) followed by a search term can used to perform a search across all of the installed R packages on your system.
Many functions have built-in examples available to help you understand how to use them. The example() function can be invoked to access these built-in samples.
When you are ready to terminate a session within the R Console, use the q() or quit() function.
R has excellent support for creating high quality graphics with minimal effort.
Here are a few simple examples.
First, we have a scatterplot showing the trends in naming females born in Ontario, Canada from 1917 through 2010. The name 'Emma' was selected for this plot.
The next example is a box plot showing the fuel consumption of cars available in Canada
in 2013. The data has been categorized by the number of cylinders the vehicle has.
The final plot is of a simple pie-chart showing the distribution of 4, 6, and 8 cylinder cars available in Canada in 2013.
We will learn how to create these and other plots in future lessons.
Sep 4 '14 #1
1 4984
zmbd
5,501 Recognized Expert Moderator Expert
nbiswas:
Please provide proper citations for these articles.
-z
Sep 29 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2301
by: soni29 | last post by:
Hi, I'm going to be starting a project with a friend of mine in C#, we're doing it to learn the language a little better, to get more experience with it. We've already come up with an idea of the application to build, but need advise on how to get started, to share the work? Also if there are free servers available with sourcesafe, so we...
84
3853
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
6
2174
by: Jack Duijf | last post by:
Hello, I am looking for a person in The Netherlands that is willing to help me getting started with Vb.net. Please send a message to jack@aicn.nl if you can help me getting started with the Microsoft Developement Enviroment. Thanks,
6
2030
by: nibiery | last post by:
I am just getting started with Python, and I think I may be thinking about it wrong. I'd like to be able to work interactively with some code that I've got in a file. The only interpreted language I have much experience with is Tcl/Tk, and in that I would use "source file.tcl" in the console to load my source. Is there a similar command in...
1
4721
by: =?Utf-8?B?Q29kZVNsaW5nZXI=?= | last post by:
I plan to build my own 2008 Server/Hyper-V system and will not be using one of the tested Dell or HP systems from the release notes and could use some pointers as to my assumnptions and answers to a few questions. Some getting started with Hyper-V assumptions. Please tell me if I am off base here. 1. Any recent Intel quad core processor...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.