Learn R Programming

depigner

A utility package to help you deal with pigne

Development
CRAN
CI

Pigna [pìn’n’a] is the Italian word for pine cone.[^1] In jargon, it’s used to identify something (like a task…) boring, banal, annoying, painful, frustrating and maybe even with a not so beautiful or rewarding result, just like the obstinate act of trying to challenge yourself in extracting pine nuts from a pine cone, provided that at the end you will find at least one inside it…

Overview

This package aims to provide some useful functions to be used to solve small everyday problems of coding or analyzing data with R. The hope is to provide solutions to that kind of problems which would be normally solved using quick-and-dirty (ugly and maybe even wrong) patches.

Tools CategoryFunction(s)Aim
Harrell’s versetidy_summary()pander-ready data frame from Hmisc::summary()
 paired_test_continuousPaired test for continuous variable into Hmisc::summary
 paired_test_categoricalPaired test for categorical variable into Hmisc::summary
 adjust_p()Adjusts P-values for multiplicity of tests at tidy_summary()
 summary_interact()data frame of OR for interaction from rms::lrm()
 htypes()Will be your variables continuous or categorical in Hmisc::describe()?
Statisticalci2p()Get P-value form estimation and confidence interval
Programmingpb_len()Quick set-up of a progress::progress_bar() progress bar
 install_pkg_set()Politely install set of packages (topic-related sets at ?pkg_sets)
 view_in_excel()Open a data frame in Excel, even in the middle of a pipe chain, on interactive session only
Developmentuse_ui()Activate {usethis} user interface into your own package
 please_install()Politely ask the user to install a package
 imported_from()List packages imported from a package (which has to be installed)
Telegramstart_bot_for_chat()Quick start of a {telegram.bot} Telegram’s bot
 send_to_telegram()Unified wrapper to send someRthing to a Telegram chat
 errors_to_telegram()Divert all your error messages from the console to a Telegram chat
Why not?!gdp()Do you have TOO much pignas in your back?! … try this out ;-)

Installation

You can install the released version of {depigner} from CRAN with:

install.packages("depigner")

You can install the development version from GitHub calling:

# install.packages("devtools")
devtools::install_github("CorradoLanera/depigner")

Next, you can attach it to your session by:

library(depigner)
#> Welcome to depigner: we are here to un-stress you!

Provided Tools

Harrell’s Verse Tools

  • tidy_summary(): produces a data frame from the summary() functions provided by {Hmisc} [@R-Hmisc] and {rms} [@R-rms] packages ready to be pander::pander()ed [@R-pander].

Currently it is tested for method reverse only:

library(rms)
#> Loading required package: Hmisc
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
#> Loading required package: survival
#> Loading required package: lattice
#> Loading required package: ggplot2
#> Loading required package: SparseM
#> 
#> Attaching package: 'SparseM'
#> The following object is masked from 'package:base':
#> 
#>     backsolve
  options(datadist = 'dd')
library(survival)
library(pander)

dd <- datadist(iris)
my_summary <- summary(Species ~., data = iris, method = "reverse")
tidy_summary(my_summary) %>% 
  pander()
 setosa (N=50)versicolor (N=50)virginica (N=50)
Sepal.Length4.800/5.000/5.2005.600/5.900/6.3006.225/6.500/6.900
Sepal.Width3.200/3.400/3.6752.525/2.800/3.0002.800/3.000/3.175
Petal.Length1.400/1.500/1.5754.000/4.350/4.6005.100/5.550/5.875
Petal.Width0.2/0.2/0.31.2/1.3/1.51.8/2.0/2.3


dd <<- datadist(heart) # this to face a package build issue,
                       # use standard `<-` into analyses
surv <- Surv(heart$start, heart$stop, heart$event)
f    <- cph(surv ~ age + year + surgery, data = heart)
my_summary <- summary(f)
tidy_summary(my_summary) %>% 
  pander()
 Diff.HRLower 95% CIUpper 95% CI
