knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" ) Sys.setenv(CLIPR_ALLOW = TRUE)
read.so is not on CRAN, but you can install it by running
# install.packages("remotes") remotes::install_github("alistaire47/read.so")
Sometimes you see a really interesting question on Stack Overflow (or a Github issue, or RStudio Community), but the asker only presents the data as a presentation-style table instead of as runnable R code, because they, unlike you, don't use reprex. Fear no more! read.so will read even heinous tables into data frames and code in a trice.
For instance, should you want to return output copied from the R console back
into your own session, use read.so
for a data.frame, and read_so
for a
tibble. Pass in a filepath, a raw string of text, a vector of lines, or if the
data is on the clipboard, nothing at all, and the functions will grab it for
you:
library(read.so) iris_lines <- capture.output(head(iris)) iris_lines read.so(iris_lines) read_so(iris_lines) clipr::write_clip(head(iris)) read.so()
Further, read_so
will attempt to read in the results of printing a tibble:
mtcars_lines <- capture.output(tibble::as_tibble(mtcars)) mtcars_lines read_so(mtcars_lines)
When you need to read Markdown tables into R, read.so has you covered
with read.md
and read_md
:
chick_lines <- capture.output( knitr::kable(head(ChickWeight), format = "markdown") ) cat(chick_lines, sep = "\n") read.md(chick_lines) read_md(chick_lines)
They can handle a number of formats, including tables with delimiter rows composed of "-", "=", "+", and whitespace.
str
results back into a data frameIf all you have is the results of calling str
on a data frame, read.str
will read as many complete rows as possible into a new data frame of the same
class as the original:
warp_lines <- capture.output(str(warpbreaks)) warp_lines read.str(warp_lines)
tibble::glimpse
results back into a data frameSimilarly, if the data was printed by tibble::glimpse
, try read.glimpse
or read_glimpse
:
states <- data.frame(state.name, state.abb, state.region, state.division, state.area, center = state.center, state.x77) states_lines <- capture.output(tibble::glimpse(states)) states_lines read_glimpse(states_lines)
The reading functions make it easy to get a question's data into R, but to
construct a reproducible response requires the data in code form. dput
will
produce runnable code, but the result is not very readable. write.so
and its
alias write_so
take a data frame and restructure the results of dput
to
make it an ordinary data.frame
, tibble
, or data.table
call,
and when a name is detected, reassemble assignment to that variable. The call
is printed to the console and by default copied to the clipboard. A language
object version of the call is returned, invisibly.
options(read.so.write_clip = FALSE) # don't write to clipboard by default write.so(head(swiss)) write_so(head(tibble::as_tibble(swiss)), indent = 2)
write.so
. If you like tibble::tribble
, you'll like this package.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.