-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
120 lines (89 loc) · 2.65 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(readthat)
```
# readthat <img src='man/figures/logo.png' align="right" height="173.5" />
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/readthat)](https://CRAN.R-project.org/package=readthat)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis build status](https://travis-ci.org/mkearney/readthat.svg?branch=master)](https://travis-ci.org/mkearney/readthat)
[![Codecov test coverage](https://codecov.io/gh/mkearney/readthat/branch/master/graph/badge.svg)](https://codecov.io/gh/mkearney/readthat?branch=master)
<!-- badges: end -->
Quickly read text/source from local files and web pages.
## Installation
You can install the development released version of readthat from Github with:
``` r
remotes::install_github("mkearney/readthat")
```
## Examples
Let's say we want to read-in the source of the following websites:
```{r}
## a vector of URLs
urls <- c(
"https://mikewk.com",
"https://cnn.com",
"https://www.cnn.com/us"
)
```
Use `readthat::read()` to read the text/source of a single file/URL
```{r}
## read single web/file (returns text vector)
x <- read(urls[1])
## preview output
substr(x, 1, 60)
## use apply functions to read multiple pages
xx <- sapply(urls, read)
## preview output
lapply(xx, substr, 1, 60)
```
## Comparisons
Benchmark comparison for reading a text file:
```{r}
## save a text file
writeLines(read(urls[1]), x <- tempfile())
## coompare read times
bm_file <- bench::mark(
readr = readr::read_lines(x),
readthat = read(x),
readLines = readLines(x),
check = FALSE
)
## view results
bm_file
```
```{r, include=FALSE}
p1 <- ggplot2::autoplot(bm_file)
p1 + ggplot2::ggsave("man/figures/README-bm_file.png",
width = 9, height = 5, units = "in")
```
![](man/figures/README-bm_file.png)
Benchmark comparison for reading a web page:
```{r}
x <- "https://www.espn.com/nfl/scoreboard"
bm_html <- bench::mark(
httr = httr::content(httr::GET(x), as = "text", encoding = "UTF-8"),
xml2 = xml2::read_html(x),
readthat = read(x),
readLines = readLines(x, warn = FALSE),
readr = readr::read_lines(x),
check = FALSE,
iterations = 25,
filter_gc = TRUE
)
bm_html
```
```{r, include=FALSE}
p2 <- ggplot2::autoplot(bm_html)
p2 + ggplot2::ggsave("man/figures/README-bm_html.png",
width = 9, height = 5, units = "in")
```
![](man/figures/README-bm_html.png)