Developing shiny app as a package, How to document the function? - r

I am a beginner in shiny and I am trying to make the package of my shiny application and then I will put it on the docker for future purpose use.
For doing so, I made the launch_application function
#' #export
runExample <- function() {
appDir <- system.file("shiny-examples", "myapp", package = "mypackage")
if (appDir == "") {
stop("Could not find example directory. Try re-installing `mypackage`.",
call. = FALSE)
}
shiny::runApp(appDir, display.mode = "normal")
}
I am not able to document this function. :( Could anyone suggest a solution?

Related

R: Understand the ".call" function in R

I am using R. I am working with a library called "mco" : https://cran.r-project.org/web/packages/mco/index.html
I was looking over some of the function definitions used in this library at the github repository, for example: https://github.com/olafmersmann/mco/blob/master/R/nsga2.R
Over here, I came across the following lines of code:
res <- .Call(do_nsga2,
ff, cf, sys.frame(),
as.integer(odim),
as.integer(cdim),
as.integer(idim),
lower.bounds, upper.bounds,
as.integer(popsize), as.integer(generations),
cprob, as.integer(cdist),
mprob, as.integer(mdist))
if (1 == length(res)) {
res <- res[[1]]
names(res) <- c("par", "value", "pareto.optimal")
class(res) <- c("nsga2", "mco")
} else {
for (i in 1:length(res)) {
names(res[[i]]) <- c("par", "value", "pareto.optimal")
class(res[[i]]) <- c("nsga2", "mco")
}
class(res) <- "nsga2.collection"
}
return (res)
}
In the very first line of this code, it makes reference to some object called "do_nsga2". But apart from this function, I can't find any reference to "do_nsga2" within the entire package.
Does anyone know what exactly is being "called"?
Thanks
Note: I am trying to copy/paste all the functions from the github repository into my R session, since I am working with an older computer in which directly installing libraries from CRAN is not possible. When I tried to copy/paste all these functions, I got the following error:
Error in nsga2....
object 'do_nsga2' not found

Adding additional documentation to a package in R

Aside from a vignette, I wish to add an additional document as PDF to my package. I can, of course, copy it to the inst/doc directory and it will then be included in the package documentation.
However, I would like to make it easy for the user to display this file. The authors of the edgeR package decided to do the following: the main users manual is distributed as PDF (and is not a regular vignette), and the authors include a function called edgeRUsersGuide() which shows the PDF by means of the following code:
edgeRUsersGuide <- function (view = TRUE) {
f <- system.file("doc", "edgeRUsersGuide.pdf", package = "edgeR")
if (view) {
if (.Platform$OS.type == "windows")
shell.exec(f)
else system(paste(Sys.getenv("R_PDFVIEWER"), f, "&"))
}
return(f)
}
It appears to work. Do you think it is a reasonable approach?
Or should one use something else? Potentially, the following code would also work and be more robust:
z <- list(PDF="edgeR.pdf", Dir=system.file(package="edgeR"))
class(z) <- "vignette"
return(z)
My solution was to ape the code in utils:::print.vignette():
function(docfile) {
## code inspired by tools:::print.vignette
pdfviewer <- getOption("pdfviewer")
f <- system.file("doc", docfile, package = "tmod")
if(identical(pdfviewer, "false"))
stop(sprintf("Cannot display the file %s", f))
if (.Platform$OS.type == "windows" &&
identical(pdfviewer, file.path(R.home("bin"), "open.exe"))) {
shell.exec(f)
} else {
system2(pdfviewer, shQuote(f), wait = FALSE)
}
return(invisible(f))
}

Allowing user to change R package global variable

I want to make an R package called my_package with the following with the following behavior:
library(my_package)
my_package::get_username() # should throw error "no username set"
my_package::set_username("john doe")
my_package::get_username() # should print "john doe"
I'm not sure how to do this. I tried with the following inside an R file test.R and using source('test.R') it works. But I don't know what would be the proper way to do this when creating a package.
pkg_env <- new.env()
pkg_env$username <- NULL
set_username <- function(username) {
pkg_env$username <- username
}
get_username <- function() {
if (is.null(pkg_env$username)) {
print("ERROR")
} else {
print(pkg_env$username)
}
}
The easiest way of doing what you want is to use options. For example, in devtools, you can set a variety of options using this technique - package?devtools
If you want to use environments, then you need to create the environment when the package is loaded, using the .onLoad function

