Skip to content

Commit

Permalink
add example app
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Guggenbühl committed Mar 12, 2020
1 parent 5a6800c commit 76b7530
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dynamic-shinyUI-pt1/code/dynamic_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ ui <- dashboardPage(
p(mainPanel(width = 12,
column(width = 6, uiOutput("reference")),
column(width = 6, uiOutput("comparison"))
)
)
)
)
)
)
)
Expand Down
1 change: 1 addition & 0 deletions shinyapp-in-docker/example-app/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
13 changes: 13 additions & 0 deletions shinyapp-in-docker/example-app/example-app.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
4 changes: 4 additions & 0 deletions shinyapp-in-docker/example-app/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(renv)
library(shiny)
library(shinydashboard)
library(glue)
153 changes: 153 additions & 0 deletions shinyapp-in-docker/example-app/renv.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"R": {
"Version": "3.6.1",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cran.rstudio.com"
}
]
},
"Packages": {
"BH": {
"Package": "BH",
"Version": "1.72.0-3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "8f9ce74c6417d61f0782cbae5fd2b7b0"
},
"R6": {
"Package": "R6",
"Version": "2.4.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "292b54f8f4b94669b08f94e5acce6be2"
},
"Rcpp": {
"Package": "Rcpp",
"Version": "1.0.3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "f3ca785924863b0e4c8cb23b6a5c75a1"
},
"crayon": {
"Package": "crayon",
"Version": "1.3.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "0d57bc8e27b7ba9e45dba825ebc0de6b"
},
"digest": {
"Package": "digest",
"Version": "0.6.25",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "f697db7d92b7028c4b3436e9603fb636"
},
"fastmap": {
"Package": "fastmap",
"Version": "1.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "83ab58a0518afe3d17e41da01af13b60"
},
"glue": {
"Package": "glue",
"Version": "1.3.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "d4e25697c450c01b202c79ef35694a83"
},
"htmltools": {
"Package": "htmltools",
"Version": "0.4.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "2d7691222f82f41e93f6d30f169bd5e1"
},
"httpuv": {
"Package": "httpuv",
"Version": "1.5.2",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "f793dad2c9ae14fbb1d22f16f23f8326"
},
"jsonlite": {
"Package": "jsonlite",
"Version": "1.6.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "84b0ee361e2f78d6b7d670db9471c0c5"
},
"later": {
"Package": "later",
"Version": "1.0.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "6d927978fc658d24175ce37db635f9e5"
},
"magrittr": {
"Package": "magrittr",
"Version": "1.5",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "1bb58822a20301cee84a41678e25d9b7"
},
"mime": {
"Package": "mime",
"Version": "0.9",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "e87a35ec73b157552814869f45a63aa3"
},
"promises": {
"Package": "promises",
"Version": "1.1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "efbbe62da4709f7040a380c702bc7103"
},
"renv": {
"Package": "renv",
"Version": "0.9.3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "c1a367437d8a8a44bec4b9d4974cb20c"
},
"rlang": {
"Package": "rlang",
"Version": "0.4.5",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "1cc1b38e4db40ea6eb19ab8080bbed3b"
},
"shiny": {
"Package": "shiny",
"Version": "1.4.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "0db4f702de0f8e3b330f98d254e9ce88"
},
"shinydashboard": {
"Package": "shinydashboard",
"Version": "0.7.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "133639dc106955eee4ffb8ec73edac37"
},
"sourcetools": {
"Package": "sourcetools",
"Version": "0.1.7",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "947e4e02a79effa5d512473e10f41797"
},
"xtable": {
"Package": "xtable",
"Version": "1.8-4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "b8acdf8af494d9ec19ccb2481a9b11c2"
}
}
}
3 changes: 3 additions & 0 deletions shinyapp-in-docker/example-app/renv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library/
python/
staging/
185 changes: 185 additions & 0 deletions shinyapp-in-docker/example-app/renv/activate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@

