Learn R Programming

params (version 0.7.3)

read_sheet: Read/Write sheets (automatically detect the file type and work accordingly)

Description

Read/Write sheets (automatically detect the file type and work accordingly)

write_sheet requires version 0.3.1.

  • tsv, txt, conf, def: assumed to be tab-delimited

  • csv: assumed to be comma delimited

  • xlsx: microsoft excel, uses openxlsx to read the sheet. Also, it removes extra columns which often creep into excel files.

Usage

read_sheet(
  x,
  id_column,
  start_row = 1,
  sheet = 1,
  ext,
  header = TRUE,
  verbose = FALSE,
  ...
)

write_sheet(x, file, ext, type = "", ...)

Arguments

x

read: path to a file, to be read. write: a data.frame

id_column

all rows which have this column as blank are skipped. See details.

start_row

supplied to read.xlsx

sheet

supplied to read.xlsx, index or name of the sheet to be read from excel file. See read.xlsx

ext

determined using file extension. Specifying will override

header

first line is header? See read.table

verbose

verbosity level.

...

passed onto read.xlsx of openxlsx, read.table or read.csv2 depending on the file type.

file

write: output file name.

type

in case of writing an xlsx file, should the data.frame to written as excel 'table'. ['table']

Details

Note: for excel sheets:

  • If id_column is missing, default if first column

  • If sheet is missing, it automatically reads the first sheet

Some important default values for tsv and csv files:

stringsAsFactors = FALSE,comment.char = '#', strip.white=TRUE, blank.lines.skip=TRUE

Examples

Run this code
# NOT RUN {
## read a excel sheet
sheet = read_sheet(system.file("extdata/example.xlsx", package = "params"))

## read a comma separated sheet
csv = read_sheet(system.file("extdata/example.csv", package = "params"))

## read a tab separate sheet
tsv = read_sheet(system.file("extdata/example.tsv", package = "params"))


# write sheets -------

# }
# NOT RUN {
# throws a R CMD check note - don't run
## write a comma separated sheet
write_sheet(sheet, "example.csv")

## write a tab separated sheet
write_sheet(sheet, "example.tsv")

## write an excel separated sheet
write_sheet(sheet, "example.xlsx")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab