read.so: Read data pasted from R printing into a data frame

Description Usage Arguments Details Value Examples

View source: R/so.R

Description

read.so and read_so read data copied from R print methods into a data.frame or tibble, respectively.

Usage

1
2
3
4
5
6
7
8
9
read.so(
  file = clipr::read_clip(),
  header = TRUE,
  na.strings = c("NA", "<NA>"),
  stringsAsFactors = FALSE,
  ...
)

read_so(file = clipr::read_clip(), row_names, na = c("NA", "<NA>"), ...)

Arguments

file

Character. A path to a file, a connection, or literal data (either a single string or a vector of lines). If unspecified, reads from the clipboard.

header

Logical. Does the data include a header row?

na.strings, na

Character. Values to convert to NA.

stringsAsFactors

Logical. Indicates whether to convert string columns to factors. Passed along to read.table().

...

Passed along to read.table() or readr::read_table2() by read.so and read_so, respectively. Applied after tibble formatting lines have been removed.

row_names

Logical. Indicates whether the input contains a column of row names. If missing, guesses based on the number of elements in the header and first row. Row names are removed by read_so; to keep them, use read.so. For row names with spaces, try readr::read_table() or readr::read_fwf().

Details

read.so is designed to read the print method of a data.frame back into R, provided there are no unquoted spaces within the printing. read_so will read the results of printing a data.frame as well, but will return a tibble. Its more significant feature is that it will read in the results of printing a tibble, with or without the "A tibble", type, and additional row and column metadata lines.

The file parameter will accept a filepath or connection, but given that these functions are built for interactive use, they are built to accept a single string containing the data (distinguished from a filepath by the presence of a newline) or a vector of lines, as may be generated by clipr::read_clip().

Value

For read.so a data.frame; for read_so, a tibble.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
lines.df <- capture.output(head(iris))
lines_tbl <- capture.output(head(tibble::as_tibble(iris)))

read.so(lines.df)

read_so(lines.df)

## Not run: 
# Data has extra metadata lines, so normal reading fails:
read.so(lines_tbl)

# ...but can work:
read.so(lines_tbl, comment = '<', skip = 1)

## End(Not run)

# Alternately, use the purpose-built function:
read_so(lines_tbl)

alistaire47/read.so documentation built on April 23, 2020, 12:50 p.m.