forked from Novartis/tidymodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.R
executable file
·61 lines (59 loc) · 1.55 KB
/
examples.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#' @title Launcher for the tidymodules examples
#'
#' @description Helper function to launch the tidymodules examples.
#'
#' @param id Example ID. If null display list of examples with ID.
#' @param server boolean. Is this a server call?
#' @param options list of options to be passed to shinyApps or shinyDir
#'
#' @export
#'
#' @examples
#'
#' if (interactive()) {
#'
#' showExamples(1)
#'
#' }
showExamples <- function(id = NULL, server = F, options = NULL) {
examples <- list.dirs(system.file(package = "tidymodules","shiny/examples"),recursive = F)
if(is.null(id)){
names(examples) <- 1:length(examples)
basename(examples)
}else{
if(!is.numeric(id)){
stop("Please provide a numeric value")
}
if(id > length(examples) || id < 1){
stop("Wrong ID provided")
}
if(server){
setwd(examples[id])
if(!is.null(options))
shiny::shinyAppDir(examples[id], options = options)
else
shiny::shinyAppDir(examples[id])
}else{
shiny::runApp(examples[id])
}
}
}
#' @title check if package namespace exist, load it or display relevant information
#'
#' @description Utility function for managing package dependencies for tidymodules examples
#'
#' @param p character verctor of package names
#'
#' @export
#'
#' @examples
#'
#' check_and_load("ggplot2")
#'
check_and_load <- function(p) {
if (!requireNamespace(p, quietly = TRUE)) {
stop(paste0("Package ",p," needed for this shiny example to work. Please install it."),
call. = FALSE)
}
library(p,character.only = TRUE)
}