diff --git a/.Rbuildignore b/.Rbuildignore index 68806069..e9d11a3d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -12,3 +12,5 @@ _pkgdown.yml docs/ ^.lintr$ ^CRAN-SUBMISSION$ +^\.claude$ +^\.coderabbit\.yaml$ diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 00000000..11bd52f0 --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,2 @@ +reviews: + request_changes_workflow: true diff --git a/.github/workflows/R-CMD-check-docker.yaml b/.github/workflows/R-CMD-check-docker.yaml index feb1ae9d..58582a7b 100644 --- a/.github/workflows/R-CMD-check-docker.yaml +++ b/.github/workflows/R-CMD-check-docker.yaml @@ -37,7 +37,7 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 5577b8f4..c94766e4 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -21,9 +21,10 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes + OMP_NUM_THREADS: 1 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/_pkgdown.yml b/.github/workflows/_pkgdown.yml index ba2f3aea..53e00ff5 100644 --- a/.github/workflows/_pkgdown.yml +++ b/.github/workflows/_pkgdown.yml @@ -20,7 +20,7 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/lint_changed_files.yaml b/.github/workflows/lint_changed_files.yaml index 5e2f02a9..ffc7f403 100644 --- a/.github/workflows/lint_changed_files.yaml +++ b/.github/workflows/lint_changed_files.yaml @@ -16,7 +16,7 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 diff --git a/.github/workflows/render_readme.yml b/.github/workflows/render_readme.yml index 8536b194..f994ee94 100644 --- a/.github/workflows/render_readme.yml +++ b/.github/workflows/render_readme.yml @@ -24,7 +24,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - name: Checkout repos - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup R uses: r-lib/actions/setup-r@v2 diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index f8550927..d67bab28 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -19,7 +19,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: @@ -61,7 +61,7 @@ jobs: - name: Upload test results if: failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: coverage-test-failures path: ${{ runner.temp }}/package diff --git a/DESCRIPTION b/DESCRIPTION index d245dd67..6d271d72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: rbi -Version: 1.0.0.9000 +Version: 1.0.1 Title: Interface to 'LibBi' Authors@R: c( @@ -38,7 +38,7 @@ URL: https://github.com/sbfnk/rbi BugReports: https://github.com/sbfnk/rbi/issues SystemRequirements: LibBi (>= 1.4.2) LazyLoad: no -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.3 Language: en-GB Encoding: UTF-8 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 2ed6db42..4316c68a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -105,5 +105,6 @@ importFrom(processx,run) importFrom(reshape2,melt) importFrom(stats,as.formula) importFrom(stats,median) +importFrom(stats,na.omit) importFrom(stats,quantile) importFrom(stats,runif) diff --git a/NEWS.md b/NEWS.md index fc68c69b..5fc5b916 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ -# rbi (development version) +# rbi 1.0.1 -* work with character columns in sparse input data +* Character columns in sparse input data are now correctly handled by + converting them to factors (#54). # rbi 1.0.0 diff --git a/R/bi_write.R b/R/bi_write.R index 19fa572c..146c1dbc 100644 --- a/R/bi_write.R +++ b/R/bi_write.R @@ -340,7 +340,7 @@ bi_write <- function(filename, variables, append = FALSE, overwrite = FALSE, nc_close(nc) - return(list(time_dim = time_dim, coord_dims = coord_dims, dims = dim_factors)) + list(time_dim = time_dim, coord_dims = coord_dims, dims = dim_factors) } ##' Check if a variable is sparse @@ -379,7 +379,7 @@ check_sparse_var <- function(x, coord_cols, value_column) { length(setdiff(levels(all_values[[x]]), all_values[[x]])) == 0 }, logical(1)) - return(any(!all[["all_equal"]]) || any(!all_factors)) + any(!all[["all_equal"]]) || any(!all_factors) } ##' Create a coordinate variable @@ -457,10 +457,10 @@ create_coord_var <- function(name, dims, dim_factors, coord_dim, index_table, coord_var_dims <- c(list(ns_dim), coord_var_dims) } var <- ncvar_def(coord_var, "", coord_var_dims) - return(list( + list( name = coord_var, values = values, var = var, dim = coord_index_dim, dim_factors = dim_factors - )) + ) } ##' Get the factor levels of all character columns in data @@ -468,6 +468,8 @@ create_coord_var <- function(name, dims, dim_factors, coord_dim, index_table, ##' @param ... variable lists ##' @return a list with elements that represent the factor levels present in ##' character columns +##' @importFrom stats na.omit +##' @keywords internal ##' @author Sebastian Funk get_char_levels <- function(...) { levels <- list() @@ -484,7 +486,7 @@ get_char_levels <- function(...) { } } } - return(levels) + levels } ##' Convert character columns to factors in data @@ -492,6 +494,7 @@ get_char_levels <- function(...) { ##' @param levels factor levels, as a named list, each representing one column ##' @inheritParams bi_write ##' @return the \code{variables} argument with factorised columns +##' @keywords internal ##' @author Sebastian Funk factorise <- function(variables, levels) { data_frames <- names(variables)[ @@ -504,9 +507,9 @@ factorise <- function(variables, levels) { if (col %in% colnames(df)) { df[[col]] <- factor(df[[col]], levels = levels[[col]]) } - return(df) + df }) } } - return(variables) + variables } diff --git a/R/libbi.R b/R/libbi.R index 9ddb06fc..440c4ab4 100644 --- a/R/libbi.R +++ b/R/libbi.R @@ -64,13 +64,13 @@ libbi <- function(model, path_to_libbi, dims, use_cache = TRUE, ...) { dot_options <- list(...) if ("norun" %in% names(dot_options) && dot_options[["norun"]]) { new_obj$model <- model - return(new_obj) + new_obj } else { - return(do.call( + do.call( run.libbi, c( list(x = new_obj, model = model, client = character(0)), dot_options ) - )) + ) } } @@ -510,7 +510,16 @@ run.libbi <- function(x, client, proposal = c("model", "prior"), model, fix, ) ) ) - stop_msg <- paste0("LibBi terminated with \"", error_msg[1], "\".") + if (length(error_msg) == 0 || is.na(error_msg[1])) { + stop_msg <- paste0( + "LibBi terminated with exit code ", p$status, "." + ) + if (nchar(p$stderr) > 0) { + stop_msg <- paste0(stop_msg, "\nstderr: ", p$stderr) + } + } else { + stop_msg <- paste0("LibBi terminated with \"", error_msg[1], "\".") + } if (length(x$log_file_name) > 0) { stop_msg <- paste0( stop_msg, "\nYou can view a full log using \"print_log('", @@ -533,13 +542,13 @@ run.libbi <- function(x, client, proposal = c("model", "prior"), model, fix, x <- update(x) ## get original model back if it has been modified x$model <- save_model - return(x) + x } } else { ## if run from the constructor, just write the model and add all the options write_model(x) x$options <- all_options - return(x) + x } } @@ -878,7 +887,7 @@ attach_data.libbi <- function(x, file, data, in_place = FALSE, append = FALSE, x$options[[paste0(file, "-file")]] <- target_file_name x$timestamp[[file]] <- file.mtime(target_file_name) } - return(x) + x } #' @export @@ -1063,7 +1072,7 @@ read_libbi <- function(name, ...) { new_obj$supplement <- read_obj$supplement - return(new_obj) + new_obj } #' @export @@ -1199,7 +1208,7 @@ summary.libbi <- function(object, type = c("param", "state", "noise", "obs"), na.rm = na.rm )) ), by = summarise_columns] - return(dt) + dt })) ## reorder table numeric_columns <- c( @@ -1225,7 +1234,7 @@ summary.libbi <- function(object, type = c("param", "state", "noise", "obs"), ) setDF(summary_table) - return(summary_table) + summary_table } #' @keywords internal @@ -1336,7 +1345,7 @@ sample_obs <- function(x, ...) { x$options[["without-transform-obs-to-state"]] pr$model <- x$model - return(pr) + pr } #' @export @@ -1395,9 +1404,9 @@ logLik.libbi <- function(object, ...) { assert_files(object) res <- bi_read(object) if (is.vector(res$loglikelihood)) { - return(res$loglikelihood) + res$loglikelihood } else { - return(res$loglikelihood$value) + res$loglikelihood$value } } @@ -1432,7 +1441,7 @@ update.libbi <- function(x, ...) { x$timestamp[["output"]] <- file.mtime(x$output_file_name) } } - return(x) + x } #' @export update.default <- function(x, ...) { @@ -1455,5 +1464,5 @@ create_working_folder <- function(x) { reg.finalizer(x$.gc_env, function(env) { unlink(env$folder, recursive = TRUE) }, onexit = TRUE) - return(x) + x } diff --git a/codecov.yml b/codecov.yml index edd296b8..0183c63d 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,2 +1,11 @@ codecov: token: 14c79f07-68d5-4cc4-b1fd-66a5aae3251e + +coverage: + status: + project: + default: + enabled: false + patch: + default: + enabled: false diff --git a/cran-comments.md b/cran-comments.md index 453aa0ff..13873e72 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,13 +1,7 @@ ## Test environments -* local macOS install, R 4.3.1 -* github Actions (macOS and Linux) -* checked with devtools::check_win_devel() +* local macOS install (aarch64), R 4.5.2 +* GitHub Actions (macOS), R release +* GitHub Actions (Ubuntu), R release, R devel, R oldrel-1 ## R CMD check results -There were no ERRORs or WARNINGs, or NOTEs. - -## Downstream dependencies -0 packages with problems. - -## Updates after initial submission. -A reverse dependency issue with rbi.helpers is expected and being fixed by submitting an updated version of rbi.helpers to CRAN. +There were no ERRORs, WARNINGs, or NOTEs. diff --git a/man/factorise.Rd b/man/factorise.Rd new file mode 100644 index 00000000..6bf19d70 --- /dev/null +++ b/man/factorise.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bi_write.R +\name{factorise} +\alias{factorise} +\title{Convert character columns to factors in data} +\usage{ +factorise(variables, levels) +} +\arguments{ +\item{variables}{a \code{list} object, the names of which should be the +variable names and values should be either single values or data frames} + +\item{levels}{factor levels, as a named list, each representing one column} +} +\value{ +the \code{variables} argument with factorised columns +} +\description{ +Convert character columns to factors in data +} +\author{ +Sebastian Funk +} +\keyword{internal} diff --git a/man/get_char_levels.Rd b/man/get_char_levels.Rd new file mode 100644 index 00000000..e4f38cf7 --- /dev/null +++ b/man/get_char_levels.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bi_write.R +\name{get_char_levels} +\alias{get_char_levels} +\title{Get the factor levels of all character columns in data} +\usage{ +get_char_levels(...) +} +\arguments{ +\item{...}{variable lists} +} +\value{ +a list with elements that represent the factor levels present in + character columns +} +\description{ +Get the factor levels of all character columns in data +} +\author{ +Sebastian Funk +} +\keyword{internal} diff --git a/tests/testthat/test-libbi.r b/tests/testthat/test-libbi.r index 941e1eca..9092c5eb 100644 --- a/tests/testthat/test-libbi.r +++ b/tests/testthat/test-libbi.r @@ -1,5 +1,7 @@ context("Testing running libbi") +options(libbi_args = list(assert = FALSE)) + model_str <- " model test { const no_a = 2 @@ -60,7 +62,7 @@ test_output <- bi <- attach_data(bi, "output", test_output) config_tmp_file <- tempfile(fileext = ".conf") -writeLines("--enable-assert", config_tmp_file) +writeLines("--disable-assert", config_tmp_file) log_tmp_file <- tempfile(fileext = ".log") output_tmp_file <- tempfile(fileext = ".nc") model_tmp_file <- tempfile(fileext = ".bi") diff --git a/vignettes/rbi.Rmd b/vignettes/rbi.Rmd index a6f499b4..3e5431e5 100644 --- a/vignettes/rbi.Rmd +++ b/vignettes/rbi.Rmd @@ -14,6 +14,7 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) +options(libbi_args = list(assert = FALSE)) ``` This vignette gives an introduction to using **rbi**. For the best viewing experience, use the [version on the rbi website](https://sbfnk.github.io/rbi/articles/introduction.html). @@ -67,7 +68,7 @@ The main computational engine and model grammar behind **rbi** is provided by ** The **rbi** package mainly provides two classes: `bi_model` and `libbi`. The `bi_model` class is used to load, view and manipulate **LibBi** model files. The `libbi` class is used to run LibBi and perform inference. -The package also provides two methods for interacting with the [NetCDF](https://www.unidata.ucar.edu/software/netcdf/) files used by **LibBi**, `bi_read` and `bi_write`. Lastly, it provides a `get_traces` function to analyse Markov-chain Monte Carlo (MCMC) traces using the [coda](https://cran.r-project.org/package=coda) package. +The package also provides two methods for interacting with the [NetCDF](https://www.unidata.ucar.edu/software/netcdf) files used by **LibBi**, `bi_read` and `bi_write`. Lastly, it provides a `get_traces` function to analyse Markov-chain Monte Carlo (MCMC) traces using the [coda](https://cran.r-project.org/package=coda) package. # The `bi_model` class