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).
Contact Us