EZ

Eduzan

Learning Hub

Eduzan
Eduzan / R (programming language)

Input and Output

Developers often need to interact with users to gather data or provide results. Most modern programs use dialog boxes or console input for this purpose. In R, it is also possible to take input from the user through two main methods:

  1. Using the readline() method
  2. Using the scan() method

Using readline() Method

The readline() method in R accepts input in string format. If a user inputs a number, such as 123, it is initially treated as a string ("123"). To use the input in other data types like integers, numeric, or dates, R provides functions to convert the input to the desired type.

Conversion Functions in R:

  • as.integer(n) – Converts to integer
  • as.numeric(n) – Converts to numeric types (float, double, etc.)
  • as.complex(n) – Converts to complex numbers (e.g., 3+2i)
  • as.Date(n) – Converts to a date

Syntax:

var = readline();
var = as.integer(var);
# You can also use `<-` instead of `=`

Example:

# R program to demonstrate input using readline()

# Taking input using readline()
var = readline();

# Convert the input to an integer
var = as.integer(var);

# Print the value
print(var)

Output:

123
[1] 123

You can also display a message in the console to guide the user about what to input. Use the prompt argument inside the readline() function for this. The prompt argument is helpful for user interaction but is not mandatory.

Syntax:

var1 = readline(prompt = "Enter any number: ");
or,
var1 = readline("Enter any number: ");

Example:

# R program to demonstrate input with a prompt message

# Taking input with a message
var = readline(prompt = "Enter any number: ");

# Convert the input to an integer
var = as.integer(var);

# Print the value
print(var)

Output:

Enter any number: 123
[1] 123

Nomenclature Rules for R Variables

  1. Variable names can include alphabets, numbers, dot (.), and underscore (_).
    Example: var_1 is valid.
  2. No special characters (except dot and underscore) are allowed.
    Example: var$1 or var#1 is invalid.
  3. Variable names must start with alphabets or a dot (.).
    Example: my_var or .myVar is valid.
  4. Variable names must not begin with numbers or underscores.
    Example: 1var or _var is invalid.
  5. If a variable starts with a dot, the character immediately following cannot be a number.
    Example: .3var is invalid.
  6. Reserved keywords in R (e.g., TRUEFALSE) cannot be used as variable names.

Important Functions for R Variables

1. class() Function: This function identifies the data type of a variable.

Syntax:

class(variable)

Example:

number = 42
print(class(number))

Output:

[1] "numeric"

2. ls() Function: Lists all the variables in the current R workspace.

Syntax:

ls()

Example:

# Assigning values to variables
name = "Alice"
age <- 30
height -> 5.5

print(ls())

Output:

[1] "age" "height" "name"

3. rm() Function: Deletes a specified variable from the workspace.

Syntax:

rm(variable)

Example:

# Assigning and removing a variable
country = "India"
rm(country)
print(country)

Output:

Error in print(country): object 'country' not found

Scope of Variables in R

1. Global Variables: Accessible throughout the program and declared outside of any block or function.

Example:

# Declaring and accessing global variables
global_var = 10

display = function() {
  print(global_var)
}

display()

# Modifying the global variable
global_var = 20
display()

Output:

[1] 10
[1] 20

2. Local Variables: Accessible only within the block or function where they are declared.

Example:

# Declaring and using a local variable
my_function = function() {
  local_var = "This is local"
  print(local_var)
}

cat("Inside the function:\n")
my_function()

cat("Outside the function:\n")
print(local_var)

Output:

Inside the function:
[1] "This is local"
Outside the function:
Error in print(local_var): object 'local_var' not found

Difference Between Local and Global Variables in R

Input and Output
AspectGlobal VariablesLocal Variables
ScopeAccessible throughout the program.Accessible only within the function/block.
LifetimeExist until the program finishes execution or is removed explicitly.Exist only during the function/block execution.
Naming ConflictsProne to conflicts as accessible globally.Limited scope minimizes conflicts.
Memory UsageOccupies memory throughout the program’s execution.Utilized temporarily, thus conserving memory.
End of lesson.