mirror of
https://github.com/agdamsbo/stRoke.git
synced 2024-11-22 13:00:23 +01:00
Compare commits
5 Commits
163e970865
...
af426afef8
Author | SHA1 | Date | |
---|---|---|---|
af426afef8 | |||
d49a6a1d96 | |||
023738981d | |||
ec38ffa51b | |||
8c65c5e13b |
@ -1,6 +1,7 @@
|
||||
# Generated by roxygen2: do not edit by hand
|
||||
|
||||
S3method(print,win_Prob)
|
||||
export(add_padding)
|
||||
export(age_calc)
|
||||
export(chunks_of_n)
|
||||
export(ci_plot)
|
||||
|
2
NEWS.md
2
NEWS.md
@ -8,6 +8,8 @@
|
||||
|
||||
* NEW: `str_extract()` will extract the substring of a character string given by a regex pattern. Came to be as a helper function for labelling chunks in `chunks_of_n()`, but will be useful on its own. Other functions doing the same exists, but this is my take only using base _R_. Draws on `REDCapCAST::strsplitx()`, where splits can be performed around a pattern.
|
||||
|
||||
* NEW: `add_padding()` was created out of frustration. I wanted to add padding using `sprintf("%0s",string)`, in examples for the above, but it would fail when rendering on Windows. Say hello to another function. Just very small. Defaults to adding leading zeros, to get all string to equal length with the longer string supplied.
|
||||
|
||||
* Deprecation: `ds2dd()` moved to `REDCapCAST::ds2dd()` as this is where it belongs.
|
||||
|
||||
# stRoke 23.6.3
|
||||
|
38
R/add_padding.R
Normal file
38
R/add_padding.R
Normal file
@ -0,0 +1,38 @@
|
||||
#' Add padding to string
|
||||
#'
|
||||
#' @param d vector of strings or numbers
|
||||
#' @param length final string length
|
||||
#' @param after if padding should be added after as opposed to default before
|
||||
#' @param pad padding string of length 1
|
||||
#'
|
||||
#' @return vector or character strings of same length.
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' add_padding(sample(1:200,5))
|
||||
add_padding <- function(d,length=NULL,after=FALSE,pad="0"){
|
||||
if (!is.vector(d)) {
|
||||
stop("Please supply vector")
|
||||
}
|
||||
|
||||
if (nchar(pad)!=1) {
|
||||
stop("Padding value should be just a single character or digit")
|
||||
}
|
||||
|
||||
ns <- nchar(d)
|
||||
|
||||
if (is.null(length)){
|
||||
l <- max(ns)
|
||||
} else {
|
||||
l <- length
|
||||
}
|
||||
|
||||
ps <- unlist(lapply(l-ns,function(i){
|
||||
paste(rep(pad,i),collapse="")}))
|
||||
|
||||
if (after) {
|
||||
paste0(d,ps)
|
||||
} else {
|
||||
paste0(ps,d)
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
#' tail(chunks_of_n(seq_len(100),7),3)
|
||||
#' tail(chunks_of_n(seq_len(100),7,even=TRUE),3)
|
||||
#' ds <- data.frame(nm=paste0("Sub",
|
||||
#' sprintf("%03s", rownames(stRoke::talos))),stRoke::talos)
|
||||
#' add_padding(rownames(stRoke::talos))),stRoke::talos)
|
||||
#' head(chunks_of_n(ds,7,pattern="Sub[0-9]{3}",label="grp"),2)
|
||||
#' ## Please notice that no sorting is performed. This is on purpose to preserve
|
||||
#' ## original sorting. If sorting is intended, try something like this:
|
||||
|
@ -6,7 +6,6 @@
|
||||
[![Page deployed](https://github.com/agdamsbo/stRoke/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/agdamsbo/stRoke/actions/workflows/pages/pages-build-deployment)
|
||||
[![Codecov test coverage](https://codecov.io/gh/agdamsbo/stRoke/branch/main/graph/badge.svg)](https://app.codecov.io/gh/agdamsbo/stRoke?branch=main)
|
||||
[![CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/stRoke)](https://cran.r-project.org/package=stRoke)
|
||||
[![R-CMD-check](https://github.com/agdamsbo/stRoke/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/agdamsbo/stRoke/actions/workflows/R-CMD-check.yaml)
|
||||
<!-- badges: end -->
|
||||
|
||||
# stRoke package <img src="man/figures/logo.png" align="right" />
|
||||
|
@ -5,10 +5,10 @@ coverage:
|
||||
project:
|
||||
default:
|
||||
target: auto
|
||||
threshold: 1%
|
||||
threshold: .1%
|
||||
informational: true
|
||||
patch:
|
||||
default:
|
||||
target: auto
|
||||
threshold: 1%
|
||||
threshold: .1%
|
||||
informational: true
|
||||
|
26
man/add_padding.Rd
Normal file
26
man/add_padding.Rd
Normal file
@ -0,0 +1,26 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/add_padding.R
|
||||
\name{add_padding}
|
||||
\alias{add_padding}
|
||||
\title{Add padding to string}
|
||||
\usage{
|
||||
add_padding(d, length = NULL, after = FALSE, pad = "0")
|
||||
}
|
||||
\arguments{
|
||||
\item{d}{vector of strings or numbers}
|
||||
|
||||
\item{length}{final string length}
|
||||
|
||||
\item{after}{if padding should be added after as opposed to default before}
|
||||
|
||||
\item{pad}{padding string of length 1}
|
||||
}
|
||||
\value{
|
||||
vector or character strings of same length.
|
||||
}
|
||||
\description{
|
||||
Add padding to string
|
||||
}
|
||||
\examples{
|
||||
add_padding(sample(1:200,5))
|
||||
}
|
@ -28,7 +28,7 @@ Split to chunks of size n
|
||||
tail(chunks_of_n(seq_len(100),7),3)
|
||||
tail(chunks_of_n(seq_len(100),7,even=TRUE),3)
|
||||
ds <- data.frame(nm=paste0("Sub",
|
||||
sprintf("\%03s", rownames(stRoke::talos))),stRoke::talos)
|
||||
add_padding(rownames(stRoke::talos))),stRoke::talos)
|
||||
head(chunks_of_n(ds,7,pattern="Sub[0-9]{3}",label="grp"),2)
|
||||
## Please notice that no sorting is performed. This is on purpose to preserve
|
||||
## original sorting. If sorting is intended, try something like this:
|
||||
|
17
tests/testthat/test-add_padding.R
Normal file
17
tests/testthat/test-add_padding.R
Normal file
@ -0,0 +1,17 @@
|
||||
test_that("chunks_of_n returns correct", {
|
||||
expect_length(add_padding(sample(1:200,5)),5)
|
||||
|
||||
expect_equal(nchar(add_padding(sample(1:200,5),5)), rep(5,5))
|
||||
|
||||
expect_equal(nchar(add_padding(
|
||||
sample(1:200, 5), length = 5, after = TRUE
|
||||
)), rep(5, 5))
|
||||
|
||||
|
||||
## Errors
|
||||
expect_error(add_padding(matrix(sample(1:200,5)),5))
|
||||
|
||||
expect_error(add_padding(matrix(sample(1:200,5)),5,pad = "123"))
|
||||
|
||||
|
||||
})
|
@ -6,22 +6,29 @@ test_that("chunks_of_n returns correct", {
|
||||
use.names = FALSE), c(6, 6, 6, 6, 6))
|
||||
|
||||
# This is the example from the function, but I believe it fails in GitHub testing
|
||||
# ds <- data.frame(nm = paste0("Sub",
|
||||
# sprintf("%03s", rownames(stRoke::talos))),
|
||||
# stRoke::talos)
|
||||
|
||||
ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
|
||||
ds <- data.frame(nm = paste0("Sub",
|
||||
add_padding(rownames(stRoke::talos))),
|
||||
stRoke::talos)
|
||||
|
||||
# ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
|
||||
# stRoke::talos)
|
||||
|
||||
expect_equal(head(names(chunks_of_n(ds, 7,
|
||||
pattern = "Sub([0-9]+)", label = "grp")),
|
||||
1),"grp-Sub38-Sub11")
|
||||
pattern = "Sub[0-9]{3}", label = "grp")),
|
||||
1),"grp-Sub038-Sub011")
|
||||
|
||||
expect_equal(
|
||||
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
|
||||
ds[order(ds$nm),] |>
|
||||
chunks_of_n(7, pattern = "Sub([0-9]+)", label = "grp") |>
|
||||
head(1) |> names(),
|
||||
"grp-Sub1-Sub20"
|
||||
"grp-Sub001-Sub020"
|
||||
)
|
||||
|
||||
expect_equal(
|
||||
ds[order(ds$nm),] |>
|
||||
chunks_of_n(7, pattern = "Sub[0-9]{3}", label = "grp") |>
|
||||
head(1) |> names(),
|
||||
"grp-Sub001-Sub020"
|
||||
)
|
||||
|
||||
## Errors
|
||||
@ -36,23 +43,23 @@ test_that("n_chunks returns correct", {
|
||||
expect_equal(lengths(n_chunks(seq_len(30), 7, even = TRUE),
|
||||
use.names = FALSE), rep(5,6))
|
||||
|
||||
# This is the example from the function, but I believe it fails in GitHub testing
|
||||
# ds <- data.frame(nm = paste0("Sub",
|
||||
# sprintf("%03s", rownames(stRoke::talos))),
|
||||
# stRoke::talos)
|
||||
|
||||
ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
|
||||
## This is the example from the function, but I believe it fails in GitHub testing
|
||||
ds <- data.frame(nm = paste0("Sub",
|
||||
add_padding(rownames(stRoke::talos))),
|
||||
stRoke::talos)
|
||||
|
||||
# ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
|
||||
# stRoke::talos)
|
||||
|
||||
expect_equal(head(names(n_chunks(ds, 7,
|
||||
pattern = "Sub([0-9]+)", label = "grp")),
|
||||
1),"grp-Sub38-Sub603")
|
||||
1),"grp-Sub038-Sub603")
|
||||
|
||||
expect_equal(
|
||||
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
|
||||
ds[order(ds$nm), ] |>
|
||||
n_chunks(7, pattern = "Sub([0-9]+)", label = "grp") |>
|
||||
head(1) |> names(),
|
||||
"grp-Sub1-Sub72"
|
||||
"grp-Sub001-Sub072"
|
||||
)
|
||||
|
||||
## Errors
|
||||
|
Loading…
Reference in New Issue
Block a user