From fbd73bd4819c9e6528b82ef2bc3146574e7264f7 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 11:33:31 -0500 Subject: [PATCH 1/8] Add `div.class` to `setFixest_etable` --- R/etable.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/etable.R b/R/etable.R index 0c7ea924..c64e8e04 100644 --- a/R/etable.R +++ b/R/etable.R @@ -939,7 +939,7 @@ etable = function(..., vcov = NULL, stage = 2, agg = NULL, # Arguments that can be set globally opts = getOption("fixest_etable") - args_global = c("postprocess.tex", "postprocess.df", "view", "markdown", "page.width") + args_global = c("postprocess.tex", "postprocess.df", "view", "markdown", "page.width", "div.class") for(arg in setdiff(args_global, names(mc))){ if(arg %in% names(opts)){ assign(arg, opts[[arg]]) @@ -4585,7 +4585,7 @@ setFixest_etable = function(digits = 4, digits.stats = 5, fitstat, style.df = NULL, notes = NULL, group = NULL, extralines = NULL, fixef.group = NULL, placement = "htbp", drop.section = NULL, view = FALSE, markdown = NULL, view.cache = FALSE, - page.width = "fit", + page.width = "fit", div.class = "etable", postprocess.tex = NULL, postprocess.df = NULL, fit_format = "__var__", meta.time = NULL, meta.author = NULL, meta.sys = NULL, @@ -4655,6 +4655,7 @@ setFixest_etable = function(digits = 4, digits.stats = 5, fitstat, check_arg(view, view.cache, "logical scalar") check_arg(markdown, "NULL scalar(logical, character)") + check_value(div.class, "character scalar") page.width = check_set_page_width(page.width) From 43d8f3dcd37f7cb82ec83e6740ec68b53cfd59a2 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 14:26:51 -0500 Subject: [PATCH 2/8] Allow named export in `etable`: - In `build_tex_png`, `*.png` is detected, use that as `png_name` and then remove from `export` - Needed to move `export_path` creation below to ensure this works --- R/etable.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/R/etable.R b/R/etable.R index c64e8e04..8b642deb 100644 --- a/R/etable.R +++ b/R/etable.R @@ -5372,10 +5372,6 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, } } - if(!is.null(export)){ - export_path = check_set_path(export, "w", up = up) - } - dir = NULL if(do_build){ @@ -5396,11 +5392,19 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, time = gsub(" .+", "", Sys.time()) png_name = .dsb("etable_tex_.[time]_.[id].png") } + } else if (!is.null(export) && grepl("^.+_|\\.png$", export)) { + # png name specified in export + png_name = basename(export) + export = dirname(export) } else { png_name = "etable.png" } } + if(!is.null(export)){ + export_path = check_set_path(export, "w", up = up, recursive = TRUE) + } + if(view || do_build){ # we set the working directory properly # used to: @@ -5603,7 +5607,7 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, if(view){ my_viewer = getOption("viewer") if(is.null(my_viewer)){ - warning("To preview the table, we need a viewer -- which wasn't found (it sjould work on RStudio and VScode).") + warning("To preview the table, we need a viewer -- which wasn't found (it should work on RStudio and VScode).") } else { # setting up the html document From 20dfe90591d6af73c04283bfb015c9ca2be91ac9 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 14:27:28 -0500 Subject: [PATCH 3/8] Cleanup comment --- R/etable.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/etable.R b/R/etable.R index 8b642deb..4ba295df 100644 --- a/R/etable.R +++ b/R/etable.R @@ -5720,8 +5720,6 @@ check_set_path = function(x, type = "", create = TRUE, up = 0){ } viewer_html_template = function(png_name){ - # I really wanted to see the full table all the time, so I had to add some JS. - # There must be some straightforward way in CSS, but I don't know it... .dsb0(' From e48ee211dcbba0f0b0013aeada6283fe42a459a2 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 14:43:24 -0500 Subject: [PATCH 4/8] Fix bug where `etable` won't work with `file` if directory don't exist --- R/etable.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/etable.R b/R/etable.R index 4ba295df..f2c424bb 100644 --- a/R/etable.R +++ b/R/etable.R @@ -1134,6 +1134,10 @@ etable = function(..., vcov = NULL, stage = 2, agg = NULL, # Export to file is_file = !missnull(file) if(is_file){ + # Create directory if it doesn't exist + if(!DIR_EXISTS(dirname(file))){ + dir.create(dirname(file), recursive = TRUE) + } error_sender(sink(file = file, append = !replace), "Argument 'file': error when creating the document in ", file) From f63dc80da4283e8857db1e5c2e19a3ad7cec7572 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 14:55:44 -0500 Subject: [PATCH 5/8] Fix bugs with absolute path not showing up: This one is a bit complicated as it happens only when `export` is used, the image is built in `tempdir()`, *and* the directory does not exist. `check_set_path` returns either relative or absolute path: - If `etable` has to make the directory, then relative path. - If it doesn't then, then absolute path. This creates problems when you `setwd(tempdir())` to build the image because `file.copy` copies into the tempdir when using a relative path --- R/etable.R | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/R/etable.R b/R/etable.R index f2c424bb..5564ae0c 100644 --- a/R/etable.R +++ b/R/etable.R @@ -5406,7 +5406,7 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, } if(!is.null(export)){ - export_path = check_set_path(export, "w", up = up, recursive = TRUE) + export_path = check_set_path(export, "w, dir", create = TRUE, up = up, recursive = TRUE) } if(view || do_build){ @@ -5649,13 +5649,14 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, } -check_set_path = function(x, type = "", create = TRUE, up = 0){ +check_set_path = function(x, type = "", create = TRUE, up = 0, recursive = FALSE){ # type: # + r: read (file or dir must exists), w (file is to be created) # + dir: directory and not a document # create: # - if file: creates the parent dir if the grand parent exists # - if dir: creates the dir only if grand parent exists + # - if recursive == TRUE: create all folders set_up(up + 1) @@ -5696,12 +5697,18 @@ check_set_path = function(x, type = "", create = TRUE, up = 0){ path_dir = str_trim(path, -nchar(file_name)) if(nchar(path_dir) == 0) path_dir = "." + if(create & recursive) { + dir.create(path, recursive = TRUE) + } + path_parent = dirname(path) if(dir.exists(path_parent)){ if(is_dir && create){ dir.create(path) } - + + # ensure absolute path is returned for working with `tempdir()` + path = try(normalizePath(path, "/", mustWork = FALSE)) return(path) } @@ -5712,7 +5719,9 @@ check_set_path = function(x, type = "", create = TRUE, up = 0){ if(is_dir){ dir.create(path) } - + + # ensure absolute path is returned for working with `tempdir()` + path = try(normalizePath(path, "/", mustWork = FALSE)) return(path) } } From b35618e705178dfa6380d4e3f4e7de92af063291 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 17:13:07 -0500 Subject: [PATCH 6/8] Improve markdown output location - `knitr::fig_path('.png')` creates a file in the Rmarkdown output directory. This is really useful since it can be processed with `self_contained`, for example - Additinally, knitr has `output.dir` option to, so this outputs into the correct output dir correctly. For example when using `quarto` with `execute-dir`, the output dir and the execute dir can differ. --- R/etable.R | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/R/etable.R b/R/etable.R index 5564ae0c..004c9a29 100644 --- a/R/etable.R +++ b/R/etable.R @@ -5343,7 +5343,12 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, # (In regular markdown, the temp dir is a bit more elegant since # it does not clutter the workspace) - markdown = "./images/etable/" + markdown = file.path( + knitr::opts_knit$get("output.dir"), + knitr::fig_path('.png') + ) + markdown = dirname(markdown) + # markdown = "./images/etable/" } # we need to clean all the tmp tags otherwise the caching does not work @@ -5360,7 +5365,7 @@ build_tex_png = function(x, view = FALSE, export = NULL, markdown = NULL, do_build = TRUE export_markdown = id = NULL if(!is.null(markdown)){ - markdown_path = check_set_path(markdown, "w, dir", create = TRUE, up = up) + markdown_path = check_set_path(markdown, "w, dir", create = TRUE, up = up, recursive = TRUE) all_files = list.files(markdown_path, "\\.png$", full.names = TRUE) id_all = gsub("^.+_|\\.png$", "", all_files) @@ -5698,7 +5703,9 @@ check_set_path = function(x, type = "", create = TRUE, up = 0, recursive = FALSE if(nchar(path_dir) == 0) path_dir = "." if(create & recursive) { - dir.create(path, recursive = TRUE) + if(!dir.exists(path)) { + dir.create(path, recursive = TRUE) + } } path_parent = dirname(path) From 884da533560ccc2e9f21f74d5338d4f00eed60a3 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 17:49:48 -0500 Subject: [PATCH 7/8] Fix documentation bug --- man/etable.Rd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/etable.Rd b/man/etable.Rd index 9e38d676..38051125 100644 --- a/man/etable.Rd +++ b/man/etable.Rd @@ -232,6 +232,7 @@ setFixest_etable( markdown = NULL, view.cache = FALSE, page.width = "fit", + div.class = "etable", postprocess.tex = NULL, postprocess.df = NULL, fit_format = "__var__", From 8623e0d773fc9b3302c6d89a7113916f0e408952 Mon Sep 17 00:00:00 2001 From: Kyle Butts Date: Tue, 20 Feb 2024 18:26:01 -0500 Subject: [PATCH 8/8] Fix when `output.dir` differs from execution directory --- R/etable.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/etable.R b/R/etable.R index 004c9a29..14d5e7e8 100644 --- a/R/etable.R +++ b/R/etable.R @@ -1174,7 +1174,10 @@ etable = function(..., vcov = NULL, stage = 2, agg = NULL, if(is_md){ if(!knitr::is_latex_output()){ - path = path_to_relative(path) + + orig = knitr::opts_knit$get("output.dir") + dest = normalizePath(path, "/", mustWork = FALSE) + path = path_to_relative(orig, dest) cat(sma('
\n')) return(invisible(NULL)) } @@ -7071,15 +7074,12 @@ is_Rmarkdown = function(){ "knitr" %in% loadedNamespaces() && !is.null(knitr::pandoc_to()) } -path_to_relative = function(x){ +path_to_relative = function(orig, dest){ # orig = "C:/Users/berge028/Google Drive/R_packages/fixest/fixest" # dest = "C:/Users/berge028/Google Drive/R_packages/automake/automake/NAMESPACE" # I'm not sure it works perfectly well on linux... - dest = normalizePath(x, "/", mustWork = FALSE) - orig = normalizePath(".", "/", mustWork = FALSE) - if(dest == orig) return(".") dest_split = strsplit(dest, "/")[[1]]