Basics of Input/Output

Taking Input from the User:

R Language provides us with two inbuilt functions to read the input from the keyboard.

  • readline() method: It takes input in string format. If one inputs an integer then it is inputted as a string.

Example:

R




# R program to illustrate
# taking input from the user
 
# taking input using readline()
# this command will prompt you
# to input a desired value
var = readline();


  • scan() method: This method reads data in the form of a vector or list. This method is a very handy method while inputs are needed to taken quickly for any mathematical calculation or for any dataset.

Example:

R




# R program to illustrate
# taking input from the user
 
# taking input using scan()
x = scan()


Note: For more information, refer Taking Input from User in R Programming

Printing Output to Console:

R Provides various functions to write output to the screen, let’s see them –

  • print(): It is the most common method to print the output.

Example: 

R




# R program to illustrate
# printing output of an R program
 
# print string
print("Hello")
 
# print variable
# it will print 'w3wiki' on
# the console
x <- "Welcome to w3wiki"
print(x)


 Output:

[1] "Hello"
[1] "Welcome to w3wiki"
  • cat(): cat() converts its arguments to character strings. This is useful for printing output in user defined functions.
     

Example:

R




# R program to illustrate
# printing output of an R
# program
 
# print string with variable
# "\n" for new line
x = "Hello"
cat(x, "\nwelcome")
 
# print normal string
cat("\nto w3wiki")


 Output:

Hello 
welcome
to w3wiki

Note: For more information, refer Printing Output of an R Program

Learn R Programming

R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS.

In this R Language tutorial, we will Learn R Programming Language from scratch to advance and this tutorial is suitable for both beginners and experienced developers).

Similar Reads

Why Learn R Programming Language?

R programming is used as a leading tool for machine learning, statistics, and data analysis. R is an open-source language that means it is free of cost and anyone from any organization can install it without purchasing a license. It is available across widely used platforms like windows, Linux, and macOS. R programming language is not only a statistic package but also allows us to integrate with other languages (C, C++). Thus, you can easily interact with many data sources and statistical packages. Its user base is growing day by day and has vast community support. R Programming Language is currently one of the most requested programming languages in the Data Science job market that makes it the hottest trend nowadays....

Key Features and Applications

Some key features of R that make the R one of the most demanding job in data science market are:...

Download and Installation

There are many IDE’s available for using R in this article we will dealing with the installation of RStudio in R....

Hello World in R

R Program can be run in several ways. You can choose any of the following options to continue with this tutorial....

Fundamentals of R

...

Data Types

Variables:...

Basics of Input/Output

...

Decision Making

...

Control Flow

...

Loop Control Statements

...

Functions

...

Data Structures

...

Error Handling

Each variable in R has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. R supports 5 type of data types. These are –...

Charts and Graphs

...

Statistics

Taking Input from the User:...

Contact Us