Learn R Programming

Hmisc (version 5.1-3)

knitrSet: knitr Setup and plotly Service Function

Description

knitrSet sets up knitr to use better default parameters for base graphics, better code formatting, and to allow several arguments to be passed from code chunk headers, such as bty, mfrow, ps, bot (extra bottom margin for base graphics), top (extra top margin), left (extra left margin), rt (extra right margin), lwd, mgp, las, tcl, axes, xpd, h (usually fig.height in knitr), w (usually fig.width in knitr), wo (out.width in knitr), ho (out.height in knitr), cap (character string containing figure caption), scap (character string containing short figure caption for table of figures). The capfile argument facilities auto-generating a table of figures for certain Rmarkdown report themes. This is done by the addition of a hook function that appends data to the capfile file each time a chunk runs that has a long or short caption in the chunk header.

plotlySave saves a plotly graphic with name foo.png where foo is the name of the current chunk. You must have a free plotly account from plot.ly to use this function, and you must have run Sys.setenv(plotly_username="your_plotly_username") and Sys.setenv(plotly_api_key="your_api_key"). The API key can be found in one's profile settings.

Usage

knitrSet(basename=NULL, w=if(! bd) 4, h=if(! bd) 3, wo=NULL, ho=NULL,
         fig.path=if(length(basename)) basename else '',
         fig.align=if(! bd) 'center', fig.show='hold',
         fig.pos=if(! bd) 'htbp',
         fig.lp    = if(! bd) paste('fig', basename, sep=':'),
         dev=switch(lang, latex='pdf', markdown='png',
                    blogdown=NULL, quarto=NULL),
         tidy=FALSE, error=FALSE,
         messages=c('messages.txt', 'console'),
         width=61, decinline=5, size=NULL, cache=FALSE,
         echo=TRUE, results='markup', capfile=NULL,
         lang=c('latex','markdown','blogdown','quarto'))

plotlySave(x, ...)

Arguments

basename

base name to be added in front of graphics file names. basename is followed by a minus sign.

w,h

default figure width and height in inches

wo,ho

default figure rendering width and height, in integer pixels or percent as a character string, e.g. '40%'

fig.path

path for figures. To put figures in a subdirectory specify e.g. fig.path='folder/'. Ignored for blogdown.

fig.align,fig.show,fig.pos,fig.lp,tidy,cache,echo,results,error,size

see knitr documentation

dev

graphics device, with default figured from lang

messages

By default warning and other messages such as those from loading packages are sent to file 'messages.txt' in the current working directory. You can specify messages='console' to send them directly to the console.

width

text output width for R code and output

decinline

number of digits to the right of the decimal point to round numeric values appearing inside Sexpr

capfile

the name of a file in the current working directory that is used to accumulate chunk labels, figure cross-reference tags, and figure short captions (long captions if no short caption is defined) for the purpose of using markupSpecs$markdown$tof() to insert a table of figures in a report. The file as appended to, which is useful if cache=TRUE is used since this will keep some chunks from running. The tof function will remove earlier duplicated figure tags if this is the case. If not cacheing, the user should initialize the file to empty at the top of the script.

lang

Default is 'latex' to use LaTeX. Set to 'markdown' when using R Markdown or 'blogdown' or 'quarto'. For 'blogdown' and 'quarto', par and knitr graphics-related hooks are not called as this would prevent writing graphics files in the correct directory for the blog system.

x

a plotly graphics object or a named list of such objects. The resulting png file will go in the file path given by the knitr fig.path value, and have a base name equal to the current knitr chunk name. If x is a list, a minus sign followed by the chunk name are inserted before .png.

...

additional arguments passed to plotly::plotly_IMAGE

Author

Frank Harrell

See Also

Examples

Run this code
if (FALSE) {
# Typical call (without # comment symbols):
# <>=
# require(Hmisc)
# knitrSet()
# @

knitrSet()    # use all defaults and don't use a graphics file prefix
knitrSet('modeling')   # use modeling- prefix for a major section or chapter
knitrSet(cache=TRUE, echo=FALSE)  # global default to cache and not print code
knitrSet(w=5,h=3.75)   # override default figure width, height

# ```{r chunkname}
# p <- plotly::plot_ly(...)
# plotlySave(p)   # creates fig.path/chunkname.png
}

Run the code above in your browser using DataLab