Compare commits

..

No commits in common. "af426afef8b776950c888cdcc1faa092f9264e26" and "163e9708657222c5902d7541d5831eb34b210b36" have entirely different histories.

10 changed files with 23 additions and 113 deletions

View File

@ -1,7 +1,6 @@
# 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)

View File

@ -8,8 +8,6 @@
* 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

View File

@ -1,38 +0,0 @@
#' 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)
}
}

View File

@ -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",
#' add_padding(rownames(stRoke::talos))),stRoke::talos)
#' sprintf("%03s", 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:

View File

@ -6,6 +6,7 @@
[![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" />

View File

@ -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

View File

@ -1,26 +0,0 @@
% 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))
}

View File

@ -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",
add_padding(rownames(stRoke::talos))),stRoke::talos)
sprintf("\%03s", 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:

View File

@ -1,17 +0,0 @@
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"))
})

View File

@ -6,29 +6,22 @@ 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",
add_padding(rownames(stRoke::talos))),
# ds <- data.frame(nm = paste0("Sub",
# sprintf("%03s", rownames(stRoke::talos))),
# stRoke::talos)
ds <- data.frame(nm = paste0("Sub",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]{3}", label = "grp")),
1),"grp-Sub038-Sub011")
pattern = "Sub([0-9]+)", label = "grp")),
1),"grp-Sub38-Sub11")
expect_equal(
ds[order(ds$nm),] |>
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
chunks_of_n(7, pattern = "Sub([0-9]+)", label = "grp") |>
head(1) |> names(),
"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"
"grp-Sub1-Sub20"
)
## Errors
@ -43,23 +36,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",
add_padding(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",
# sprintf("%03s", 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-Sub038-Sub603")
1),"grp-Sub38-Sub603")
expect_equal(
ds[order(ds$nm), ] |>
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
n_chunks(7, pattern = "Sub([0-9]+)", label = "grp") |>
head(1) |> names(),
"grp-Sub001-Sub072"
"grp-Sub1-Sub72"
)
## Errors