diff --git a/R/table-notes.R b/R/table-notes.R index b77bf556..71e9b7a8 100644 --- a/R/table-notes.R +++ b/R/table-notes.R @@ -26,7 +26,7 @@ #' ) #' #'@export -tab_notes <- function(notes, escape_fun = tab_escape, +tab_notes <- function(notes = character(0), escape_fun = tab_escape, note_config = noteconf(type = "tpt"), r_file = getOption("mrg.script", NULL), r_file_label = "Source code: ", @@ -42,7 +42,7 @@ tab_notes <- function(notes, escape_fun = tab_escape, r_file_label, output_file_label, path.type = path.type) - notes <- c(notes, file_info$notes) + notes <- c(notes, file_info$file_notes) if(note_config$sanitize) { assert_that(is.character(notes) || is.null(notes)) @@ -101,7 +101,7 @@ tab_files <- function(output_file, output_dir, r_file = NULL, ) list( - notes = notes, # Notes entries for r and output files + file_notes = notes, # Notes entries for r and output files r_file = r_file, # Name of the R file output_file = output_file, # Full path to output file output_note = output_note, # Output file, formatted diff --git a/R/table-object.R b/R/table-object.R index 53abdc4f..0370629c 100644 --- a/R/table-object.R +++ b/R/table-object.R @@ -22,7 +22,7 @@ st_arg_names <- c( "sumrows", "note_config", "clear_reps", "clear_grouped_reps", "hline_at", "hline_from", "sizes", "units", "drop", "lt_cap_text", "lt_cap_macro", "lt_cap_label", "lt_cap_short", "lt_continue", - "caption", + "caption", "path.type", "args" ) @@ -491,6 +491,8 @@ st_right <- function(x,...) { #' See the `r_file` and `output_file` arguments passed to [stable()] and then #' to [tab_notes()]. #' +#' @inheritParams tab_notes +#' #' @param x an stobject. #' @param r set `r_file`, passed to [stable()]. #' @param output set `output_file`, the `.tex` file containing rendered @@ -510,7 +512,7 @@ st_right <- function(x,...) { #' @export st_files <- function(x, r = getOption("mrg.script", NULL), output = NULL, output_dir = getOption("pmtables.dir"), - esc = NULL) { + path.type = NULL, esc = NULL) { check_st(x) if(!missing(r)) { if(!is.null(esc)) r <- tab_escape(r, esc = esc) @@ -523,6 +525,9 @@ st_files <- function(x, r = getOption("mrg.script", NULL), output = NULL, if(is.character(output_dir)) { x$output_dir <- output_dir } + if(is.character(path.type)) { + x$path.type <- path.type + } x } diff --git a/man/format_table_path.Rd b/man/format_table_path.Rd new file mode 100644 index 00000000..920acb9f --- /dev/null +++ b/man/format_table_path.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{format_table_path} +\alias{format_table_path} +\title{Format the path to output file} +\usage{ +format_table_path(file, dir = NULL, path.type = c("proj", "none", "raw")) +} +\arguments{ +\item{file}{the name of the output file.} + +\item{dir}{the directory where the output file is stored.} + +\item{path.type}{a character string indicating how the path to the output +file should be formatted; use \code{"proj"} to have the path expressed relative +to an RStudio project file; use \code{"none"} to format without any directory +information (just the output file name); use \code{"raw"} to print the complete +path to the file using \code{dir} as-is.} +} +\value{ +A string with the formatted image file path. +} +\description{ +Format the path to output file +} +\details{ +Note that the default value for \code{path.type} is \code{"proj"}. This requires +that an RStudio project file is able to be found using +\code{\link[rprojroot:find_root]{rprojroot::find_root()}} with the \link[rprojroot:criteria]{rprojroot::is_rstudio_project} criterion. + +Once mrggsave finds a root for a given working directory, it caches the value +for the remainder of the R session. An error will be generated if an image is +attempted to be saved using \code{path.type="proj"} but an RStudio project file +was not able to be located. +} +\examples{ +\dontrun{ +format_table_path("foo.tex", "my/path") +format_table_path("foo.tex", "my/path", path.type = "proj") +format_table_path("foo.tx", "my/path", path.type = "raw") +} + +} diff --git a/man/st_files.Rd b/man/st_files.Rd index 1c647581..54b168bb 100644 --- a/man/st_files.Rd +++ b/man/st_files.Rd @@ -9,6 +9,7 @@ st_files( r = getOption("mrg.script", NULL), output = NULL, output_dir = getOption("pmtables.dir"), + path.type = NULL, esc = NULL ) } @@ -24,6 +25,10 @@ file; passed to \code{\link[=stable]{stable()}}.} \item{output_dir}{directory for \code{output_file}; this will be overridden with a warning if \code{output_file} contains the full path to the file.} +\item{path.type}{whether to include the path to the output file in the +table notes and how to format it; options include "none", "proj", and +"raw"; see \code{\link[=format_table_path]{format_table_path()}}.} + \item{esc}{passed to \code{\link[=tab_escape]{tab_escape()}}; use \code{NULL} to bypass escaping.} } \description{ diff --git a/man/tab_notes.Rd b/man/tab_notes.Rd index 30943744..b797c1a5 100644 --- a/man/tab_notes.Rd +++ b/man/tab_notes.Rd @@ -5,7 +5,7 @@ \title{Form table notes} \usage{ tab_notes( - notes, + notes = character(0), escape_fun = tab_escape, note_config = noteconf(type = "tpt"), r_file = getOption("mrg.script", NULL), diff --git a/tests/testthat/test-table-utils.R b/tests/testthat/test-table-utils.R index 0ca5b782..2b20c498 100644 --- a/tests/testthat/test-table-utils.R +++ b/tests/testthat/test-table-utils.R @@ -199,7 +199,7 @@ test_that("option to format path - non project", { expect_equal(x$output_note, file) }) -test_that("option to format path, path.type = proj", { +test_that("option to format path - path.type = proj", { on.exit(options(pmtables.path.type = NULL, pmtables.dir = NULL), add = TRUE) tdir <- tempfile("pmtables-test-") #normalizePath(tempdir(), "pmtables-test-") fs::dir_create(tdir) @@ -251,6 +251,21 @@ test_that("option to format path, path.type = proj", { file.path(dir_proj, dir), path.type = "proj" ), "tables/foo/bar.tex") + + x <- pmtables:::tab_notes( + output_file = "bar.tex", + output_dir = "tables/foo", + path.type = "proj" + ) + expect_equal(x$file_notes, "Source file: subdir/tables/foo/bar.tex") + + options(pmtables.path.type = "proj") + y <- pmtables:::tab_notes( + output_file = "bar.tex", + output_dir = "tables/foo" + ) + expect_equal(x$notes, y$notes) + options(pmtables.path.type = NULL) }) }) @@ -263,4 +278,3 @@ test_that("table-utils paste units [PMT-TEST-0239]", { c("B mg", "E", "D kg", "C pounds", "A") ) }) -