inSilecoMisc 0.4.0 (part 2/2)

[This article was first published on R-bloggers on inSileco, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the first part of this post I
introduced several functions available in the package inSilecoMisc. In this post, I keep on introducing features of the package you
might find useful! If you did not read the first part of this post and are
interested in reproducing the examples below, simply install inSilecoMisc:

# Run if package is not already installed
install.packages("remotes")
remotes::install_github("inSileco/inSilecoMisc")

Load it:

library("inSilecoMisc")
packageVersion("inSilecoMisc")
#R> [1] '0.4.0'

and you’re good to go!

scaleWithin()

I wrote scaleWithin() to handle color scales for a specific yet frequent situation. Let us say that I have 40 percentage values – meaning 0 to 100 – in a vector val

val <- runif(40, 0, 100)
val
#R>  [1]  9.003099 90.257697 65.175491 42.780934 74.135713 89.142560 49.916612
#R>  [8] 78.692421 64.176170 49.876287 87.883136 95.740987 26.597048 33.802412
#R> [15] 11.971489 85.233280 87.448615 20.995311 21.208502 52.727318 97.502782
#R> [22] 38.735630 18.807955 21.397138 82.067280 30.170049  7.384720 66.160372
#R> [29] 82.295189 18.599729  6.061537 22.118283 72.039487 15.467621 24.042791
#R> [36] 37.464883  1.004264 31.025760 30.801622  8.517938

and that I wish to create a color scale with 25 tones. I use showPalette() to show the color palette:

pal <- colorRampPalette(c("#f9fa98", "#500127"))(25)
graphicsutils::showPalette(pal)

But the color scale should be used for the range [30%-70%], meaning that values below 30% should have the lowest values and values above 70%, the highest one. The caption should thus indicate \(\geqslant\) 30% and \(\leqslant\) 70%. Then the function scaleWithin() is very handy!

scaleWithin(val, n = 25, mn = 30, mx = 70)
#R>  [1]  1 25 22  8 25 25 13 25 22 13 25 25  1  3  1 25 25  1  1 15 25  6  1  1 25
#R> [26]  1  1 23 25  1  1  1 25  1  1  5  1  1  1  1
graphicsutils::showPalette(pal[scaleWithin(val, n = 25, mn = 30, mx = 70)], add_codecolor = FALSE)

Even though this function is pretty useful – at least I think it is! – I had a
lot of trouble conveying why! So, in the last version of inSilecoMisc, I
re-wrote the entire documentation and I hope that, together with this example,
others will, as I so, find it useful.

Messages

Daily, I use R packages and R functions to analyse data, create
model, run simulations, and a number of other things! So I write scripts that combine functions from
various packages to create pipelines that do the analyses I need. When running
such scripts, I like having information reported on a clear and visual way, that is why I value packages such as
progress, crayon and cli. In
inSilecoMisc, inspired by messages reported by devtools when
building a package, I created four simple message functions using crayon and cli packages to standardize messages in my scripts.

# 1. msgInfo() indicates what the upcoming computation
msgInfo("this is what's gonna happen next")
#R> ℹ this is what's gonna happen next

# 2. msgWarning() reminds me something important that should not affect the run
msgWarning("Got to be careful")
#R> ⚠ Got to be careful

# 3. msgError() when something went wrong (and I anticipated that it could happen)
msgError("Something wrong")
#R> ✖ Something wrong

# 4. msgSuccess() when a step/ a computation has been successfully completed
msgSuccess("All good")
#R> ✔ All good

These functions help me structure my scripts. Here is a contrived example:

scr_min <- function() {
  # msgInfo() lets me know where I am in the script
  msgInfo("Average random values")
  set.seed(111)
  out <- mean(runif(100))
  msgSuccess("Done!")
  # msgSuccess() indicates the successful completion of this part
  out
}
scr_min()
#R> ℹ Average random values
#R> ✔ Done!
#R> [1] 0.4895239

Another helpful aspect of these functions is that they all are based on message(). As such, if I want to execute a script quietly, all I need to do is to call suppressMessages() beforehand

# quiet run
suppressMessages(scr_min())
#R> [1] 0.4895239

If you want to see an example of how I use these functions in a script for a scientific manuscript,
check out the research compendium
coocNotInteract.

tblDown()

Last but not least, I’d like to introduce a function to quickly write table data
frame (or a list of data frames) in documents of various formats. I created
tblDown()
for a colleague of mine that was looking for a quick way to export a table. In
the package knitr, there is the very handy function kable()
that quickly writes a data frame in various formats.

knitr::kable(head(CO2))

Plant Type Treatment conc uptake
Qn1 Quebec nonchilled 95 16.0
Qn1 Quebec nonchilled 175 30.4
Qn1 Quebec nonchilled 250 34.8
Qn1 Quebec nonchilled 350 37.2
Qn1 Quebec nonchilled 500 35.3
Qn1 Quebec nonchilled 675 39.2

I wrote a function that calls kable() to write the data frame
and then renders the table(s) in the
desired format indicated by the extension of the output file (docx by
default) using pandoc.

# NB tblDown(head(CO2)) returns table.docx by default
tblDown(head(CO2), output_file = "table.odt")

table_odt.png (screenshot)
table_odt.png (screenshot)

As I mentioned above tblDown() handles lists of data frames and the user can also provide a set of captions for every table and even separate them with section headers (of level 1).

tblDown(list(head(CO2), tail(CO2)), output_file = "tables.pdf",
  caption = c("This is the head of CO2", "This is the tail of CO2"),
  section = "Table")
#R> `section` and `x` have different lengths

Check out the output file ➡ ! Note that in the example above I only use one
character string for section and tblDown() has appended an index; this is
also the default behavior for caption: if there are less captions
or sections titles than data frames, vectors of captions (and/or sections) are repeated and an index is appended.

If you are already writing your documents with R
Markdown
, you may not need this. Yet keep in mind that
tblDown() quickly exports tables in various formats with only one line of
command!

That’s all folks 🎉!

Session info

sessionInfo()
#R> R version 4.0.0 (2020-04-24)
#R> Platform: x86_64-pc-linux-gnu (64-bit)
#R> Running under: Ubuntu 20.04 LTS
#R> 
#R> Matrix products: default
#R> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
#R> 
#R> locale:
#R>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#R>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#R>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
#R>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#R>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#R> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#R> 
#R> attached base packages:
#R> [1] stats     graphics  grDevices utils     datasets  methods   base     
#R> 
#R> other attached packages:
#R> [1] inSilecoMisc_0.4.0     inSilecoRef_0.0.1.9000
#R> 
#R> loaded via a namespace (and not attached):
#R>  [1] Rcpp_1.0.4             highr_0.8              pillar_1.4.3          
#R>  [4] compiler_4.0.0         later_1.0.0            plyr_1.8.6            
#R>  [7] tools_4.0.0            digest_0.6.25          lifecycle_0.2.0       
#R> [10] tibble_3.0.0           lubridate_1.7.4        jsonlite_1.6.1        
#R> [13] evaluate_0.14          rcrossref_1.0.0        pkgconfig_2.0.3       
#R> [16] rlang_0.4.5            bibtex_0.4.2.2         cli_2.0.2             
#R> [19] shiny_1.4.0.2          crul_0.9.0             curl_4.3              
#R> [22] yaml_2.2.1             blogdown_0.18          xfun_0.12             
#R> [25] fastmap_1.0.1          RefManageR_1.2.12      httr_1.4.1            
#R> [28] stringr_1.4.0          dplyr_0.8.5            xml2_1.3.1            
#R> [31] knitr_1.28             graphicsutils_1.4.9000 vctrs_0.2.4           
#R> [34] htmlwidgets_1.5.1      tidyselect_1.0.0       DT_0.13               
#R> [37] glue_1.4.0             httpcode_0.2.0         R6_2.4.1              
#R> [40] fansi_0.4.1            rmarkdown_2.1          bookdown_0.18         
#R> [43] purrr_0.3.3            magrittr_1.5           ellipsis_0.3.0        
#R> [46] promises_1.1.0         htmltools_0.4.0        assertthat_0.2.1      
#R> [49] mime_0.9               xtable_1.8-4           httpuv_1.5.2          
#R> [52] stringi_1.4.6          miniUI_0.1.1.1         crayon_1.3.4

To leave a comment for the author, please follow the link and comment on their blog: R-bloggers on inSileco.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)