R Advanced

Printing an Output of a Program in R

When working with R in an interactive mode, you don’t need to use any functions or methods to print the result of your expressions; it will print them automatically.

Simply typing a variable or expression will print its value.

data <- 42

data

# [1]  42

However, you need functions to see the output if you are working in a script where you have written a program.

Here are six ways you can use to print the output of a program:

  1. Using print()
  2. Using cat()
  3. Using message(), warning(), and stop()
  4. Using sprintf()
  5. Using paste() / paste0()
  6. Writing to a file

Method 1: Using print()

The print() function is explicitly used to display the value of an object. It is a standard method for printing output to the console.

Basic print() usage

data <- 1:5

print(data)

# [1] 1 2 3 4 5

Passing “digits” argument

data <- 1 / 5
data2 <- 111 / 23
data3 <- 10900 / 2966

print(data, digits = 2)
print(data2, digits = 3)
print(data3, digits = 4)

# [1] 0.2
# [1] 4.83
# [1] 3.675

Handling NA values

If you pass the na.print argument to empty, it will replace NA values with nothing in the output.

mat <- matrix(c(1, NA, 3, 4, NA, 6, 7, 8, NA),
   nrow = 3, byrow = TRUE
)

print(mat)
cat("After passing na.print argument to empty", "\n")

print(mat, na.print = "")

Method 2: Using cat()

The cat() function concatenates and prints its arguments. It helps print custom messages or formatting output. It does not add quotes. For a cleaner console output, you can use this approach.

x <- 21

cat("The value of x is:", x, "\n")

# The value of x is: 21

The cat() method does not automatically add a new line, as you can see from the above code. We added a newline character “\n” manually. 

It cannot handle complex objects like a matrix.

mat <- matrix(1:4, nrow = 2)

cat(mat)

# 1 2 3 4%

You can see that the console output is wrong, and it does not look like a matrix.

Method 3: Using message(), warning(), and stop()

The message() prints a message but does not stop the execution of a function.

The warning() issues a warning message but, like message(), allows the execution to continue.

The stop() prints an error message and stops the execution.

message("This is a message.")

warning("This is a warning.")

Output

This is a message.

Warning message:
This is a warning.

Method 4: Using sprintf() for Formatted Strings

The sprintf() function formats strings (similar to printf() in C/C++). It’s helpful when you need to format numbers or strings before printing.

x <- 19

sprintf("The value of x is: %f", x)

Output

[1] "The value of x is: 19.000000"

Method 5: Using paste() / paste0()

The paste() function inserts spaces by default. The paste0() function concatenates without spaces.

paste("AI", "Learning")
# "AI Learning"

paste0("Hello", "world")
# "AILearning"

Method 6: Writing to a file

To print output to a file, you can use cat(), write.table(), or similar functions with a file connection.

x <- 1921

cat("The value of x is:", x, file = "output.txt", append = TRUE)

Output

That’s it!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

6 days ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

7 days ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

2 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

3 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

1 month ago