Skip to content

Commit f777c46

Browse files
committed
new append_data function
1 parent eeb4b5c commit f777c46

File tree

8 files changed

+163
-4
lines changed

8 files changed

+163
-4
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: pomp
22
Type: Package
33
Title: Statistical Inference for Partially Observed Markov Processes
4-
Version: 6.0.3.1
5-
Date: 2024-12-28
4+
Version: 6.0.4.0
5+
Date: 2024-12-29
66
Authors@R: c(person(given=c("Aaron","A."),family="King",role=c("aut","cre"),email="kingaa@umich.edu",comment=c(ORCID="0000-0001-6159-3207")),
77
person(given=c("Edward","L."),family="Ionides",role="aut",comment=c(ORCID="0000-0002-4190-0174")) ,
88
person(given="Carles",family="Bretó",role="aut",comment=c(ORCID="0000-0003-4695-4902")),

NAMESPACE

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ S3method(as.data.frame,probed_pomp)
1414
S3method(as.data.frame,wpfilterd_pomp)
1515
S3method(c,Pomp)
1616
export(Csnippet)
17+
export(append_data)
1718
export(as_pomp)
1819
export(bake)
1920
export(blowflies1)
@@ -147,6 +148,8 @@ exportMethods(wpfilter)
147148
import(methods)
148149
importFrom(coda,mcmc)
149150
importFrom(coda,mcmc.list)
151+
importFrom(data.table,fread)
152+
importFrom(data.table,fwrite)
150153
importFrom(data.table,rbindlist)
151154
importFrom(deSolve,diagnostics)
152155
importFrom(deSolve,ode)

R/bake.R

+37-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
##' Therefore, avoid using \sQuote{pomp} objects as dependencies in \code{bake} and \code{stew}.
3636
##' @param file Name of the archive file in which the result will be stored or retrieved, as appropriate.
3737
##' For \code{bake}, this will contain a single object and hence be an RDS file (extension \sQuote{rds});
38-
##' for \code{stew}, this will contain one or more named objects and hence be an RDA file (extension \sQuote{rda}).
38+
##' for \code{stew}, this will contain one or more named objects and hence be an RDA file (extension \sQuote{rda});
39+
##' for \code{append_data}, this will be a CSV file.
3940
##' @param dir Directory holding archive files;
4041
##' by default, this is the current working directory.
4142
##' This can also be set using the global option \code{pomp_archive_dir}.
@@ -333,3 +334,38 @@ freeze <- function (expr,
333334
}
334335
val
335336
}
337+
338+
##' @rdname bake
339+
##' @param data data frame
340+
##' @param overwrite logical; if \code{TRUE}, \code{data} are written to \code{file}, replacing any existing contents.
341+
##' If \code{FALSE}, the \code{data} is appended to the existing contents of \code{file}.
342+
##' @return
343+
##' \code{append_data} returns a data frame containing the new contents of \code{file}, invisibly.
344+
##' @importFrom data.table fread fwrite rbindlist
345+
##' @export
346+
append_data <- function (
347+
data,
348+
file,
349+
overwrite = FALSE,
350+
dir = getOption("pomp_archive_dir",getwd())
351+
) {
352+
tryCatch({
353+
file <- create_path(dir,file)
354+
append <- file.exists(file) && !as.logical(overwrite)
355+
if (append) {
356+
data <- rbindlist(
357+
list(
358+
fread(file=file),
359+
data
360+
),
361+
fill=TRUE,
362+
use.names=TRUE
363+
)
364+
}
365+
fwrite(data,file=file)
366+
},
367+
error=function (e) {
368+
pStop(who="append_data",conditionMessage(e))
369+
})
370+
invisible(data)
371+
}

inst/NEWS

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
_N_e_w_s _f_o_r _p_a_c_k_a_g_e '_p_o_m_p'
22

3+
_C_h_a_n_g_e_s _i_n '_p_o_m_p' _v_e_r_s_i_o_n _6._0._4:
4+
5+
• The new function ‘append_data’ appends a data frame to an
6+
existing CSV file (creating the file if it does not exist).
7+
This facilitates keeping a database of parameter-space
8+
explorations.
9+
10+
_C_h_a_n_g_e_s _i_n '_p_o_m_p' _v_e_r_s_i_o_n _6._0._3:
11+
12+
• The new function ‘eeulermultinom’ gives the expectation of an
13+
Euler-multinomial random variable.
14+
315
_C_h_a_n_g_e_s _i_n '_p_o_m_p' _v_e_r_s_i_o_n _6._0._2:
416

