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:
|
|
Load it:
|
|
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…
|
|
… and that I wish to create a color scale with 25 tones. I use showPalette() to show the color palette:
|
|
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!
|
|
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 analyze 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.
|
|
|
|
|
|
|
|
These functions help me structure my scripts. Here is a contrived example:
|
|
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
|
|
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.
|
|
| 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.
|
|
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).
|
|
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 🎉!
Display information relative to the R session used to render this post.
|
|