Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/brioPublic

Basic R Input Output

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

r-lib/brio

Repository files navigation

R-CMD-checkCodecov test coverage

Functions to handle basic input output, these functions always read andwrite UTF-8 files and provide more explicit control over line endings.

Reading files

library(brio)write_lines(c("abc","123"),"my-file")# Write with windows newlineswrite_lines(c("abc","123"),"my-file-2",eol="\r\n")file_line_endings("my-file")#> [1] "\n"file_line_endings("my-file-2")#> [1] "\r\n"read_lines("my-file")#> [1] "abc" "123"unlink(c("my-file","my-file-2"))

Drop-ins

brio also hasreadLines() andwriteLines() functions drop-inreplacements forbase::readLines() andbase::writeLines(). Thesefunctions are thin wrappers aroundbrio::read_lines() andbrio::write_lines(), with deliberately fewer features than the baseequivalents. If you want to convert a package to using brio you can addthe following line and re-document.

#' @importFrom brio readLines writeLines

Benchmarks

Speed is not necessarily a goal of brio, but it does end up being a niceside effect.

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 longdata<- 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 tolarger block sizes and avoidance of extra copies.

bench::mark(brio::read_lines("benchmark"),readr::read_lines("benchmark"),base::readLines("benchmark"))#> # A tibble: 3 × 6#>   expression                           min   median `itr/sec` mem_alloc `gc/sec`#>   <bch:expr>                      <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>#> 1 "brio::read_lines(\"benchmark\… 692.28µs 703.68µs     1417.   15.91KB     0#> 2 "readr::read_lines(\"benchmark…   1.79ms   1.89ms      521.    3.73MB     8.30#> 3 "base::readLines(\"benchmark\"…   2.78ms    2.8ms      357.   31.39KB     0

Writing

Write speeds are basically the same regardless of method, though briodoes avoid some extra memory allocations.

bench::mark(brio::write_lines(data,"benchmark"),readr::write_lines(data,"benchmark"),base::writeLines(data,"benchmark"),check=FALSE)#> # A tibble: 3 × 6#>   expression                             min median `itr/sec` mem_alloc `gc/sec`#>   <bch:expr>                         <bch:t> <bch:>     <dbl> <bch:byt>    <dbl>#> 1 "brio::write_lines(data, \"benchm…   476µs  511µs    1378.         0B     0#> 2 "readr::write_lines(data, \"bench…  10.3ms   15ms      66.4     102KB     0#> 3 "base::writeLines(data, \"benchma… 500.8µs  528µs    1718.         0B     2.01unlink("benchmark")

Code of Conduct

Please note that the brio project is released with aContributor Codeof Conduct. Bycontributing to this project, you agree to abide by its terms.

About

Basic R Input Output

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors10

Languages


[8]ページ先頭

©2009-2025 Movatter.jp