Skip to content

Commit

Permalink
improve bake/stew directory creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kingaa committed Apr 13, 2023
1 parent 427c6c4 commit 05515cc
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: pomp
Type: Package
Title: Statistical Inference for Partially Observed Markov Processes
Version: 5.0.1.1
Date: 2023-04-11
Version: 5.0.1.2
Date: 2023-04-13
Authors@R: c(person(given=c("Aaron","A."),family="King",
role=c("aut","cre"),email="[email protected]",
comment=c(ORCID="0000-0001-6159-3207")),
Expand Down
22 changes: 13 additions & 9 deletions R/bake.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
##' @param dir Directory holding archive files;
##' by default, this is the current working directory.
##' This can also be set using the global option \code{pomp_archive_dir}.
##' This directory will be created if it does not exist.
##' If it does not exist, this directory will be created (with a message).
##' @param expr Expression to be evaluated.
##' @param seed,kind,normal.kind optional.
##' To set the state and of the RNG.
Expand Down Expand Up @@ -150,6 +150,16 @@ update_bake_archive <- function (val, code, deps, file) {
val
}

create_path <- function (dir, file, mode = "0755") {
path <- file.path(as.character(dir[[1L]]),as.character(file[1L]))
dir <- dirname(path)
if (!dir.exists(dir)) {
pMess_("creating archive directory ",sQuote(dir),".")
dir.create(dir,recursive=TRUE,mode=mode)
}
path
}

##' @rdname bake
##' @export
bake <- function (
Expand All @@ -167,10 +177,7 @@ bake <- function (
)
info <- as.logical(info)
timing <- as.logical(timing)
if (!dir.exists(dir)) {
dir.create(dir,recursive=TRUE,mode="0644") #nocov
}
file <- file.path(as.character(dir[[1L]]),as.character(file))
file <- create_path(dir,file)
reload <- file.exists(file)
if (reload) {
val <- readRDS(file)
Expand Down Expand Up @@ -256,10 +263,7 @@ stew <- function (
ep="stew"
)
info <- as.logical(info)
if (!dir.exists(dir)) {
dir.create(dir,recursive=TRUE,mode="0644") #nocov
}
file <- file.path(as.character(dir[[1L]]),as.character(file))
file <- create_path(dir,file)
reload <- file.exists(file)
e <- new.env()
if (reload) {
Expand Down
5 changes: 5 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
_N_e_w_s _f_o_r _p_a_c_k_a_g_e '_p_o_m_p'

_C_h_a_n_g_e_s _i_n '_p_o_m_p' _v_e_r_s_i_o_n _5._0._1:

• When the archive directory does not exist, ‘bake’ and ‘stew’
now create it.

_C_h_a_n_g_e_s _i_n '_p_o_m_p' _v_e_r_s_i_o_n _5._0._0:

• We no longer import from ‘plyr’.
Expand Down
5 changes: 5 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
\name{NEWS}
\title{News for package `pomp'}
\section{Changes in \pkg{pomp} version 5.0.1}{
\itemize{
\item When the archive directory does not exist, \code{bake} and \code{stew} now create it.
}
}
\section{Changes in \pkg{pomp} version 5.0.0}{
\itemize{
\item We no longer import from \pkg{plyr}.
Expand Down
2 changes: 1 addition & 1 deletion man/bake.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/bake.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,28 @@ stopifnot(
identical(x,xx),
identical(y,yy)
)

capture.output(
{
bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x1
bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x2
bake(file="mary/tmp.rds",rnorm(5),dir=file.path(tempdir(),"results/bob")) -> x3
op <- options(pomp_archive_dir=file.path(tempdir(),"results"))
bake(file="bob/mary/tmp.rds",rnorm(5)) -> x4
bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x5
options(op)
bake(file="results/results/bob/mary/tmp.rds",rnorm(5)) -> x6
},
type="message"
) -> out
stopifnot(
identical(x1,x2),
identical(x1,x3),
identical(x1,x4),
!identical(x1,x5),
identical(x5,x6),
length(out)==2,
grepl("^NOTE: creating archive directory",out),
grepl("results/bob/mary'.",out),
grepl("results/results/bob/mary'.",out[2])
)
29 changes: 27 additions & 2 deletions tests/bake.Rout.save
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
R version 4.2.3 (2023-03-15) -- "Shortstop Beagle"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
Expand Down Expand Up @@ -177,3 +177,28 @@ NOTE: in 'stew': recomputing archive tmp2.rda.
+ identical(y,yy)
+ )
>
> capture.output(
+ {
+ bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x1
+ bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x2
+ bake(file="mary/tmp.rds",rnorm(5),dir=file.path(tempdir(),"results/bob")) -> x3
+ op <- options(pomp_archive_dir=file.path(tempdir(),"results"))
+ bake(file="bob/mary/tmp.rds",rnorm(5)) -> x4
+ bake(file="results/bob/mary/tmp.rds",rnorm(5)) -> x5
+ options(op)
+ bake(file="results/results/bob/mary/tmp.rds",rnorm(5)) -> x6
+ },
+ type="message"
+ ) -> out
> stopifnot(
+ identical(x1,x2),
+ identical(x1,x3),
+ identical(x1,x4),
+ !identical(x1,x5),
+ identical(x5,x6),
+ length(out)==2,
+ grepl("^NOTE: creating archive directory",out),
+ grepl("results/bob/mary'.",out),
+ grepl("results/results/bob/mary'.",out[2])
+ )
>

0 comments on commit 05515cc

Please sign in to comment.