read.so
and read_so
read data copied from R print methods into a
data.frame or tibble, respectively.
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>"), ...)
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 |
stringsAsFactors | Logical. Indicates whether to convert string columns
to factors. Passed along to |
... | Passed along to |
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 |
For read.so
a data.frame; for read_so
, a tibble.
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()
.
lines.df <- capture.output(head(iris)) lines_tbl <- capture.output(head(tibble::as_tibble(iris))) read.so(lines.df)#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosaread_so(lines.df)#> # A tibble: 6 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosaif (FALSE) { # Data has extra metadata lines, so normal reading fails: read.so(lines_tbl) # ...but can work: read.so(lines_tbl, comment = '<', skip = 1) } # Alternately, use the purpose-built function: read_so(lines_tbl)#> # A tibble: 6 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa