Title: | Helper Functions for Rmd Documents |
---|---|
Description: | A series of functions to aid in repeated tasks for Rmd documents. All details are to my personal preference, though I am happy to add flexibility if there are use cases I am missing. I will continue updating with new functions as I add utility functions for myself. |
Authors: | Mark Peterson |
Maintainer: | Mark Peterson <[email protected]> |
License: | GPL |
Version: | 1.3.1 |
Built: | 2025-03-09 02:40:04 UTC |
Source: | https://github.com/cran/rmdHelpers |
A series of functions to aid in repeated tasks for Rmd documents. All details are to my personal preference, though I am happy to add flexibility if there are use cases I am missing. I will continue updating with new functions as I add utility functions for myself.
The DESCRIPTION file:
Package: | rmdHelpers |
Type: | Package |
Title: | Helper Functions for Rmd Documents |
Version: | 1.3.1 |
Date: | 2024-07-11 |
Author: | Mark Peterson |
Maintainer: | Mark Peterson <[email protected]> |
Description: | A series of functions to aid in repeated tasks for Rmd documents. All details are to my personal preference, though I am happy to add flexibility if there are use cases I am missing. I will continue updating with new functions as I add utility functions for myself. |
License: | GPL |
Depends: | knitr, dplyr |
NeedsCompilation: | no |
Packaged: | 2024-07-11 20:40:49 UTC; mark |
Date/Publication: | 2024-07-11 21:00:02 UTC |
Repository: | https://petersmp.r-universe.dev |
RemoteUrl: | https://github.com/cran/rmdHelpers |
RemoteRef: | HEAD |
RemoteSha: | b10d28eb122fa68f300f62247ce3c8b77d6d9076 |
Index of help topics:
formatEffectTable Format an effect table formatP Format p-value myFrac Print Fraction for markdown myKable Wrapper for kable printList Print nice lists refNote Generate a popup reference note rmdHelpers-package Helper Functions for Rmd Documents thisFileName Identify current file thisfile_knit Identify the file currently being knitted
Basic functions that I use in multiple Rmd documents
Mark Peterson
Maintainer: Mark Peterson <[email protected]>
Performs general formatting of effect tables from 'lm' suitable for basic printing. This includes merging estimates and confidence intervals, rounding, and optionally improving the display of factor and logical columns (using a colon and space, instead of just concatenating them).
formatEffectTable(object , level = 0.95 , estDigits = 2 , pDigits = 4 , cleanFactors = TRUE)
formatEffectTable(object , level = 0.95 , estDigits = 2 , pDigits = 4 , cleanFactors = TRUE)
object |
A fitted model object from 'lm' |
level |
The confidence level to be returned |
estDigits |
The number of digits to be displayed for the estimate and confidence thresholds |
pDigits |
The number of digits to be displayed for the p-values |
cleanFactors |
Logical. Should the parameter names for factors be cleaned by separating the parameter from the value with a colon and a space, or not. (This option may lead to issues with merging if set to 'TRUE'.) |
A data.frame formatted ready to be displayed (e.g. by 'kable')
Note that "NA" values are silently dropped (they are not returned by 'summary.lm' in the coefficients table).
Mark Peterson
irisMod <- lm(Sepal.Length ~ ., data = iris) formatEffectTable(irisMod)
irisMod <- lm(Sepal.Length ~ ., data = iris) formatEffectTable(irisMod)
A wrapper to sensibly control the printing of p-values
because I was frustrated with playing with round
.
Calls format
instead now.
formatP(p, digits = 3, scientific = FALSE, ...)
formatP(p, digits = 3, scientific = FALSE, ...)
p |
Numeric vector of values to be displayed |
digits |
Numeric of length one giving the number of digits to display.
Note that if |
scientific |
Logical, should scientific notation be used? |
... |
Further arguments passed to |
Character vector of the p-value(s) formatted
Mark Peterson
formatP(0.049865465646) formatP(0.00000013212354) formatP(c(0.01564643131,.0003456463131, .45643131564), 2)
formatP(0.049865465646) formatP(0.00000013212354) formatP(c(0.01564643131,.0003456463131, .45643131564), 2)
Converts fractions for inline rendering.
myFrac(num, denom, format = "markdown")
myFrac(num, denom, format = "markdown")
num |
Either a vector of values for the numerators,
or, if |
denom |
Vector of values for the denominators, or |
format |
Character vector of length one giving the format of the output.
Default of "markdown" uses super script for the numerator,
a slash, then subscript for the denominator.
Alternatively, "latex" uses |
Character vector of the fractions
If passing fractions, the function assumes that there is exactly one "/" in each fraction, and will return "NA" for the denominator if none is present or truncate the fraction if more than one "/" is included.
Mark Peterson
myFrac(3,4) myFrac(1:3,4:6) myFrac(1:3,4:6, "latex") myFrac(letters[1:5], LETTERS[1:5]) myFrac( c("1/2","3/4", "9856/5646") ) myFrac( c("1/2","3/4", "9856/5646"), format = "latex" )
myFrac(3,4) myFrac(1:3,4:6) myFrac(1:3,4:6, "latex") myFrac(letters[1:5], LETTERS[1:5]) myFrac( c("1/2","3/4", "9856/5646") ) myFrac( c("1/2","3/4", "9856/5646"), format = "latex" )
A small wrapper for the knitr kable function to allow automated bolding of row and/or column names. Additional functionality may be added/
myKable(x, row.names = NA, boldRowNames = TRUE, boldColNames = TRUE, ...)
myKable(x, row.names = NA, boldRowNames = TRUE, boldColNames = TRUE, ...)
x |
Table or matrix to be passed to |
row.names |
Logical: should row names be included?
Defaults to |
boldRowNames |
Logical: should row names be bolded? |
boldColNames |
Logical: should column names be bolded? |
... |
Additional arguments to be passed to |
Currently bolds in markdown format, so needs to be passed through interpreter after running.
A formatted table from kable
Mark Peterson
tempTable <- matrix(LETTERS[6:20], nrow = 5) colnames(tempTable) <- LETTERS[24:26] row.names(tempTable) <- LETTERS[1:5] myKable(tempTable) myKable(tempTable, boldColNames = FALSE)
tempTable <- matrix(LETTERS[6:20], nrow = 5) colnames(tempTable) <- LETTERS[24:26] row.names(tempTable) <- LETTERS[1:5] myKable(tempTable) myKable(tempTable, boldColNames = FALSE)
Generate a list formatted for printing from a vector.
printList(toPrint = letters[1:3], finalSepWord = "and", midSep = ",")
printList(toPrint = letters[1:3], finalSepWord = "and", midSep = ",")
toPrint |
Vector that you want to turn into a text list. |
finalSepWord |
The last word to include, defaults to "and" but could be "or" or similar |
midSep |
Separator between items, defaults to "," |
Note that this function includes an Oxford comma.
Character vector of length 1 with the values of toPrint
concatenated
and separated as specified in the text.
Mark Peterson
printList() printList(LETTERS[1:5]) printList(letters[1:5], "or", ";")
printList() printList(LETTERS[1:5]) printList(letters[1:5], "or", ";")
Generates the html needed to include a popup reference note. Note requires inclusion of javascript libraries for this to function.
refNote(text = "This is a test note", number = "*")
refNote(text = "This is a test note", number = "*")
text |
Note to include in popup. Line breaks occasionally cause problems. As this is generally for short notes; I have not come up with a general solution yet. If your note text is complicated, ensure that you check the result. |
number |
The label to identify the note in the text. |
The html for the popup note
The html page also needs to include the javascript and css to process the notes for these to work. Specifically, need to include the following (generally in the header):
<!-- The javascript to run the popup refernce notes --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script> jQuery.noConflict(); jQuery(function() { jQuery(".refbody").hide(); jQuery(".refnum").click(function(event) { jQuery(this.nextSibling).toggle(); event.stopPropagation(); }); jQuery("body").click(function(event) { jQuery(".refbody").hide(); }); }); </script>
And the following to wherever you include you css definitions:
/* the reference tooltips style starts here */ /* This was stolen from the What If? css sytle sheet * at https://what-if.xkcd.com/css/style.css * in an effort to emulate that style */ .ref { position: relative; vertical-align: baseline; } .refnum { position: relative; left: 2px; bottom: 1ex; font-family: Verdana, sans-serif; color: #005994; font-size: .7em; font-weight: bold; text-decoration: underline; cursor: pointer; } .refbody { font-family: Verdana, sans-serif; font-size: .7em; line-height: 1.1; display: block; min-width: 20em; position: absolute; left: 25px; bottom: 5px ; border: 1px solid; padding: 5px; background-color: #fff; word-wrap: break-word; z-index: 9999; overflow: auto; }
Mark Peterson
This was based on the popups on the https://what-if.xkcd.com/ site
cat(refNote()) cat(refNote("Any text can go in here", 42))
cat(refNote()) cat(refNote("Any text can go in here", 42))
Identifies the file that knitr is processing when the function is called.
thisfile_knit()
thisfile_knit()
Returns the current file name
Mark Peterson
Based on https://github.com/krlmlr/kimisc/blob/master/R/thisfile.R
thisfile_knit()
thisfile_knit()
Identify the file currently being processed
thisFileName()
thisFileName()
Currently only works for files being processed with knit
;
may try to add more
The current file name
Based on https://github.com/krlmlr/kimisc/blob/master/R/thisfile.R
thisFileName()
thisFileName()