https://github.com/r-lib/brio
Basic R Input Output
https://github.com/r-lib/brio
Last synced: 16 days ago
JSON representation
Basic R Input Output
- Host: GitHub
- URL: https://github.com/r-lib/brio
- Owner: r-lib
- License: other
- Created: 2020-03-20T19:53:01.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-05T16:00:59.000Z (9 months ago)
- Last Synced: 2025-03-29T07:05:10.033Z (23 days ago)
- Language: R
- Homepage: https://brio.r-lib.org/
- Size: 5.39 MB
- Stars: 57
- Watchers: 4
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
- jimsghstars - r-lib/brio - Basic R Input Output (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# brio - Basic R Input Output
[](https://app.codecov.io/gh/r-lib/brio?branch=main)
[](https://github.com/r-lib/brio/actions/workflows/R-CMD-check.yaml)Functions to handle basic input output, these functions always
read and write UTF-8 files and provide more explicit control over line endings.## Reading files
```{r}
library(brio)
write_lines(c("abc", "123"), "my-file")# Write with windows newlines
write_lines(c("abc", "123"), "my-file-2", eol = "\r\n")file_line_endings("my-file")
file_line_endings("my-file-2")
read_lines("my-file")
unlink(c("my-file", "my-file-2"))
```## Drop-ins
brio also has `readLines()` and `writeLines()` functions drop-in replacements
for `base::readLines()` and `base::writeLines()`. These functions are thin
wrappers around `brio::read_lines()` and `brio::write_lines()`, with
deliberately fewer features than the base equivalents. If you want to convert a
package to using brio you can add the following line and re-document.```r
#' @importFrom brio readLines writeLines
```## Benchmarks
Speed is not necessarily a goal of brio, but it does end up being a nice side effect.
```{r}
gen_random <- function(characters, num_lines, min, max) {
line_lengths <- sample.int(max - min, num_lines, replace = TRUE) + min
vapply(line_lengths, function(len) paste(sample(characters, len, replace = TRUE), collapse = ""), character(1))
}set.seed(42)
# generate 1000 random lines between 100-1000 characters long
data <- gen_random(letters, 1000, min = 100, max = 1000)brio::write_lines(data, "benchmark")
```### Reading
Reading speeds are a decent amount faster with brio, mainly due to larger block sizes and avoidance of extra copies.
```{r}
bench::mark(
brio::read_lines("benchmark"),
readr::read_lines("benchmark"),
base::readLines("benchmark")
)
```### Writing
Write speeds are basically the same regardless of method, though brio does avoid some extra memory allocations.
```{r}
bench::mark(
brio::write_lines(data, "benchmark"),
readr::write_lines(data, "benchmark"),
base::writeLines(data, "benchmark"),
check = FALSE
)unlink("benchmark")
```## Code of Conduct
Please note that the brio project is released with a
[Contributor Code of Conduct](https://brio.r-lib.org/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.