From 8fdcbf79ff95af47b876907bda75582466702254 Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 14:44:56 -0400 Subject: [PATCH 1/8] For #73: find pandoc automatically --- exec/chiimp.cmd | 3 +-- exec/chiimp.command | 3 +-- exec/chiimp.sh | 2 +- exec/find_pandoc.R | 47 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100755 exec/find_pandoc.R diff --git a/exec/chiimp.cmd b/exec/chiimp.cmd index 49cce45..343e8f5 100755 --- a/exec/chiimp.cmd +++ b/exec/chiimp.cmd @@ -15,8 +15,7 @@ set rpath=%rpath: "%1"=% set rpath=%rpath:"=% set rdir=%rpath%\..\ -REM TODO: detect RStudio instead of presuming the path -set RSTUDIO_PANDOC=C:\Program Files\RStudio\bin\pandoc +for /f "tokens=* usebackq" %%x in (`%rdir%\Rscript %dir%\find_pandoc.R`) do set RSTUDIO_PANDOC=%%x if "%~1"=="" ( echo.To run CHIIMP, drag and drop a configuration file onto this icon. diff --git a/exec/chiimp.command b/exec/chiimp.command index 4251391..c554776 100755 --- a/exec/chiimp.command +++ b/exec/chiimp.command @@ -18,11 +18,10 @@ if [[ $# -eq 0 ]]; then echo "https://shawhahnlab.github.io/chiimp/GUIDE.pdf" echo else - which pandoc > /dev/null || export RSTUDIO_PANDOC=/Applications/RStudio.app/Contents/MacOS/pandoc/ - if [[ ! "$dir" =~ ^/ ]]; then rel=$(pwd) fi + export RSTUDIO_PANDOC=$(Rscript "$rel/$dir/find_pandoc.R") cd "$cfg_dir" Rscript "$rel/$dir/chiimp" "$@" fi diff --git a/exec/chiimp.sh b/exec/chiimp.sh index 1e9dec9..9a9f5b1 100755 --- a/exec/chiimp.sh +++ b/exec/chiimp.sh @@ -16,7 +16,7 @@ if [[ $# -eq 0 ]]; then echo "https://shawhahnlab.github.io/chiimp/GUIDE.pdf" echo else - which pandoc > /dev/null || export RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc + export RSTUDIO_PANDOC=$(Rscript "$dir/find_pandoc.R") cd "$cfg_dir" Rscript "$dir/chiimp" $* fi diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R new file mode 100755 index 0000000..05d827b --- /dev/null +++ b/exec/find_pandoc.R @@ -0,0 +1,47 @@ +#!/usr/bin/env Rscript + +# Figure out the path to the pandoc executable. In order of priority: +# +# 1. Anything set in RSTUDIO_PANDOC env variable +# 2. Anything found in installed copies of RStudio +# 3. Any other pandoc found on PATH + +find_pandoc <- function() { + # If there's already an environment variable defined, we'll just give that. + pandoc_env <- Sys.getenv("RSTUDIO_PANDOC") + if (pandoc_env != "") { + return(pandoc_env) + } + + # On Windows this should give something like "c:/", but an empty string + # everywhere else. + prefix <- sub("/.*$", "/", normalizePath(".", winslash = "/")) + + # Search for RStudio installations + search_paths <- c( + list.files(list.files(prefix, pattern = "Program Files.*", full.names = TRUE), pattern = "RStudio.*", full.names = TRUE), + list.files("/Applications", pattern = "^RStudio.*.app", full.names = TRUE), + "/usr/lib/rstudio") + + execs <- file.path(strsplit(Sys.getenv("PATH"), ":")[[1]], "pandoc") + execs <- execs[file.exists(execs)] + + # Find files (no dirs) that are named exactly "pandoc", and get the full path + # to the parent dir of any found + pandoc_dirs_available <- c( + dirname(list.files(search_paths, recursive = TRUE, full.names = TRUE, pattern = "^pandoc$")), + dirname(execs)) + + # Let rmarkdown package decide which pandoc to use, if that feature is + # available (>=2.2). It will also implicitly accept a pandoc executable found + # on the PATH. If that function's not available we'll just pick a pandoc. + # https://stackoverflow.com/a/24692264 + pandoc_dir <- if ("find_pandoc" %in% getNamespaceExports("rmarkdown")) { + rmarkdown::find_pandoc(dir = pandoc_dirs_available)$dir + } else { + file.path(pandoc_dirs_available[1], "pandoc") + } + return(pandoc_dir) +} + +cat(find_pandoc(), end = "\n") From e8a04af01e2efb3a2989990a45df80826c21a1bd Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 14:49:02 -0400 Subject: [PATCH 2/8] For #73: circleci: check find_pandoc output --- .circleci/config.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1537072..730fee7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -48,6 +48,9 @@ jobs: - store_artifacts: name: "Store Artifacts: 00install.out" path: chiimp.Rcheck/00install.out + - run: + name: "Run find_pandoc script" + command: Rscript exec/find_pandoc.R - run: name: "Run demo script" command: bash exec/demo.sh $PWD/demo-files From cca3d9194a0d272c521adab960f9d5c85b53850c Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 16:00:24 -0400 Subject: [PATCH 3/8] find_pandoc: fixes for Windows --- exec/find_pandoc.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R index 05d827b..ca654b7 100755 --- a/exec/find_pandoc.R +++ b/exec/find_pandoc.R @@ -23,13 +23,15 @@ find_pandoc <- function() { list.files("/Applications", pattern = "^RStudio.*.app", full.names = TRUE), "/usr/lib/rstudio") - execs <- file.path(strsplit(Sys.getenv("PATH"), ":")[[1]], "pandoc") + execs <- file.path( + strsplit(Sys.getenv("PATH"), .Platform$path.sep)[[1]], + c("pandoc", "pandoc.exe")) execs <- execs[file.exists(execs)] # Find files (no dirs) that are named exactly "pandoc", and get the full path # to the parent dir of any found pandoc_dirs_available <- c( - dirname(list.files(search_paths, recursive = TRUE, full.names = TRUE, pattern = "^pandoc$")), + dirname(list.files(search_paths, recursive = TRUE, full.names = TRUE, pattern = "^pandoc(?:\\.exe)$")), dirname(execs)) # Let rmarkdown package decide which pandoc to use, if that feature is From d49e7d4dffb36fcdf3a0f064af78a3e4e86b6d8e Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 16:23:07 -0400 Subject: [PATCH 4/8] fix whitespace in pandoc-finding on Windows --- exec/chiimp.cmd | 10 +++++++++- exec/find_pandoc.R | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/exec/chiimp.cmd b/exec/chiimp.cmd index 343e8f5..29f460c 100755 --- a/exec/chiimp.cmd +++ b/exec/chiimp.cmd @@ -15,7 +15,15 @@ set rpath=%rpath: "%1"=% set rpath=%rpath:"=% set rdir=%rpath%\..\ -for /f "tokens=* usebackq" %%x in (`%rdir%\Rscript %dir%\find_pandoc.R`) do set RSTUDIO_PANDOC=%%x +REM Figure out path to pandoc. +REM Haven't bothered to figure out what combinations of options to for /f are +REM quite right between this command and the one for rpath above. If anyone +REM reading this understands the insanity of Microsoft's syntax here please go +REM ahead and make a pull request to clean this up. +REM Also note that spaces are OK in the Rscript path but not the path to the +REM script; for some reason that makes space-handling for the whole command +REM fail. +for /f "tokens=* usebackq" %%x in (`"%rdir%\Rscript" %dir%\find_pandoc.R`) do set RSTUDIO_PANDOC=%%x if "%~1"=="" ( echo.To run CHIIMP, drag and drop a configuration file onto this icon. diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R index ca654b7..c605615 100755 --- a/exec/find_pandoc.R +++ b/exec/find_pandoc.R @@ -46,4 +46,6 @@ find_pandoc <- function() { return(pandoc_dir) } -cat(find_pandoc(), end = "\n") +# Leaving off a newline for windows because chiimp.cmd mangles that to a space +# and then keeps it on the end of the file path. +cat(find_pandoc(), end = if (.Platform$OS.type == "windows") "" else "\n") From 3f82aa5c24adda0480f24bd1f91786651e9a8f88 Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 17:15:28 -0400 Subject: [PATCH 5/8] more Windows whitespace fix attempts --- exec/chiimp.cmd | 4 ++++ exec/find_pandoc.R | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/exec/chiimp.cmd b/exec/chiimp.cmd index 29f460c..34915f4 100755 --- a/exec/chiimp.cmd +++ b/exec/chiimp.cmd @@ -24,6 +24,10 @@ REM Also note that spaces are OK in the Rscript path but not the path to the REM script; for some reason that makes space-handling for the whole command REM fail. for /f "tokens=* usebackq" %%x in (`"%rdir%\Rscript" %dir%\find_pandoc.R`) do set RSTUDIO_PANDOC=%%x +REM Removing the trailing space on the end of the variable I can't figure out +REM how to avoid +REM https://stackoverflow.com/a/29504225 +set RSTUDIO_PANDOC=%RSTUDIO_PANDOC:~0,-1% if "%~1"=="" ( echo.To run CHIIMP, drag and drop a configuration file onto this icon. diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R index c605615..ca654b7 100755 --- a/exec/find_pandoc.R +++ b/exec/find_pandoc.R @@ -46,6 +46,4 @@ find_pandoc <- function() { return(pandoc_dir) } -# Leaving off a newline for windows because chiimp.cmd mangles that to a space -# and then keeps it on the end of the file path. -cat(find_pandoc(), end = if (.Platform$OS.type == "windows") "" else "\n") +cat(find_pandoc(), end = "\n") From 87bf580aa746c368a459caa1e79339a086c69f58 Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 17:57:52 -0400 Subject: [PATCH 6/8] fix pandoc-finding when not .exe The optional capture group doesn't seem to work if the .exe version is not found. Instead we'll just use the two patterns side-by-side. --- exec/find_pandoc.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R index ca654b7..b5e428e 100755 --- a/exec/find_pandoc.R +++ b/exec/find_pandoc.R @@ -1,6 +1,7 @@ #!/usr/bin/env Rscript -# Figure out the path to the pandoc executable. In order of priority: +# Figure out the path to the pandoc executable's parent directory. In order of +# priority: # # 1. Anything set in RSTUDIO_PANDOC env variable # 2. Anything found in installed copies of RStudio @@ -28,10 +29,10 @@ find_pandoc <- function() { c("pandoc", "pandoc.exe")) execs <- execs[file.exists(execs)] - # Find files (no dirs) that are named exactly "pandoc", and get the full path - # to the parent dir of any found + # Find files (no dirs) that are named exactly "pandoc" or "pandoc.exe", and + # get the full path to the parent dir of any found pandoc_dirs_available <- c( - dirname(list.files(search_paths, recursive = TRUE, full.names = TRUE, pattern = "^pandoc(?:\\.exe)$")), + dirname(list.files(search_paths, recursive = TRUE, full.names = TRUE, pattern = "^(pandoc|pandoc\\.exe)$")), dirname(execs)) # Let rmarkdown package decide which pandoc to use, if that feature is @@ -41,7 +42,7 @@ find_pandoc <- function() { pandoc_dir <- if ("find_pandoc" %in% getNamespaceExports("rmarkdown")) { rmarkdown::find_pandoc(dir = pandoc_dirs_available)$dir } else { - file.path(pandoc_dirs_available[1], "pandoc") + pandoc_dirs_available[1] } return(pandoc_dir) } From 6fde3432e09185bcd33c9769e3318dfb899b809c Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 18:22:08 -0400 Subject: [PATCH 7/8] finally fix trailing space It was R, not windows! I need to learn cat() better. --- exec/chiimp.cmd | 4 ---- exec/find_pandoc.R | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/exec/chiimp.cmd b/exec/chiimp.cmd index 34915f4..29f460c 100755 --- a/exec/chiimp.cmd +++ b/exec/chiimp.cmd @@ -24,10 +24,6 @@ REM Also note that spaces are OK in the Rscript path but not the path to the REM script; for some reason that makes space-handling for the whole command REM fail. for /f "tokens=* usebackq" %%x in (`"%rdir%\Rscript" %dir%\find_pandoc.R`) do set RSTUDIO_PANDOC=%%x -REM Removing the trailing space on the end of the variable I can't figure out -REM how to avoid -REM https://stackoverflow.com/a/29504225 -set RSTUDIO_PANDOC=%RSTUDIO_PANDOC:~0,-1% if "%~1"=="" ( echo.To run CHIIMP, drag and drop a configuration file onto this icon. diff --git a/exec/find_pandoc.R b/exec/find_pandoc.R index b5e428e..b6e757d 100755 --- a/exec/find_pandoc.R +++ b/exec/find_pandoc.R @@ -47,4 +47,4 @@ find_pandoc <- function() { return(pandoc_dir) } -cat(find_pandoc(), end = "\n") +cat(find_pandoc(), "\n", sep = "") From 831696cee81822ad8087a7ae67842651779805ac Mon Sep 17 00:00:00 2001 From: Jesse Connell Date: Thu, 21 Apr 2022 18:38:34 -0400 Subject: [PATCH 8/8] For #75: NEWS [skip ci] --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9fe0601..b29a8cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # chiimp dev + * Fixed pandoc error with newer RStudio versions (>= 2022.02) by finding + pandoc automatically ([#75]) * Made `load_csv` use custom row names if given ([#74]) * Made installer obey site-wide R configuration if present ([#72]) * Fixed superfluous quotes in report HTML file when rendered with newer pandoc @@ -12,6 +14,7 @@ ([#63]) * Fixed handling of extra pheatmap arguments in `plot_dist_mat` ([#62]) +[#75]: https://github.com/ShawHahnLab/chiimp/pull/75 [#74]: https://github.com/ShawHahnLab/chiimp/pull/74 [#72]: https://github.com/ShawHahnLab/chiimp/pull/72 [#69]: https://github.com/ShawHahnLab/chiimp/pull/69