Skip to content

Make script path available to R #122

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

Merged
merged 3 commits into from
Jan 17, 2025

Conversation

Image for: Conversation
Copy link
Contributor

It can be helpful to know the path to the script being executed via a shebang line. This can be determined using Rscript via the --file= argument, although it's a little clunky:

$ cat Rscript.R
#! /usr/bin/env Rscript
print(commandArgs(FALSE))

$ ./Rscript.R
[1] "/opt/homebrew/Cellar/r/4.4.2_1/lib/R/bin/exec/R"
[2] "--no-echo"                                      
[3] "--no-restore"                                   
[4] "--file=./Rscript.R"

But, as far as I can tell from the source and some experimentation, r doesn't pass this information through argv, commandArgs() or the environment. This is a simple patch to set an environment variable containing the path, which should be unconditionally available by this point in the execution. The script path is then readily available:

$ cat littler.r 
#! /usr/bin/env lr
print(argv)
print(commandArgs(FALSE))
print(Sys.getenv("LITTLER_SCRIPT_PATH"))

$ ./littler.r 
NULL
[1] "littler"       "--gui=none"    "--no-restore"  "--no-save"    
[5] "--no-readline" "--silent"      "--slave"      
[1] "./littler.r"

I'm happy to change the name of the variable or refactor if there's a better way to do this (or be told that there is already a mechanism for it!), but since it's a small addition I thought I'd just propose something.

Copy link
Owner

Hm, sure, if you really think you need it. It should not create too much trouble. Can we consider making it an opt-in? These littler reads one or two .rc files where you could flag it.

Also, should we not call a path expansion on ./littler.r to have . replace with the current working dir?

Copy link
Owner

It's a little odd that the env var would be 'there' only in the script case but then again we do have the piped and expression cases and it is likely of less value there.

Copy link
Contributor Author

I'm not sure what the variable would usefully contain in the pipe and expression cases. Path expansion isn't strictly required in my case but is worth having; I'll look into that and making the whole thing opt-in.

Copy link
Owner

Yes, maybe all we need is

  • a ChangeLog entry
  • a sensible line or two of documentation in the man page

and we may be good to go.

setenv("LITTLER_SCRIPT_PATH", script_path, 1);
else
setenv("LITTLER_SCRIPT_PATH", argv[optind], 1);
#else
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I had not realized the extra work with realpath() here but well done! That is likely more useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realpath is probably pretty universally available, but R checks for it so I thought I should play it safe. I'll add to the docs and changelog shortly (doing this between other stuff). Making it optional I'm not sure about - it would need a flag or an env var, I guess, neither of which feels ideal. I would hope it can be easily ignored where it's not helpful.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really appreciate the clean configure.ac work here.

Let's drop the optional. We guess we can bear the cost of setting an env.var.

Copy link
Owner

Looks good. I may shorten the ChangeLog a wee bit (max of two lines per entry is a goal, remove two empty lines to group but overall this is ready to go.

eddelbuettel merged commit 63dd230 into eddelbuettel:master Jan 17, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants