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

Share:

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

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

write_sheet(x, file, ext, ...)

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 extention. 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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## read a excel sheet
sheet = read_sheet(system.file("extdata/example.xlsx", package = "params"))

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

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


# write sheets -------

## write a comma seperated sheet
write_sheet(sheet, "example.csv")

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

## write an excel seperated sheet
write_sheet(sheet, "example.xlsx")