R Input Output

Taking Input and showing the Outputs are important in a programming language.


Input Functions

Functions which take Input from the user are called Input Functions
Here are the Input Functions used in R

Function

Description

Example

scan()

Reads data from the console.

input_data <- scan(n = 1, quiet = TRUE)

read.table()

Reads data from a file into a data frame.

data <- read.table("filename.txt", header = TRUE)

read.csv()

Reads data from a CSV file into a data frame.

data <- read.csv("filename.csv", header = TRUE)

readLines()

Reads lines from a text file.

lines <- readLines("filename.txt")

readline()

Reads a line of text from the console.

user_input <- readline(prompt = "Enter something: ")


Example
cat("Enter your name: ")
user_name <- readline(prompt = "")
cat("Enter your age: ")
user_age <- as.numeric(readline(prompt = ""))

cat("\nEnter the numbers separated by spaces: ")
numbers <- scan(what = numeric(), quiet = TRUE)

# Print User Input
cat("\n\nUser Information:\n")
cat("Name: ", user_name, "\n")
cat("Age: ", user_age, "\n")
cat("Numbers entered: ", numbers, "\n")

Output Functions

Functions which gives Output to the user are called Output Functions
Here are the Output Functions used in R

Function

Description

Example

print()

Prints values to the console.

print(result)

write.table()

Writes a data frame to a text file.

write.table(data, "output.txt", row.names = FALSE)

write.csv()

Writes a data frame to a CSV file.

write.csv(data, "output.csv", row.names = FALSE)

cat()

Concatenates and prints values to the console or a file.

cat("Hello", "World", file = "output.txt", sep = "\n")

sprintf()

Formats values as strings.

formatted_result <- sprintf("%.2f", result)
print(formatted_result)

Plots and Graphics

Functions for creating plots and graphics (e.g., plot(), hist(), barplot()).

plot(x, y, type = "l", col = "blue", lwd = 2, xlab = "X-axis", ylab = "Y-axis", main = "Sine Function")


Example
data <- data.frame(
  Name = c("John", "Jane", "Bob"),
  Age = c(25, 30, 22),
  Grade = c("A", "B", "C")
)

# Writing to File
write.table(data, "output_data.txt", row.names = FALSE)

# Formatted Output
formatted_result <- sprintf("The mean age is %.2f", mean(data$Age))
cat("\nFormatted Output:\n")
cat(formatted_result, "\n")

# Plotting
x <- seq(0, 2 * pi, length.out = 100)
y <- sin(x)
png("sine_plot.png")
plot(x, y, type = "l", col = "blue", lwd = 2, xlab = "X-axis", ylab = "Y-axis", main = "Sine Function")
dev.off()

cat("\nOutput Files and Plots Created.\n")

Other ways to show a output

Apart from file output functions, we have nomal ways to show an output without the functions.
print() is most commenly used to show an text output. remaing are used for this and other purpuses
without using output funxtions only keeping a variable will show the output

Example
a = 5

a

Quick Recap - Topics Covered

R Input Output
R Input Functions
R Output Functions

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK