Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alistaire47/read.so

Read Tables from Stack Overlow Questions into R
https://github.com/alistaire47/read.so

r stackoverflow

Last synced: 4 days ago
JSON representation

Read Tables from Stack Overlow Questions into R

Awesome Lists containing this project

README

        

---
tags: [r]
output: github_document
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
Sys.setenv(CLIPR_ALLOW = TRUE)
```

# read.so

[![Travis-CI Build Status](https://travis-ci.org/alistaire47/read.so.svg?branch=master)](https://travis-ci.org/alistaire47/read.so)
[![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/17mg5b1yd926krpk?svg=true)](https://ci.appveyor.com/project/alistaire47/read-so)
[![Coverage Status](https://img.shields.io/codecov/c/github/alistaire47/read.so/master.svg)](https://codecov.io/github/alistaire47/read.so?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/read.so)](https://cran.r-project.org/package=read.so)

### Installation

read.so is not on CRAN, but you can install it by running

``` r
# install.packages("remotes")
remotes::install_github("alistaire47/read.so")
```

---

## Read data from Stack Overflow questions into R

Sometimes you see a really interesting question on Stack Overflow (or a Github
issue, or [RStudio Community](https://community.rstudio.com/)), 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](http://reprex.tidyverse.org/). Fear no more! read.so will read even
heinous tables into data frames and code in a trice.

### Read data frame print output back into a data frame

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:

```{r read.so}
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:

```{r read_so, message=FALSE}
mtcars_lines <- capture.output(tibble::as_tibble(mtcars))

mtcars_lines

read_so(mtcars_lines)
```

### Read Markdown tables into data frames

When you need to read Markdown tables into R, read.so has you covered
with `read.md` and `read_md`:

```{r 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.

### Read `str` results back into a data frame

If 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:

```{r}
warp_lines <- capture.output(str(warpbreaks))

warp_lines

read.str(warp_lines)
```

### Read `tibble::glimpse` results back into a data frame

Similarly, if the data was printed by `tibble::glimpse`, try `read.glimpse`
or `read_glimpse`:

```{r}
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)
```

## Generate pretty code to reproduce a data frame

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.

```{r write.so}
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)
```

## Related packages

- [reprex](http://reprex.tidyverse.org/): A great way to generate reproducible
examples. read.so is [mostly] for when people don't use reprex. You should
use reprex.
- [datapasta](https://github.com/MilesMcBain/datapasta): A broader approach to
getting data from the clipboard into R code containing alternatives to
`write.so`. If you like `tibble::tribble`, you'll like this package.