Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file.path("", "../path/file") and intermediates_dir make Rmarkdown create <intermediates_dir>/../path/file #2538

Open
5 tasks
ljw20180420 opened this issue Feb 1, 2024 · 2 comments
Labels
reprex needs a minimal reproducible example

Comments

@ljw20180420
Copy link

Checklist

When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:

  • formatted your issue so it is easier for us to read?

  • included a minimal, self-contained, and reproducible example?

  • pasted the output from xfun::session_info('rmarkdown') in your issue?

  • upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?

  • installed and tested your bug with the development version of the rmarkdown package using remotes::install_github("rstudio/rmarkdown")?

My rmd file is

pythonpath <- file.path("", "../py312/bin/python3.12")
rmarkdown::render(input = "rmdbug.Rmd", intermediates_dir = "/home/user")

When I excute it, R report the error:
Warning messages:
1: In dir.create(dirname(dest), recursive = TRUE) :
cannot create dir '/home/user/../py312', reason 'Permission denied'
2: In file.create(to[okay]) :
cannot create file '/home/user/../py312/bin/python3.12', reason 'No such file or directory'

Seems that use "../" ("../py312/bin/python3.12") in file.path with intermediates_dir confuse Rmarkdown.
This error disappears when I remove the option intermediates_dir = "/home/user" or when I remove "../" from "../py312/bin/python3.12" in file.path

@cderv
Copy link
Collaborator

cderv commented Feb 1, 2024

To clarify the context, why do you need to set the intermediates_dir to a different folder using absolute path ?

This is not an expected usage and I would like to understand it better.

Playing with path in rmarkdown is known to have side effect and usually is discouraged. It is there for advanced usage in specific case.

pythonpath <- file.path("", "../py312/bin/python3.12")

What is it used for example? Is this something used inside your rmdbug.Rmd?

My guess is that it is used somehow in your process, and it will be made relative to intermediates dir.

This means that at some point the error will be

/home/user/../py312

because you don't have the access right to create a new py312 folder in /home/user.

But I don't know why this is trying to create a directory - as I don't know what pythonpath is used for.

Anyhow, please to clarify the usage you have, and share a reproducible example. intermediates_dir is a directory where resources and computes will happen, so it needs to be writable.

Thanks !

@cderv cderv added the reprex needs a minimal reproducible example label Feb 1, 2024
@ljw20180420
Copy link
Author

Thank you

To clarify the context, why do you need to set the intermediates_dir to a different folder using absolute path ?

This is not an expected usage and I would like to understand it better.

Playing with path in rmarkdown is known to have side effect and usually is discouraged. It is there for advanced usage in specific case.

pythonpath <- file.path("", "../py312/bin/python3.12")

What is it used for example? Is this something used inside your rmdbug.Rmd?

My guess is that it is used somehow in your process, and it will be made relative to intermediates dir.

This means that at some point the error will be

/home/user/../py312

because you don't have the access right to create a new py312 folder in /home/user.

But I don't know why this is trying to create a directory - as I don't know what pythonpath is used for.

Anyhow, please to clarify the usage you have, and share a reproducible example. intermediates_dir is a directory where resources and computes will happen, so it needs to be writable.

Thanks !

Thank you, sir. I need the pythonpath to be used in reticulate::use_python(python = pythonpath) to call python in my Rmarkdown. But reticulate should be irrelavant to this probem because the error appears with only the two lines of code here.

pythonpath <- file.path("", "../py312/bin/python3.12")
rmarkdown::render(input = "rmdbug.Rmd", intermediates_dir = "/home/user")

By the way, I originally use this.path() package and dirname() of R to get the path of my Rmd file, say /home/user/project/tools/run.Rmd, and then I use

pythonpath <- file.path("/home/user/project/tools", "../py312/bin/python3.12")

I use "../py312/bin/python3.12" with the prefix "../" because run.Rmd is in a directory (tools) which has the same parent directory as that of py312. In other words, the full path of python3.12 is /home/user/project/py312/bin/python3.12. Then I run

rmarkdown::render(input = "run.Rmd", intermediates_dir = "/home/user/project/test")

Here, /home/user/project/test contains my data files. I want to keep all intermediate files in the sam folder as my datafile to avoid permission issues for multi-user cases. Then I find that rmarkdown::render and file.path will do some evil things to intermediates_dir/../py312/bin/python3.12. After the rendering is done, my python excutable "intermediates_dir/../py312/bin/python3.12" will be deleted. Then I try to change intermediates_dir to "/home/user", I got the following error

Warning messages:
1: In dir.create(dirname(dest), recursive = TRUE) :
cannot create dir '/home/user/../py312', reason 'Permission denied'
2: In file.create(to[okay]) :
cannot create file '/home/user/../py312/bin/python3.12', reason 'No such file or directory'

So I guess rmarkdown::render and file.path tries to create a file, which is determined by the second parameter in file.path("/home/user/project/tools/run.Rmd", "../py312/bin/python3.12"). That is "../py312/bin/python3.12". Since the intermediates_dir = "/home/user", rmarkdown::render tries to create "/home/user/../py312/bin/python3.12". If intermediates_dir = "/home/user/project/test", this will be "/home/user/project/test/../py312/bin/python3.12". So my python excutable /home/user/project/py312/bin/python3.12 is replaced by the newly created python3.12. When rmarkdown ::render begins to clean intermediate files, it find that /home/user/project/py312/bin/python3.12 is created by itself, so it deletes python3.12.

Then I try to replace file.path("/home/user/project/tools", "../py312/bin/python3.12") by file.path("", "../py312/bin/python3.12"), the error continues. This should exclude the problem of this.path() anddirname. Then I try to replace file.path("", "../py312/bin/python3.12") by file.path("", "py312/bin/python3.12"). This time, the error disappears. So the minimal code to cause problem is just the above two lines

pythonpath <- file.path("", "../py312/bin/python3.12")
rmarkdown::render(input = "rmdbug.Rmd", intermediates_dir = "/home/user")

In my project, I have avoid to use file.path. But rmarkdown::render with intermediates_dir and file.path("", "../py312/bin/python3.12") would try to create a file. I think this should be something unexpected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reprex needs a minimal reproducible example
Projects
None yet
Development

No branches or pull requests

2 participants