Skip to content

Commit

Permalink
make it possible to set options without persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Oct 31, 2015
1 parent 430d626 commit ef417bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^scenarios/
^configure.R$
^NEWS.md$
^\.Rprofile$
35 changes: 24 additions & 11 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,45 @@ get_opts <- function(options = NULL, simplify = TRUE, project = NULL) {

make_setter <- function(name) {
force(name)
function(x) {
function(x, persist = TRUE) {
if (missing(x)) return(get_opts(name))
else do.call(set_opts, setNames(list(x), name))
else setOptions(setNames(list(x), name), persist = persist)
}
}

##' @rdname packrat-options
##' @name packrat-options
##' @export
set_opts <- function(..., project = NULL) {
setOptions(list(...), project = project)
}

setOptions <- function(options, project = NULL, persist = TRUE) {

project <- getProjectDir(project)
optsPath <- packratOptionsFilePath(project)

if (!file.exists(optsPath)) {
if (persist && !file.exists(optsPath)) {
dir.create(dirname(optsPath), recursive = TRUE, showWarnings = FALSE)
file.create(optsPath)
}
dots <- list(...)
validateOptions(dots)
keys <- names(dots)
values <- dots

validateOptions(options)
keys <- names(options)
values <- options
opts <- read_opts(project = project)
for (i in seq_along(keys)) {
if (is.null(values[[i]]))
opts[keys[[i]]] <- list(NULL)
else
opts[[keys[[i]]]] <- values[[i]]
}
write_opts(opts, project = project)
updateSettings(project)

write_opts(opts, project = project, persist = persist)

if (persist)
updateSettings(project)

invisible(opts)
}

Expand Down Expand Up @@ -227,7 +235,7 @@ readOptsFile <- function(path) {
read_opts <- function(project = NULL) {
project <- getProjectDir(project)
path <- packratOptionsFilePath(project)
if (!file.exists(path)) return(invisible(NULL))
if (!file.exists(path)) return(list())
opts <- readOptsFile(path)
if (!length(opts)) return(list())
opts[] <- lapply(opts, function(x) {
Expand All @@ -244,7 +252,8 @@ read_opts <- function(project = NULL) {
opts
}

write_opts <- function(options, project = NULL) {
write_opts <- function(options, project = NULL, persist = TRUE) {

project <- getProjectDir(project)
if (!is.list(options))
stop("Expecting options as an R list of values")
Expand Down Expand Up @@ -274,6 +283,10 @@ write_opts <- function(options, project = NULL) {
# Update the in-memory options cache
assign("options", options, envir = .packrat)

# Write options to disk
if (!persist)
return(invisible(TRUE))

sep <- ifelse(
unlist(lapply(options, length)) > 1,
":\n",
Expand Down

0 comments on commit ef417bb

Please sign in to comment.