aaarg, sang too quickly. I saved the script by @jake
library(kimisc)
library(magrittr)
library(stringr)
root_dir <- thisfile() %>%
str_replace(pattern = "/code/test.R", replacement = "")
print(root_dir)
into a file called test.R
, and tried to run it, using Rscript
, but the output was just
"test.R"
which fair enough if you consider the documentation. So one of the ways referenced in the documentation to get the full path is to source the file, so this will work:
R cmd -e "source('test.R')"
R version 3.4.2 (2017-09-28) -- "Short Summer"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> source('test.R')
[1] "/srv/shiny-server/XXXX/test.R"
But of course, it only works if you are already in the working directory!!
Which you could express explicitly... but...
Maybe if(Sys.info()["nodename"]
... is as good as it gets? Or maybe setting the variable from linux. In fact, I'm writing a blog post about how to accomplish this using linux services... and there you can set up the path explicitly, which at least helps the automated run scenario... and perhaps for all other cases, here
is enough?