Input and Output in R
Programming
• Understanding how R interacts with users and
files for input and output operations.
Introduction
• Input and Output (I/O) are essential for
interacting with the user and external data
sources.
• • Input: To read data from the user or files.
• • Output: To display or save data.
Types of Input and Output
• Input:
• • Keyboard input
• • File input
• • Data frame import
• Output:
• • Console output
• • File output
• • Plot output
Keyboard Input Functions
• • readline(): Reads a line from the keyboard
• Example:
• name <- readline(prompt='Enter your name: ')
• print(paste('Hello', name))
• • scan(): Reads multiple numeric or character
inputs
• nums <- scan()
• print(nums)
Reading Data from Files
• • read.table():
• data <- read.table('data.txt', header=TRUE)
• • read.csv():
• csv_data <- read.csv('file.csv')
• • read_excel() (readxl library):
• library(readxl)
• excel_data <- read_excel('data.xlsx')
Writing Data to Files
• • write.table():
• write.table(data, 'output.txt', sep='t')
• • write.csv():
• write.csv(data, 'output.csv',
row.names=FALSE)
Output Display Functions
• • print(): Basic output
• print('Hello World')
• • cat(): Concatenate and print
• cat('The sum is:', 5 + 3, 'n')
• • message() and warning(): Display system
messages
File Handling Example
• data <- read.csv('input.csv')
• summary(data)
• write.csv(summary(data),
'summary_output.csv')
Plot Output
• • Saving plots:
• png('plot.png')
• plot(x, y)
• dev.off()
Summary
• • readline() and scan() for user input
• • read.csv(), read.table() for file input
• • print() and cat() for output
• • write.csv(), write.table() for saving data
• • Graphs saved using png() or pdf()
References
• • R Documentation: https://www.r-project.org
• • TutorialsPoint: R Input and Output
• • GeeksforGeeks: R Input/Output

Input_and_Output_in_R_Programming_Normal.pptx

  • 1.
    Input and Outputin R Programming • Understanding how R interacts with users and files for input and output operations.
  • 2.
    Introduction • Input andOutput (I/O) are essential for interacting with the user and external data sources. • • Input: To read data from the user or files. • • Output: To display or save data.
  • 3.
    Types of Inputand Output • Input: • • Keyboard input • • File input • • Data frame import • Output: • • Console output • • File output • • Plot output
  • 4.
    Keyboard Input Functions •• readline(): Reads a line from the keyboard • Example: • name <- readline(prompt='Enter your name: ') • print(paste('Hello', name)) • • scan(): Reads multiple numeric or character inputs • nums <- scan() • print(nums)
  • 5.
    Reading Data fromFiles • • read.table(): • data <- read.table('data.txt', header=TRUE) • • read.csv(): • csv_data <- read.csv('file.csv') • • read_excel() (readxl library): • library(readxl) • excel_data <- read_excel('data.xlsx')
  • 6.
    Writing Data toFiles • • write.table(): • write.table(data, 'output.txt', sep='t') • • write.csv(): • write.csv(data, 'output.csv', row.names=FALSE)
  • 7.
    Output Display Functions •• print(): Basic output • print('Hello World') • • cat(): Concatenate and print • cat('The sum is:', 5 + 3, 'n') • • message() and warning(): Display system messages
  • 8.
    File Handling Example •data <- read.csv('input.csv') • summary(data) • write.csv(summary(data), 'summary_output.csv')
  • 9.
    Plot Output • •Saving plots: • png('plot.png') • plot(x, y) • dev.off()
  • 10.
    Summary • • readline()and scan() for user input • • read.csv(), read.table() for file input • • print() and cat() for output • • write.csv(), write.table() for saving data • • Graphs saved using png() or pdf()
  • 11.
    References • • RDocumentation: https://www.r-project.org • • TutorialsPoint: R Input and Output • • GeeksforGeeks: R Input/Output