I want to get the example codes of R functions to use in knitr. There might be an easy way but tried the following code using helpExtract function which can be obtained from here (written by @AnandaMahto). With my approach I have to look whether a function has Examples or not and have to include only those functions which have Examples.This is very inefficient and naive approach. Now I'm trying to include only those functions which have Examples. I tried the following code but it is not working as desired. How can I to extract Examples codes from an R package?\documentclass{book}\usepackage[T1]{fontenc}\begin{document}<< label=packages, echo=FALSE>>=library(ggplot2)library(devtools)source_gist("https://gist.github.com/mrdwab/7586769")library(noamtools) # install_github("noamtools", "noamross")@\chapter{Linear Model}<< label = NewTest1, results="asis">>=tryCatch( {helpExtract(lm, section="Examples", type = "s_text"); cat( "\\Sexpr{ knit_child( textConnection(helpExtract(lm, section=\"Examples\", type = \"s_text\")) , options = list(tidy = FALSE, eval = TRUE) ) }", "\n" ) } , error=function(e) FALSE )@\chapter{Modify properties of an element in a theme object}<< label = NewTest2, results="asis">>=tryCatch( {helpExtract(add_theme , section="Examples", type = "s_text"); cat( "\\Sexpr{ knit_child( textConnection(helpExtract(add_theme , section=\"Examples\", type = \"s_text\")) , options = list(tidy = FALSE, eval = TRUE) ) }", "\n" ) } , error=function(e) FALSE )@\end{document} 解决方案 I've done some quick work modifying the function (which I've included at this Gist). The Gist also includes a sample Rnw file (I haven't had a chance to check an Rmd file yet).The function now looks like this:helpExtract <- function(Function, section = "Usage", type = "m_code", sectionHead = NULL) { A <- deparse(substitute(Function)) x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(utils::help(A)), options = list(sectionIndent = 0))) B <- grep("^_", x) ## section start lines x <- gsub("_\b", "", x, fixed = TRUE) ## remove "_\b" X <- rep(FALSE, length(x)) ## Create a FALSE vector X[B] <- 1 ## Initialize out <- split(x, cumsum(X)) ## Create a list of sections sectionID <- vapply(out, function(x) ## Identify where the section starts grepl(section, x[1], fixed = TRUE), logical(1L)) if (!any(sectionID)) { ## If the section is missing... "" ## ... just return an empty character } else { ## Else, get that list item out <- out[[which(sectionID)]][-c(1, 2)] while(TRUE) { ## Remove the extra empty lines out <- out[-length(out)] ## from the end of the file if (out[length(out)] != "") { break } } switch( ## Determine the output type type, m_code = { before <- "```r" after <- "```" c(sectionHead, before, out, after) }, s_code = { before <- "<<eval = FALSE>>=" after <- "@" c(sectionHead, before, out, after) }, m_text = { c(sectionHead, paste(" ", out, collapse = "\n")) }, s_text = { before <- "\\begin{verbatim}" after <- "\\end{verbatim}" c(sectionHead, before, out, after) }, stop("`type` must be either `m_code`, `s_code`, `m_text`, or `s_text`") ) }}What has changed?A new argument sectionHead has been added. This is used to be able to specify the section title in the call to the helpExtract function.The function checks to see whether the relevant section is available in the parsed document. If it is not, it simply returns a "" (which doesn't get printed).Example use would be:<<echo = FALSE>>=mySectionHeading <- "\\section{Some cool section title}"@\Sexpr{knit_child(textConnection(helpExtract(cor, section = "Examples", type = "s_code",sectionHead = mySectionHeading)),options = list(tidy = FALSE, eval = FALSE))}Note: Since Sexpr doesn't allow curly brackets to be used ({), we need to specify the title outside of the Sexpr step, which I have done in a hidden code chunk. 这篇关于使用helpExtract函数将R函数的示例代码获取到knitr中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 10:51
查看更多