local({

# the requested version of renv
version <- "0.9.3"

# avoid recursion
if (!is.na(Sys.getenv("RENV_R_INITIALIZING", unset = NA)))
return(invisible(TRUE))

# signal that we're loading renv during R startup
Sys.setenv("RENV_R_INITIALIZING" = "true")
on.exit(Sys.unsetenv("RENV_R_INITIALIZING"), add = TRUE)

# signal that we've consented to use renv
options(renv.consent = TRUE)

# load the 'utils' package eagerly -- this ensures that renv shims, which
# mask 'utils' packages, will come first on the search path
library(utils, lib.loc = .Library)

# check to see if renv has already been loaded
if ("renv" %in% loadedNamespaces()) {

# if renv has already been loaded, and it's the requested version of renv,
# nothing to do
spec <- .getNamespaceInfo(.getNamespace("renv"), "spec")
if (identical(spec[["version"]], version))
return(invisible(TRUE))

# otherwise, unload and attempt to load the correct version of renv
unloadNamespace("renv")

}

# construct path to renv in library
libpath <- local({

root <- Sys.getenv("RENV_PATHS_LIBRARY", unset = "renv/library")
prefix <- paste("R", getRversion()[1, 1:2], sep = "-")

# include SVN revision for development versions of R
# (to avoid sharing platform-specific artefacts with released versions of R)
devel <-
identical(R.version[["status"]], "Under development (unstable)") ||
identical(R.version[["nickname"]], "Unsuffered Consequences")

if (devel)
prefix <- paste(prefix, R.version[["svn rev"]], sep = "-r")

file.path(root, prefix, R.version$platform)

})

# try to load renv from the project library
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {

# warn if the version of renv loaded does not match
loadedversion <- utils::packageDescription("renv", fields = "Version")
if (version != loadedversion) {

# assume four-component versions are from GitHub; three-component
# versions are from CRAN
components <- strsplit(loadedversion, "[.-]")[[1]]
remote <- if (length(components) == 4L)
paste("rstudio/renv", loadedversion, sep = "@")
else
paste("renv", loadedversion, sep = "@")

fmt <- paste(
"renv %1$s was loaded from project library, but renv %2$s is recorded in lockfile.",
"Use `renv::record(\"%3$s\")` to record this version in the lockfile.",
"Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.",
sep = "\n"
)

msg <- sprintf(fmt, loadedversion, version, remote)
warning(msg, call. = FALSE)

}

# load the project
return(renv::load())

}

# failed to find renv locally; we'll try to install from GitHub.
# first, set up download options as appropriate (try to use GITHUB_PAT)
install_renv <- function() {

message("Failed to find installation of renv -- attempting to bootstrap...")

# ensure .Rprofile doesn't get executed
rpu <- Sys.getenv("R_PROFILE_USER", unset = NA)
Sys.setenv(R_PROFILE_USER = "<NA>")
on.exit({
if (is.na(rpu))
Sys.unsetenv("R_PROFILE_USER")
else
Sys.setenv(R_PROFILE_USER = rpu)
}, add = TRUE)

# prepare download options
pat <- Sys.getenv("GITHUB_PAT")
if (nzchar(Sys.which("curl")) && nzchar(pat)) {
fmt <- "--location --fail --header \"Authorization: token %s\""
extra <- sprintf(fmt, pat)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "curl", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
fmt <- "--header=\"Authorization: token %s\""
extra <- sprintf(fmt, pat)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "wget", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
}

# fix up repos
repos <- getOption("repos")
on.exit(options(repos = repos), add = TRUE)
repos[repos == "@CRAN@"] <- "https://cloud.r-project.org"
options(repos = repos)

# check for renv on CRAN matching this version
db <- as.data.frame(available.packages(), stringsAsFactors = FALSE)
if ("renv" %in% rownames(db)) {
entry <- db["renv", ]
if (identical(entry$Version, version)) {
message("* Installing renv ", version, " ... ", appendLF = FALSE)
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)
utils::install.packages("renv", lib = libpath, quiet = TRUE)
message("Done!")
return(TRUE)
}
}

# try to download renv
message("* Downloading renv ", version, " ... ", appendLF = FALSE)
prefix <- "https://api.github.com"
url <- file.path(prefix, "repos/rstudio/renv/tarball", version)
destfile <- tempfile("renv-", fileext = ".tar.gz")
on.exit(unlink(destfile), add = TRUE)
utils::download.file(url, destfile = destfile, mode = "wb", quiet = TRUE)
message("Done!")

# attempt to install it into project library
message("* Installing renv ", version, " ... ", appendLF = FALSE)
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)

# invoke using system2 so we can capture and report output
bin <- R.home("bin")
exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
r <- file.path(bin, exe)
args <- c("--vanilla", "CMD", "INSTALL", "-l", shQuote(libpath), shQuote(destfile))
output <- system2(r, args, stdout = TRUE, stderr = TRUE)
message("Done!")

# check for successful install
status <- attr(output, "status")
if (is.numeric(status) && !identical(status, 0L)) {
text <- c("Error installing renv", "=====================", output)
writeLines(text, con = stderr())
}


}

try(install_renv())

# try again to load
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
message("Successfully installed and loaded renv ", version, ".")
return(renv::load())
}

# failed to download or load renv; warn the user
msg <- c(
"Failed to find an renv installation: the project will not be loaded.",
"Use `renv::activate()` to re-initialize the project."
)

warning(paste(msg, collapse = "\n"), call. = FALSE)

})
6 changes: 6 additions & 0 deletions shinyapp-in-docker/example-app/renv/settings.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
external.libraries:
ignored.packages:
package.dependency.fields: Imports, Depends, LinkingTo
snapshot.type: packrat
use.cache: TRUE
vcs.ignore.library: TRUE
Loading

0 comments on commit 76b7530

Please sign in to comment.