knitr::opts_chunk$set(echo = TRUE)
(x <- "alfa,bravo,charlie,delta") #> [1] "alfa,bravo,charlie,delta" strsplit(x, split = ",") #> [[1]] #> [1] "alfa" "bravo" "charlie" "delta"
unlist(strsplit(x, split = ",")) #> [1] "alfa" "bravo" "charlie" "delta" strsplit(x, split = ",")[[1]] #> [1] "alfa" "bravo" "charlie" "delta"
strsplit1 <- function(x, split) { strsplit(x, split = split)[[1]] }
use_r("strsplit1") #> • Edit 'R/strsplit1.R' #> • Call `use_test()` to create a matching test file
load_all() #> ℹ Loading regexcite (x <- "alfa,bravo,charlie,delta") #> [1] "alfa,bravo,charlie,delta" strsplit1(x, split = ",") #> [1] "alfa" "bravo" "charlie" "delta"
exists("strsplit1", where = globalenv(), inherits = FALSE) #> [1] FALSE
check()
use_mit_license()
strsplit1 <- function(x, split) { strsplit(x, split = split)[[1]]} document()
install()
library(regexcite) x <- "alfa,bravo,charlie,delta" strsplit1(x, split = ",") #> [1] "alfa" "bravo" "charlie" "delta"
use_testthat() use_test("strsplit1") #> ✔ Writing 'tests/testthat/test-strsplit1.R' #> • Edit 'tests/testthat/test-strsplit1.R'
test()
use_package("stringr")
str_split_one <- function(string, pattern, n = Inf) { stopifnot(is.character(string), length(string) <= 1) if (length(string) == 1) { stringr::str_split(string = string, pattern = pattern, n = n)[[1]] } else { character() } }
rename_files("strsplit1", "str_split_one")
str_split_one <- function(string, pattern, n = Inf) { stopifnot(is.character(string), length(string) <= 1) if (length(string) == 1) { stringr::str_split(string = string, pattern = pattern, n = n)[[1]] } else { character() } }
test_that("str_split_one() splits a string", { expect_equal(str_split_one("a,b,c", ","), c("a", "b", "c")) }) test_that("str_split_one() errors if input length > 1", { expect_error(str_split_one(c("a,b","c,d"), ",")) }) test_that("str_split_one() exposes features of stringr::str_split()", { expect_equal(str_split_one("a,b,c", ",", n = 2), c("a", "b,c")) expect_equal(str_split_one("a.b", stringr::fixed(".")), c("a", "b")) })
document()
load_all() #> ℹ Loading regexcite str_split_one("a, b, c", pattern = ", ") #> [1] "a" "b" "c"
use_readme_rmd()
build_readme()
check()
install()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.