517
• The ‘save.states’ option to ‘pfilter’ has changed. See

inst/NEWS.Rd

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
\name{NEWS}
22
\title{News for package `pomp'}
3+
\section{Changes in \pkg{pomp} version 6.0.4}{
4+
\itemize{
5+
\item The new function \code{append_data} appends a data frame to an existing CSV file (creating the file if it does not exist).
6+
This facilitates keeping a database of parameter-space explorations.
7+
}
8+
}
9+
\section{Changes in \pkg{pomp} version 6.0.3}{
10+
\itemize{
11+
\item The new function \code{eeulermultinom} gives the expectation of an Euler-multinomial random variable.
12+
}
13+
}
314
\section{Changes in \pkg{pomp} version 6.0.2}{
415
\itemize{
516
\item The \code{save.states} option to \code{pfilter} has changed.

man/bake.Rd

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/save.R

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library(dplyr)
2+
library(tidyr)
3+
library(pomp)
4+
set.seed(1800076828)
5+
ricker() -> po
6+
options(pomp_archive_dir=tempdir())
7+
8+
simulate(po,nsim=20) |>
9+
coef() |>
10+
melt() |>
11+
pivot_wider() |>
12+
append_data("tmp.csv",overwrite=TRUE)
13+
14+
simulate(po,nsim=20,times=1:3) |>
15+
as.data.frame() |>
16+
rename(.id=.L1) |>
17+
append_data("tmp.csv") -> dat
18+
19+
data.table::fread(file.path(tempdir(),"tmp.csv")) -> dat1
20+
21+
stopifnot(all.equal(dat,dat1))
22+
23+
try(append_data("bob",file="tmp.csv"))
24+
try(append_data("bob",file="tmp.csv",overwrite=TRUE))

tests/save.Rout.save

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
3+
Copyright (C) 2024 The R Foundation for Statistical Computing
4+
Platform: x86_64-pc-linux-gnu
5+
6+
R is free software and comes with ABSOLUTELY NO WARRANTY.
7+
You are welcome to redistribute it under certain conditions.
8+
Type 'license()' or 'licence()' for distribution details.
9+
10+
Natural language support but running in an English locale
11+
12+
R is a collaborative project with many contributors.
13+
Type 'contributors()' for more information and
14+
'citation()' on how to cite R or R packages in publications.
15+
16+
Type 'demo()' for some demos, 'help()' for on-line help, or
17+
'help.start()' for an HTML browser interface to help.
18+
Type 'q()' to quit R.
19+
20+
> library(dplyr)
21+
22+
Attaching package: 'dplyr'
23+
24+
The following objects are masked from 'package:stats':
25+
26+
filter, lag
27+
28+
The following objects are masked from 'package:base':
29+
30+
intersect, setdiff, setequal, union
31+
32+
> library(tidyr)
33+
> library(pomp)
34+
> set.seed(1800076828)
35+
> ricker() -> po
36+
> options(pomp_archive_dir=tempdir())
37+
>
38+
> simulate(po,nsim=20) |>
39+
+ coef() |>
40+
+ melt() |>
41+
+ pivot_wider() |>
42+
+ append_data("tmp.csv",overwrite=TRUE)
43+
>
44+
> simulate(po,nsim=20,times=1:3) |>
45+
+ as.data.frame() |>
46+
+ rename(.id=.L1) |>
47+
+ append_data("tmp.csv") -> dat
48+
>
49+
> data.table::fread(file.path(tempdir(),"tmp.csv")) -> dat1
50+
>
51+
> stopifnot(all.equal(dat,dat1))
52+
>
53+
> try(append_data("bob",file="tmp.csv"))
54+
Error : in 'append_data': Item 2 of input is not a data.frame, data.table or list
55+
> try(append_data("bob",file="tmp.csv",overwrite=TRUE))
56+
Error : in 'append_data': is.list(x) is not TRUE
57+
>

0 commit comments

Comments
 (0)