Load package at start-up

I am trying to load a package at start-up if it's already installed. If it isn't then I want to first install it and then load it. So, I created the following function:
RLoadPackage <- function(packname)
{
if((packname %in% rownames(installed.packages()))==FALSE)
{
install.packages(packname,dependencies = TRUE)
}
library(packname,character.only = TRUE)
}
This works well once RStudio is opened, but it doesn't quite work at start-up. I added this function to my local .RProfile file as:
RLoadPackage("ggplot2")
RLoadPackage <- function(packname)
{
if((packname %in% rownames(installed.packages()))==FALSE)
{
install.packages(packname,dependencies = TRUE)
}
library(packname,character.only = TRUE)
}
However, I get the error message as:
Error: could not find function "RLoadPackage"
One option is to install packages manually and then add a bunch of library("xyz")
However, the above option is very clunky. So, I created a function.
I've 2 questions:
1) Can someone please help me with it?
2) Is there more efficient way of doing this?
My post is inspired from the following two links:
1) Check for installed packages before running install.packages()
2) http://www.statmethods.net/interface/customizing.html
I'd appreciate any help.
Thanks
Ok. This piece of code works:
library("utils")
RLoadPackage <- function(packname)
{
if((packname %in% rownames(installed.packages()))==FALSE)
{
install.packages(packname,dependencies = TRUE)
}
library(packname,character.only = TRUE)
}
RLoadPackage("ggplot2")
RLoadPackage("dplyr")
RLoadPackage("lubridate")
However, is there more efficient way of loading multiple packages--maybe a vectorized version of this? I am just curious.

Patch base::library with wrapper in R

Inside an R package, I'm trying to patch the base::library() function in R to specifically set the position of the loaded packages in the search path. I haveve defined several environments (all named env:<something>) and want to make sure that libraries are placed below these environments in the search path.
# wrap around library function.
library = function(..., pos = NULL) {
print("NEW LIBRARY FUNCTION!")
if (is.null(pos)) {
pos <- grep("env:", search())
pos <- if (length(pos) == 0) 2 else max(pos) + 1
}
base::library(..., pos=pos)
}
When I assign this function in the console, everything runs fine:
> library(stats)
[1] "NEW LIBRARY FUNCTION!"
> eval(parse(text = "library(stats)"))
[1] "NEW LIBRARY FUNCTION!"
> eval(parse(text = "library(stats)"), envir = globalenv())
[1] "NEW LIBRARY FUNCTION!"
When I define the above wrapper function inside my package, build it and load it in a new R Session, the following executes as expected:
> library(mypackage)
> mypackage:::library(stats)
[1] "NEW LIBRARY FUNCTION!"
But, when using eval() with the envir argument inside a function in mypackage, my new definition of library() is not retrieved:
# Functions defined in mypackage
testlibrary1 = function(...) {
library(...)
}
testlibrary2 = function(code) {
eval(parse(text = code))
}
testlibrary3 = function(code) {
eval(parse(text = code), envir = globalenv())
}
In console, I get the following results:
> mypackage:::testlibrary1(stats)
[1] "NEW LIBRARY FUNCTION!"
> mypackage:::testlibrary2("library(stats)")
[1] "NEW LIBRARY FUNCTION!"
> mypackage:::testlibrary3("library(stats)")
>
The last function, testlibrary3(), did not use the new wrapper function.
I want all functions that call library() inside mypackage to use my wrapper function. Can somebody help me out?
I guess the problem is the following, but as your question did not include a fully reproducible example (i.e., by uploading the package somewhere) it is difficult to tell.
As long as your library function is not exported from your package via the NAMESPACE it is not visible. Consequently, the only available library function to eval is base::library().
Note that while your function resides in the namespace of the package the calling environment for mypackage:::testlibraryX() is still the global environment. There your library functions is not available. Try to export is and see if this helps.

Resources