age10.691.3361.0091.767
year3.3740.61040.38310.9727
surgery10.52860.25741.085
  • paired_test_*(): Paired test for categorical/continuous variables to be used in the summary() of the {Hmisc} [@R-Hmisc] package:
data(Arthritis)
# categorical -------------------------
## two groups
summary(Treatment ~ Sex,
    data    = Arthritis,
    method  = "reverse",
    test    = TRUE,
    catTest = paired_test_categorical
)
#> 
#> 
#> Descriptive Statistics by Treatment
#> 
#> +----------+--------------------+--------------------+------------------------------+
#> |          |Placebo             |Treated             |  Test                        |
#> |          |(N=43)              |(N=41)              |Statistic                     |
#> +----------+--------------------+--------------------+------------------------------+
#> |Sex : Male|           26%  (11)|           34%  (14)|Chi-square=5.92 d.f.=1 P=0.015|
#> +----------+--------------------+--------------------+------------------------------+
## more than two groups
summary(Improved ~ Sex,
    data    = Arthritis,
    method  = "reverse",
    test    = TRUE,
    catTest = paired_test_categorical
)
#> 
#> 
#> Descriptive Statistics by Improved
#> 
#> +----------+-----------------+-----------------+-----------------+------------------------+
#> |          |None             |Some             |Marked           |  Test                  |
#> |          |(N=42)           |(N=14)           |(N=28)           |Statistic               |
#> +----------+-----------------+-----------------+-----------------+------------------------+
#> |Sex : Male|        40%  (17)|        14%  ( 2)|        21%  ( 6)|chi2=1.71 d.f.=3 P=0.634|
#> +----------+-----------------+-----------------+-----------------+------------------------+

# continuous --------------------------
## two groups
summary(Species ~.,
    data    = iris[iris$Species != "setosa",],
    method  = "reverse",
    test    = TRUE,
    conTest = paired_test_continuous
)
#> 
#> 
#> Descriptive Statistics by Species
#> 
#> +------------+---------------------+---------------------+------------------------+
#> |            |versicolor           |virginica            |  Test                  |
#> |            |(N=50)               |(N=50)               |Statistic               |
#> +------------+---------------------+---------------------+------------------------+
#> |Sepal.Length|    5.600/5.900/6.300|    6.225/6.500/6.900| t=-5.28 d.f.=49 P<0.001|
#> +------------+---------------------+---------------------+------------------------+
#> |Sepal.Width |    2.525/2.800/3.000|    2.800/3.000/3.175| t=-3.08 d.f.=49 P=0.003|
#> +------------+---------------------+---------------------+------------------------+
#> |Petal.Length|    4.000/4.350/4.600|    5.100/5.550/5.875|t=-12.09 d.f.=49 P<0.001|
#> +------------+---------------------+---------------------+------------------------+
#> |Petal.Width |       1.2/1.3/1.5   |       1.8/2.0/2.3   |t=-14.69 d.f.=49 P<0.001|
#> +------------+---------------------+---------------------+------------------------+
## more than two groups
summary(Species ~.,
    data    = iris,
    method  = "reverse",
    test    = TRUE,
    conTest = paired_test_continuous
)
#> 
#> 
#> Descriptive Statistics by Species
#> 
#> +------------+--------------------+--------------------+--------------------+-----------------------+
#> |            |setosa              |versicolor          |virginica           |  Test                 |
#> |            |(N=50)              |(N=50)              |(N=50)              |Statistic              |
#> +------------+--------------------+--------------------+--------------------+-----------------------+
#> |Sepal.Length|   4.800/5.000/5.200|   5.600/5.900/6.300|   6.225/6.500/6.900| F=30.55 d.f.=2 P<0.001|
#> +------------+--------------------+--------------------+--------------------+-----------------------+
#> |Sepal.Width |   3.200/3.400/3.675|   2.525/2.800/3.000|   2.800/3.000/3.175| F=12.63 d.f.=2 P<0.001|
#> +------------+--------------------+--------------------+--------------------+-----------------------+
#> |Petal.Length|   1.400/1.500/1.575|   4.000/4.350/4.600|   5.100/5.550/5.875|F=322.89 d.f.=2 P<0.001|
#> +------------+--------------------+--------------------+--------------------+-----------------------+
#> |Petal.Width |      0.2/0.2/0.3   |      1.2/1.3/1.5   |      1.8/2.0/2.3   |F=234.21 d.f.=2 P<0.001|
#> +------------+--------------------+--------------------+--------------------+-----------------------+
  • adjust_p(): Adjust P-values of a tidy_summary objects:
