Making an R package PDF manual using devtools - r

I am making an R package using devtools and roxygen2. I can get a PDF manual using R CMD but I am really curious as to whether this can be done using devtools. devtools' build(), check(), install() all don't make a PDF manual. Is this tied to making vignettes?
I have read and referred to a similar thread Package development : location of pdf manual and vignette

After you install it, you can use:
pack <- "name_of_your_package"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),
"CMD", "Rd2pdf", shQuote(path)))

There is
devtools::build_manual()
Maybe also
devtools::check(manual=TRUE)
could work.

For the PDF manual of one specific function, you can run
fun <- "name_of_function"
help(fun, package = "name_of_package", help_type = "pdf")
system(paste0("open ", fun, ".pdf"))
assuming you have the package installed.

Related

Package YieldCurve

My question is about package YieldCurve of R.
The R YieldCurve package used for modeling yield curves with parametric models like Nelson Siegel and Svensson has been removed from rcran, so it is not possible to download it in RStudio.
Is there a way to load it from another repository?
Is there a package that replaces it and allows to do the same thing that YieldCurve did?
Especially if packages are recently archived, they can usually be installed from source without a problem.
I've been meaning to write this helper function for a little while:
go to CRAN archive page for the package
find the most recent/last version of the package (I'm not sure this will work perfectly if the package versions are such that they are sorted inconsistently in alphabetical order ...)
construct the location of the 'tarball' (compressed source archive)
install
If the package has compiled components you'll need to have development tools installed.
This is very much like remotes::install_version() but (1) it finds the latest archived version automatically (2) you don't need to install the remotes package.
install_last_archived <- function(pkg, verbose = TRUE) {
arch_url <- "https://cran.r-project.org/src/contrib/Archive/"
rr <- readLines(paste0(arch_url, pkg))
last <- tail(rr[grepl(pkg,rr)],1)
tarball <- gsub(sprintf(".*(%s_[0-9.]*\\.tar\\.gz).*", pkg), "\\1", last)
if (verbose) cat("installing ", tarball, "\n")
install.packages(paste0(arch_url, pkg, "/", tarball), repos = NULL)
}
install_last_archived("YieldCurve")
Possible enhancements: (1) work harder to make sure we have the most recent version (check dates explicitly?) (2) extract the DESCRIPTION file to see if the package has compiled components, if so then warn user ...

invisible functions description in own package

I've just created my first package and I have some problems with description. I was trying to use vignette with code usethis::use_vignette("introduction") and it creates introduction to package and to included functions. To see it I pressed "knit" button and it works, look nice etc.. I also create new R script and use library('mypackage') and all function work, but I have no idea how can I see my vignette (description of package and functions). For example in packages like ggplot2 or graphics you can just put ??ggplot2, ??graphics to just see description of package. But putting ??mypackage I see in R help 'No results found'. How can I see this created vignette not in model building tools, but in new script which refers to my package.
RStudio's build and reload does not build vignettes by default. You'll need to use devtools::build() to ensure vignettes are built. If you are using Github to install the package use devtools::install_github("package", build_vignettes = TRUE)

Faster way of generating R package documentation (i.e. pdf manual) in roxygen2?

For a package made with roxygen2, documentaiton can be generated with
R CMD check package_name
Is there a faster way to generate the package manual (pdf)?
Note: it must work for roxygen2-made packages. I tried (this method, but it doesn't appear to work for a roxygen2-made package)

How can I install packages in knitr?

Till now, I was using this chunk of code to load R packages and write .R files. But I am trying to use knitr
rm (list=ls(all=TRUE))
kpacks <- c('ggplot2','install_github','devtools','mapdata')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
options(max.print=5.5E5)
But now, when I put this chunk of code in a Knitr document, I get this error:
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror calls:......
How can I fix this?
The narrow answer to your question is that you should set your repos option:
options(repos=c(CRAN="<something sensible near you>"))
You're hitting the problem because R's default behaviour when the repository option is initially unset is to query the user -- and it can't do that when you're running code non-interactively.
More broadly, I would question whether you want to include this sort of thing in your R code; under some circumstances it can be problematic.
what if the user doesn't have a network connection?
what if they are geographically very far from you so that your default repository setting doesn't make sense?
what if they don't feel like downloading and installing a (possibly large) package?
My preferred practice is to specify in the instructions for running the code that users should have packages X, Y, Z installed (and giving them example code to install them, in case they're inexperienced with R).
One way to avoid installing the packages is to do something like
if(!require(package.name))
stop("you need to install package.name")
In your code chunk. Depending on your knitr document settings, this will generate the message in the document, in the console, or prevent the document from being knitted.

R Package tm.plugin.webmining does not work with R 3.1.0 (but does with R 2.1.5)

I am using R 3.1.0, along with the tm.plugin.webmining package (within RStudio). Packages are installed fine (along with all dependencies) and I can load the library.
When I try to run a basic test however:
yahoocorpus<-WebCorpus(YahooNewsSource("Microsoft"))
I get:
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object '.Source' not found
This does not occur if I point RStudio to R 2.1.5
I have looked online but not found any resolution of the issue (though somebody did suggest a hack of the source code). It would be great to understand exactly what is causing the problem (and what has changed between versions to make this happen (I also tried 3.0.1, and that also does not work)
Thanks again,
Alan
I think that function WebCorpus does not exist in last version of TM package. Check the doc here!
You have 3 possibilities : DirSource, VectorSource, or DataframeSource.
You can check the source on R with:
{r}
library(tm)
getSources()
Which should give you :
[1] "DataframeSource" "DirSource" "URISource" "VectorSource" "XMLSource"
Cyrille

Resources