From 40e3f7d37be98533496c3cc64e9e033cecfe4f05 Mon Sep 17 00:00:00 2001 From: Kelly Street Date: Thu, 9 May 2024 14:43:51 -0700 Subject: [PATCH] added print.maze as per issue #3. updated GHA workflows --- .Rproj.user/shared/notebooks/paths | 4 +- .github/workflows/R-CMD-check.yaml | 90 +++++++--------------------- .github/workflows/test-coverage.yaml | 61 +++++++++++++++++++ DESCRIPTION | 3 +- NAMESPACE | 2 + R/maze.R | 43 +++++++++++++ README.md | 4 +- man/maze.Rd | 3 + man/print.maze.Rd | 40 +++++++++++++ tests/testthat/test-mazing.R | 6 +- 10 files changed, 184 insertions(+), 72 deletions(-) create mode 100644 .github/workflows/test-coverage.yaml create mode 100644 man/print.maze.Rd diff --git a/.Rproj.user/shared/notebooks/paths b/.Rproj.user/shared/notebooks/paths index f16657f..7265ba2 100644 --- a/.Rproj.user/shared/notebooks/paths +++ b/.Rproj.user/shared/notebooks/paths @@ -1 +1,3 @@ -/Users/kstreet/Projects/mazing/vignettes/inspiration.Rmd="242599EE" +/Users/kellystreet/Projects/Packages/mazing/R/plotting.R="6786BE2A" +/Users/kellystreet/Projects/Packages/mazing/tests/testthat.R="D8E3519A" +/Users/kellystreet/Projects/Packages/mazing/vignettes/inspiration.Rmd="CA08C1EB" diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 9aa95a3..0f2fe08 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,17 +1,15 @@ -# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. -# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: - - main - - master + branches: [main, master] pull_request: - branches: - - main - - master + branches: [main, master] name: R-CMD-check +permissions: read-all + jobs: R-CMD-check: runs-on: ${{ matrix.config.os }} @@ -22,75 +20,33 @@ jobs: fail-fast: false matrix: config: + - {os: macos-latest, r: 'release'} - {os: windows-latest, r: 'release'} - - {os: macOS-latest, r: 'release'} - - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} env: - R_REMOTES_NO_ERRORS_FROM_WARNINGS: true - RSPM: ${{ matrix.config.rspm }} GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-pandoc@v2 - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} + http-user-agent: ${{ matrix.config.http-user-agent }} + use-public-rspm: true - - uses: r-lib/actions/setup-pandoc@v1 - - - name: Query dependencies - run: | - install.packages('remotes') - saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) - writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") - shell: Rscript {0} - - - name: Restore R package cache - uses: actions/cache@v2 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - - name: Install system dependencies - if: runner.os == 'Linux' - run: | - while read -r cmd - do - eval sudo $cmd - done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') + extra-packages: any::rcmdcheck + needs: check - - name: Install dependencies - run: | - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("rcmdcheck") - shell: Rscript {0} - - - name: Check - env: - _R_CHECK_CRAN_INCOMING_REMOTE_: false - run: | - options(crayon.enabled = TRUE) - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") - shell: Rscript {0} - - - name: Upload check results - if: failure() - uses: actions/upload-artifact@main + - uses: r-lib/actions/check-r-package@v2 with: - name: ${{ runner.os }}-r${{ matrix.config.r }}-results - path: check - - - name: Install covr - if: runner.os == 'macOS' - run: | - remotes::install_cran("covr") - shell: Rscript {0} - - - name: Test coverage - if: runner.os == 'macOS' - run: covr::codecov() - shell: Rscript {0} \ No newline at end of file + upload-snapshots: true + build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml new file mode 100644 index 0000000..fefc52e --- /dev/null +++ b/.github/workflows/test-coverage.yaml @@ -0,0 +1,61 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: test-coverage + +permissions: read-all + +jobs: + test-coverage: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::covr, any::xml2 + needs: coverage + + - name: Test coverage + run: | + cov <- covr::package_coverage( + quiet = FALSE, + clean = FALSE, + install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") + ) + covr::to_cobertura(cov) + shell: Rscript {0} + + - uses: codecov/codecov-action@v4 + with: + fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }} + file: ./cobertura.xml + plugin: noop + disable_search: true + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Show testthat output + if: always() + run: | + ## -------------------------------------------------------------------- + find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true + shell: bash + + - name: Upload test results + if: failure() + uses: actions/upload-artifact@v4 + with: + name: coverage-test-failures + path: ${{ runner.temp }}/package diff --git a/DESCRIPTION b/DESCRIPTION index 83b1fb4..d3dd4c6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,6 +16,7 @@ Imports: methods, stats Suggests: + bittermelon, rmarkdown, emojifont, knitr, @@ -24,6 +25,6 @@ Suggests: testthat (>= 3.0.0), covr VignetteBuilder: knitr -RoxygenNote: 7.1.1 +RoxygenNote: 7.3.1 BugReports: https://github.com/kstreet13/mazing/issues Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index fe3baf7..5695ec7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ S3method(lines,maze) S3method(plot,maze) +S3method(print,maze) export(as.maze) export(condense_matrix) export(expand_matrix) @@ -15,3 +16,4 @@ importFrom(graphics,lines) importFrom(methods,as) importFrom(methods,is) importFrom(stats,median) +importFrom(utils,packageVersion) diff --git a/R/maze.R b/R/maze.R index 7f3ec0e..be922bb 100644 --- a/R/maze.R +++ b/R/maze.R @@ -14,6 +14,8 @@ #' @return For \code{is.maze} a logical value indicating if the input is a valid #' \code{maze}. #' +#' @seealso [print.maze()] +#' #' @examples #' maze(10,10) #' @export @@ -80,3 +82,44 @@ as.maze <- function(x){ } +#' @title Print Maze +#' @name print.maze +#' @description +#' Display a \code{maze} object. +#' +#' @param x A \code{maze} object. +#' @param walls logical value, indicating that the walls of the maze should be +#' plotted, rather than the paths through the maze. Default is \code{FALSE}. +#' @param start,end The coordinates of the starting and ending points of a path +#' through the maze, or descriptions of relative locations (eg. +#' \code{"topleft"}, see \code{\link{find_maze_refpoint}}). If not provided, +#' no path will be drawn. +#' @param compress How to compress the bitmap representation, see +#' \code{\link[bittermelon]{print.bm_bitmap}}. For \code{maze} objects, the +#' default is \code{"v"} for vertical. +#' @param ... Further arguments passed to +#' \code{\link[bittermelon]{print.bm_bitmap}}, if the \code{bittermelon} +#' package is available. +#' +#' @details If the \code{\link[bittermelon]{bittermelon}} package is available, +#' \code{print.maze} prints a representation of the maze az a bitmap object. +#' Otherwise, it prints the matrix representation. +#' +#' @examples +#' m <- maze(10,10) +#' print(m) +#' +#' @importFrom utils packageVersion +#' @export +print.maze <- function(x, ..., walls = FALSE, start = NULL, end = NULL, + compress = "v") { + if(requireNamespace("bittermelon", quietly = TRUE) && + packageVersion("bittermelon") >= "1.2.0-2"){ + bm <- bittermelon::as_bm_bitmap(x, walls = walls, start = start, end = end) + print(bm, compress = compress, ...) + }else{ + print.default(x) + } +} + + diff --git a/README.md b/README.md index 648be15..f9639c2 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ # mazing -[![R-CMD-check](https://github.com/kstreet13/mazing/workflows/R-CMD-check/badge.svg)](https://github.com/kstreet13/mazing/actions) -[![Codecov test coverage](https://codecov.io/gh/kstreet13/mazing/branch/master/graph/badge.svg)](https://codecov.io/gh/kstreet13/mazing?branch=master) +[![R-CMD-check](https://github.com/kstreet13/mazing/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/kstreet13/mazing/actions/workflows/R-CMD-check.yaml) +[![test-coverage](https://github.com/kstreet13/mazing/actions/workflows/test-coverage.yaml/badge.svg)](https://github.com/kstreet13/mazing/actions/workflows/test-coverage.yaml) diff --git a/man/maze.Rd b/man/maze.Rd index 0cdbcf0..d8588c2 100644 --- a/man/maze.Rd +++ b/man/maze.Rd @@ -55,3 +55,6 @@ for(i in 1:nrow(mat)){ m <- as.maze(mat) } +\seealso{ +[print.maze()] +} diff --git a/man/print.maze.Rd b/man/print.maze.Rd new file mode 100644 index 0000000..a5bfd4a --- /dev/null +++ b/man/print.maze.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/maze.R +\name{print.maze} +\alias{print.maze} +\title{Print Maze} +\usage{ +\method{print}{maze}(x, ..., walls = FALSE, start = NULL, end = NULL, compress = "v") +} +\arguments{ +\item{x}{A \code{maze} object.} + +\item{...}{Further arguments passed to +\code{\link[bittermelon]{print.bm_bitmap}}, if the \code{bittermelon} +package is available.} + +\item{walls}{logical value, indicating that the walls of the maze should be +plotted, rather than the paths through the maze. Default is \code{FALSE}.} + +\item{start, end}{The coordinates of the starting and ending points of a path +through the maze, or descriptions of relative locations (eg. +\code{"topleft"}, see \code{\link{find_maze_refpoint}}). If not provided, +no path will be drawn.} + +\item{compress}{How to compress the bitmap representation, see +\code{\link[bittermelon]{print.bm_bitmap}}. For \code{maze} objects, the +default is \code{"v"} for vertical.} +} +\description{ +Display a \code{maze} object. +} +\details{ +If the \code{\link[bittermelon]{bittermelon}} package is available, + \code{print.maze} prints a representation of the maze az a bitmap object. + Otherwise, it prints the matrix representation. +} +\examples{ +m <- maze(10,10) +print(m) + +} diff --git a/tests/testthat/test-mazing.R b/tests/testthat/test-mazing.R index 1ef3030..96f9c1a 100644 --- a/tests/testthat/test-mazing.R +++ b/tests/testthat/test-mazing.R @@ -74,8 +74,12 @@ test_that("pathfinding works as expected", { expect_true(all(p[,2] == 1)) }) -test_that("plotting functions do not give errors", { +test_that("plotting and printing functions do not give errors", { m <- maze(10, 10) + + expect_invisible(print(m)) + # can't find a way to test this and the matrix print method + expect_invisible(plot(m)) expect_invisible(plot(m, walls = TRUE)) expect_invisible(plot(m, adjust = c(.5,.5)))