my_summary <- summary(Species ~., data = iris,
  method = "reverse",
  test = TRUE
)

tidy_summary(my_summary, prtest = "P") %>%
  adjust_p()
#> ✔ P adjusted with BH method.
#> # A tibble: 4 × 5
#>   `&nbsp;`     `setosa \n(N=50)`   `versicolor \n(N=50)` `virginica \n(N=50)`
#>   <chr>        <chr>               <chr>                 <chr>               
#> 1 Sepal.Length "4.800/5.000/5.200" "5.600/5.900/6.300"   "6.225/6.500/6.900" 
#> 2 Sepal.Width  "3.200/3.400/3.675" "2.525/2.800/3.000"   "2.800/3.000/3.175" 
#> 3 Petal.Length "1.400/1.500/1.575" "4.000/4.350/4.600"   "5.100/5.550/5.875" 
#> 4 Petal.Width  "   0.2/0.2/0.3"    "   1.2/1.3/1.5"      "   1.8/2.0/2.3"    
#> # ℹ 1 more variable: `P-value` <chr>
  • summary_interact(): Produce a data frame of OR (with the corresponding CI95%) for the interactions between different combination of a continuous variable (for which it is possible to define the reference and the target values) and (every or a selection of levels of) a categorical one in a logistic model provided by lrm() (from the {rms} package [@R-rms]):
data("transplant", package = "survival")
censor_rows <- transplant[['event']] != 'censored' 
transplant <- droplevels(transplant[censor_rows, ])

dd <<- datadist(transplant) # this to face a package build issue,
                            # use standard `<-` into analyses

lrm_mod <- lrm(event ~ rcs(age, 3)*(sex + abo) + rcs(year, 3),
  data = transplant
)
summary_interact(lrm_mod, age, abo) %>%
  pander()
 LowHighDiff.Odds RatioLower 95% CIUpper 95% CI
age - A4358151.0020.5571.802
age - B4358151.8170.744.463
age - AB4358150.6350.1862.169
age - O4358150.6450.3521.182

summary_interact(lrm_mod, age, abo, p = TRUE) %>%
  pander()
 LowHighDiff.Odds RatioLower 95% CIUpper 95% CIP-value
age - A4358151.0020.5571.8020.498
age - B4358151.8170.744.4630.137
age - AB4358150.6350.1862.1690.728
age - O4358150.6450.3521.1820.883
  • htypes() and friends: get/check types of variable with respect to the {Hmisc} ecosystem [@R-Hmisc].
htypes(mtcars)
#>    mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear   carb 
#>  "con" "none"  "con"  "con"  "con"  "con"  "con"  "cat"  "cat" "none" "none"

desc <- Hmisc::describe(mtcars)
htypes(desc)
#>    mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear   carb 
#>  "con" "none"  "con"  "con"  "con"  "con"  "con"  "cat"  "cat" "none" "none"
htype(desc[[1]])
#> [1] "con"
is_hcat(desc[[1]])
#> [1] FALSE
is_hcon(desc[[1]])
#> [1] TRUE

Statistical Tools

  • ci2p(): compute the p-value related with a provided confidence interval:
ci2p(1.125, 0.634,  1.999, log_transform = TRUE)
#> [1] 0.367902

Programming Tools

  • pb_len(): Progress bar of given length, wrapper from the {progress} [@R-progress] package:
pb <- pb_len(100)

