yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.37k stars 874 forks source link

Allow dots in file names in .rnw files #2213

Closed otoomet closed 1 year ago

otoomet commented 1 year ago

I prefer to use automatically generated figures in .figs folder to make the folder invisible and stress it is autogenerated (I also use .cache for the caches). However, knitr changes the leading dot to an underscire when working for .rnw (latex) files (see also #1471). This post here is both bug report and feature request:

A minimal example (file "test.rnw"):

\documentclass{article}
\usepackage{graphicx}
<<>>=
knitr::opts_chunk$set(fig.path=".figs/")
@ 
\begin{document}
<<>>=
plot(10:1)
@
\end{document}

and this can be compiled with:

Rscript -e "knitr::knit('test.rnw')"

Knitr (correctly) gives a message about replacing dots with underscores.

Session info:

$ Rscript -e "xfun::session_info('knitr')"
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Locale:
  LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
  LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
  LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
  LC_PAPER=en_US.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

Package version:
  evaluate_0.16   graphics_4.2.2  grDevices_4.2.2 highr_0.9      
  knitr_1.42      methods_4.2.2   stats_4.2.2     tools_4.2.2    
  utils_4.2.2     xfun_0.36       yaml_2.3.5   

The bug part

This feature (changing dots to underscores) is not documented. Neither ?knitr::opts_chunk nor the website it refers to currently mention sanitization, the website currently states that:

fig.path: ('figure/'; character) A prefix to be used to generate figure file paths. fig.path and chunk labels are concatenated to generate the full paths. It may contain a directory like figure/prefix-; the directory will be created if it does not exist.

As a note, dots are allowed in .rmd/markdown related paths, this creates confusion as why it sometimes works and sometimes does not work.

Hence my request to document the behavior.

The feature request part.

It seems that modern latex supports dots in file names. For instance, a file

\documentclass{article}
\usepackage{graphicx}
<<>>=
knitr::opts_chunk$set(fig.path=".figs/")
@ 
\begin{document}
<<>>=
plot(10:1)
@
% note: not a self-contained example, you need the the 'picture.pdf' below
% note: path contains a leading dot!
\includegraphics{.figs/picture.pdf}
\end{document}

compiles correctly using ubuntu 20.04 (pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) and XeTeX 3.14159265-2.6-0.999991 (TeX Live 2019/Debian))

Alternatively, if allowing dots globally is considered not a good idea, then can you make it optional using some sort of sanitize=TRUE/FALSE option?

Thank you for considering :-)


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 1 year ago

Thanks for the detailed report! Would it meet your demand if I allow leading dots? Then .figs/ will be respected, but .figs/foo.bar will still be changed to .figs/foo_bar.

can you make it optional using some sort of sanitize=TRUE/FALSE option?

That sounds like a good idea. Yes, I think I can do it.

otoomet commented 1 year ago

Thanks! Leading dot is all I need (at least in the foreseeable future :-) )

If I were the designing the package--I'd probably leave the dots alone, and just tell the user that they have to figure out what file names are legal given the software/OS they use.

But anyway, appreciate your work in what seems like a quite big project!

yihui commented 1 year ago

Done. Dots are left untouched now.

To prevent other special characters from being sanitized, you can set options(knitr.sanitize.paths = FALSE).

Thanks!

github-actions[bot] commented 11 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.