Package YieldCurve - r

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 ...

Related

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.
Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:
invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")
The conflicted package chose the functions select, lag, and filter comes from the dplyr package rather from the stats package.
So I have not figured out how to use the conflict_prefer function inside the package.
Should they be the first lines of my function?
There is a roxygen way to prefer same-name functions?
I ask this because I get this warning:
> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’
4: In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: favar_est
Thanks in advance!!
If you are writing your own package and using external dependencies, you should not load them through repeated calls to library.
The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer, as dplyr will be higher up on the search path than stats. It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.
The DESCRIPTION file resides in the root directory of your package. It is a simple text file.
You need only add:
Depends:
tibble,
readxl,
dplyr,
stringr,
tidyr,
vars,
conflicted,
forecast,
lubridate
within this file, and your dependencies will be loaded with your package.

R: develop R Package with bioconductor dependency (earlier solutions fail)

I'm developing an R package to put on cran and for one of the help pages/in the example section, there is need to load a Bioconductor data set.
As found here, the trick for automated installation of Bioconductor packages was to add biocViews: to the DESCRIPTION file, which does not work anymore.
Instead,
\examples{
\dontrun{
if (requireNamespace("GEOquery","Biobase")) {
myGEO <- GEOquery::getGEO("GDS3143")
eset <- GEOquery::GDS2eSet(myGEO)
xpr <- na.omit(data.frame(Biobase::exprs(eset)))
dose <- c(rep(0,10),rep(0.5,4),rep(1,8),rep(5,4),rep(10,9),rep(30,5),rep(50,4))
plot(dose, xpr[78,], col=as.factor(dose), lwd=2, ylab ="expression")
}
}}
"works", but is obviously not the appropriate way to proceed. (the :: is used to prevent the Bioconductor packages masking existing R functions)
Is there another way to automatically install Bioconductor packages upon (my) package installation? (some modification of DESCRIPTION file & NAMESPACE?)

R: Check which branch of a package was installed with install_git

Say I use the function install_git from library devtools to install a particular branch of a package e.g. from the install_git documentation:
install_git("git://github.com/hadley/stringr.git", branch = "stringr-0.2")
Is there a way later on to find out if a branch was installed and if so which one? I can use packageVersion() to find the version of the package installed, but this does not give me any additional information about branches referenced.
I have found the answer - the information is contained in the output of the function packageDescription('package_name') e.g.
packageDescription('mlr')
The output is fairly verbose, but the following items within that output hold the answer:
RemoteRepo: mlr
RemoteRef: mindepth_order
GithubRepo: mlr
GithubRef: mindepth_order
This shows that package mlr is loaded, referencing the branch mindepth_order.

mmetric could not find function R

I have library(rminer) installed yet wondering why mmertric is still not there and unable to use the function.
Anyone has come across this?
#probability of each landmass category
flagdmodel <- naiveBayes(landmass ~ ., data=trainfdata)
#flagdmodel
#predictionmodel
flagprediction <- predict(flagdmodel, testfdata)
mmetric(testfdata$landmass, flagprediction, c("ACC","PRECISION","TPR","F1"))
+ mmetric(testfdata$landmass, flagprediction, c("ACC","PRECISION","TPR","F1"))
mmetric()
Error in mmetric() : could not find function "mmetric"
mmetric()
Error in mmetric() : could not find function "mmetric"
Question: why can't R find functions for the package I installed with RStudio's install tool?
Answer:
When you want to use functions or other objects in packages that aren't in R, you need to do two things:
install.packages("rminer")
library(rminer)
RStudio can do the first step for you with the install tool, but you still need to do the second one. The first step installs the needed directories and files on your computer. The second step loads them into your current R environment.
In RStudio, you can use the packages tab to check both steps. Installed packages will be in the list in that tab. Loaded packages will have a checkmark to the left of the package name.
It may be easier to find, though if you just run the following in your console:
"package:rminer" %in% search()
If the output is TRUE you're good to go. If it's FALSE you need to run library(rminer)

Making an R package PDF manual using devtools

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.

Resources