for (i in 1:100) {
    Sys.sleep(0.1)
    tick(pb, paste("i = ", i))
}
  • install_pkg_set(): Simple and polite wrapper to install sets of packages. Moreover, {depigner} provides some sets already defined for common scenario in R (analyses, production, documenting, …). See them by call ?pgk_sets.
install_pkg_set() # this install the whole `?pkg_all`
install_pkg_set(pkg_stan)

?pkg_sets
  • view_in_excel(): A pipe-friendly function to view a data frame in Excel, optimal when used in the middle of a pipe-chain to see intermediate results. It works in interactive session only, so it is RMarkdown/Quarto friendly too!
four_cyl_cars <- mtcars %>%
  view_in_excel() %>%
  dplyr::filter(cyl == 4) %>%
  view_in_excel()

four_cyl_cars

Development Tools

  • use_ui(): Use {usethis}’ user interface [@R-usethis] in your package
# in the initial setup steps of the development of a package
use_ui()
  • lease_install(): This is a polite wrapper to install.packages() inspired (= w/ very minimal modification) by a function Hadley showed us during a course.
a_pkg_i_miss <- setdiff(available.packages(), installed.packages())[[1]]
please_install(a_pkg_i_miss)
  • imported_from(): If you would like to know which packages are imported by a package (eg to know which packages are required for its installation or either installed during it) you can use this function
imported_from("depigner")
#>  [1] "desc"         "dplyr"        "fs"           "ggplot2"      "Hmisc"       
#>  [6] "magrittr"     "progress"     "purrr"        "readr"        "rlang"       
#> [11] "rms"          "rprojroot"    "stats"        "stringr"      "telegram.bot"
#> [16] "tibble"       "tidyr"        "usethis"      "utils"

Telegram Tools

  • Wrappers to simple use of Telegram’s bots: wrappers from the {telegram.bot} package [@R-telegram.bot]:
# Set up a Telegram bot. read `?start_bot_for_chat`
start_bot_for_chat()

# Send something to telegram
send_to_telegram("hello world")

library(ggplot2)
gg <- ggplot(mtcars, aes(x = mpg, y = hp, colour = cyl)) +
    geom_point()
send_to_telegram(
  "following an `mtcars` coloured plot",
  parse_mode = "Markdown"
)
send_to_telegram(gg)

# Divert output errors to the telegram bot
errors_to_telegram()

Why Not?!

  • gdp(): A wrapper to relax
gdp(7)

Feature request

If you need some more features, please open an issue here.

Bug reports

If you encounter a bug, please file a reprex (minimal reproducible example) here.

Code of Conduct

Please note that the depigner project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Acknowledgements

The {depigner}’s logo was lovely designed by Elisa Sovrano.

Reference

[^1]: You can find all the possible meanings of pigna here, and you can listen how to pronounce it here. Note: the Italian plural for “pigna” is “pigne” [pìn’n’e].

Copy Link

Version

Install

install.packages('depigner')

Monthly Downloads

244

Version

0.9.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Corrado Lanera

Last Published

April 24th, 2023

Functions in depigner (0.9.1)

summary_interact

summary_interact
is_hdesc

Checks for describe objects
start_bot_for_chat

Set up a Telegram bot
please_install

Please install
paired_test_continuous

Paired test for continuous variables
%>%

Pipe operator
tidy_summary

tidy_summary
pb_len

Progress bar of given length
paired_test_categorical

Paired test for categorical variables
send_to_telegram

Send something to telegram
use_ui

Use usethis'ui(s) in your package
pkg_sets

Packages' sets
view_in_excel

View in Excel
imported_from

Packages imported by a package
check_for_bot_options

Check if a bot is set up
depigner-package

Utilities for _pigna_s
errors_to_telegram

Divert output errors to the telegram bot
adjust_p

Adjust P-values
install_pkg_set

Check basic installed packages
ci2p

ci2p
Arthritis

Data for example
htype

Type's checks accordingly to [Hmisc] package
gdp

GDP