Skip to content

Commit a103439

Browse files
committed
repurpose SimCheck
1 parent 1176c34 commit a103439

File tree

4 files changed

+55
-66
lines changed

4 files changed

+55
-66
lines changed

R/SimCheck.R

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,51 @@
1-
#' Check the status of the simulation's temporary results
1+
#' Check for missing files in array simulations
22
#'
3-
#' This function reads the temporary file saved by \code{\link{runSimulation}}
4-
#' by collapsing the information into a suitable (albeit temporary) object of
5-
#' class \code{'SimDesign'}. This is useful when taking a quick-peak at how the
6-
#' early simulation results are performing (useful long running simulation
7-
#' results with many rows in the \code{Design} object). Returns a tibble-based
8-
#' data.frame object (\code{tbl_df}).
3+
#' Given the saved files from a \code{\link{runArraySimulation}} remote
4+
#' evaluation check whether all \code{.rds} files have been saved. If missing
5+
#' the missing row condition numbers will be returned
96
#'
10-
#' @param file the temp file currently saving the simulation state. If missing
11-
#' the file is assumed to be in the current working directory, and start with the
12-
#' name \code{'SIMDESIGN-TEMPFILE'}
7+
#' @param files vector of file names referring to the saved simulation files.
8+
#' E.g. \code{c('mysim-1.rds', 'mysim-2.rds', ...)}
139
#'
14-
#' @seealso \code{\link{runSimulation}}
10+
#' @param min minimum number after the \code{'-'} deliminator. Default is 1
11+
#'
12+
#' @param max maximum number after the \code{'-'} deliminator. If not specified
13+
#' is taken to be the highest number of the \code{files} input
14+
#'
15+
#' @seealso \code{\link{runArraySimulation}}
1516
#'
1617
#' @references
1718
#'
1819
#' Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations
1920
#' with the SimDesign Package. \code{The Quantitative Methods for Psychology, 16}(4), 248-280.
2021
#' \doi{10.20982/tqmp.16.4.p248}
2122
#'
22-
#' Sigal, M. J., & Chalmers, R. P. (2016). Play it again: Teaching statistics with Monte
23-
#' Carlo simulation. \code{Journal of Statistics Education, 24}(3), 136-156.
24-
#' \doi{10.1080/10691898.2016.1246953}
25-
#'
2623
#' @export
2724
#'
2825
#' @author Phil Chalmers \email{rphilip.chalmers@@gmail.com}
2926
#'
3027
#' @examples
3128
#' \dontrun{
3229
#'
33-
#' # explicit
34-
#' temp_results <- SimCheck(file = 'SIMDESIGN-TEMPFILE_mycomp.rds')
35-
#' temp_results
36-
#'
37-
#' # works if file is in the current working directory
38-
#' temp_results <- SimCheck()
39-
#' temp_results
30+
#' setwd('mysimfiles/')
31+
#' files <- dir()
32+
#' SimCheck()
4033
#'
4134
#' }
4235
#'
43-
SimCheck <- function(file){
44-
pat <- 'SIMDESIGN-TEMPFILE'
45-
input <- if(missing(file)){
46-
files <- dir()
47-
pick <- grepl(pat, files)
48-
if(!any(pick)){
49-
message("No temporary file found")
50-
return(invisible(NULL))
51-
}
52-
readRDS(files[pick][1L])
53-
} else {
54-
if(!file.exists(file)){
55-
message("file name does not exist")
56-
return(invisible(NULL))
57-
}
58-
readRDS(file)
36+
SimCheck <- function(files, min = 1L, max = NULL){
37+
filename <- strsplit(files[1], '-')[[1L]][1L]
38+
if(is.null(max)){
39+
subfiles <- gsub(paste0(filename, '-'), files, replacement = '')
40+
subfiles <- gsub('.rds', subfiles, replacement = '')
41+
max <- max(as.integer(subfiles))
5942
}
60-
ret <- dplyr::as_tibble(dplyr::bind_rows(input))
61-
ret <- subset(ret, select = !(names(ret) %in% c('ID', 'REPLICATION')))
62-
ret
43+
minmax <- min:max
44+
notin <- !(paste0(filename, '-', minmax, '.rds') %in% files)
45+
if(any(notin)){
46+
cat(sprintf('The following row conditions were missing:\n%s',
47+
paste0(minmax[notin], collapse=',')))
48+
} else
49+
cat(sprintf('No missing conditions from %i to %i were detected', min, max))
50+
invisible(NULL)
6351
}

R/runArraySimulation.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@
129129
#' \doi{10.1080/10691898.2016.1246953}
130130
#'
131131
#' @seealso \code{\link{runSimulation}}, \code{\link{expandDesign}},
132-
#' \code{\link{gen_seeds}}, \code{\link{SimCollect}}, \code{\link{getArrayID}}
132+
#' \code{\link{gen_seeds}}, \code{\link{SimCheck}},
133+
#' \code{\link{SimCollect}}, \code{\link{getArrayID}}
133134
#'
134135
#' @examples
135136
#'
@@ -232,6 +233,9 @@
232233
#' # list saved files
233234
#' dir('sim/')
234235
#'
236+
#' # check that all files saved
237+
#' SimCheck(dir('sim/'))
238+
#'
235239
#' setwd('sim')
236240
#' condition14 <- readRDS('condition-14.rds')
237241
#' condition14

man/SimCheck.Rd

Lines changed: 16 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/runArraySimulation.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)