I'm trying to make available a shiny app for some people at work but I don't want to change the working directory manually for everyone. I had plan to use the rstudioapi
function getActiveDocumentContext()
but it only works when the app is lunch from rstudio and i'm using R console because the app is deployed with a .bat file (describe in this page http://rstudio-pubs-static.s3.amazonaws.com/3269_a6682dfda37e411fb5e0e6699495cdc4.html). I tried a bunch of the answers here (Rscript: Determine path of the executing script) but neither work, and most of them I don't understand so I was not able to "fix theme".
Asked
Active
Viewed 1,480 times
0

Alejandro Andrade
- 2,196
- 21
- 40
-
Are you guys using same LAN?? If so, then you can share direct URL of the shiny app. – Sourabh Sep 04 '18 at 09:31
-
we are not it is personal computers for each person – Alejandro Andrade Sep 04 '18 at 14:37
1 Answers
2
As your linked SO question indicates, there are many solutions, my favorite is using rprojroot
(I think it is probably the easiest). Using the simply shiny test_app example, you need to have this in your run.R:
library(shiny)
library(rprojroot)
folder_address = dirname(thisfile())
runApp(folder_address, launch.browser=TRUE)
I tested it on a Mac with the start script (test.command
) below, and it works wherever you have the test_app folder:
#! /bin/bash
PWD="`dirname \"$0\"`"
cd "${PWD}"
Rscript "run.R"
On a Windows computer, you'll need to specify the path to Rscript.exe (or R.exe) in your test.bat
:
"C:\Program Files\R\R-3.5.1\bin\Rscript.exe" "run.R"

LmW.
- 1,364
- 9
- 16
-
I tried it but i keep getting the following error: `Error in thisfile() : could not find function "thisfile"`. I installed the package `kimisc` which seems to be the one that has that function but then I got a lot of error messages saying I should use `rprojroot::` but it doesn't work. I don't know how relevant this is but i´m using R version 3.4.2 but the answer shouldn't depend on the R version. – Alejandro Andrade Sep 04 '18 at 14:48
-
@AlejandroAndrade Install the package `rprojroot` instead and I think version 3.4.2 should work. – LmW. Sep 04 '18 at 19:49
-
I also installed that package and tried using the command you posted but it didn't work I got the error in the comment above thats why I tried with the package `kimisc` which then said that i should be using the package `rprojroot` – Alejandro Andrade Sep 05 '18 at 19:10
-
-
obvious. What version of the package do you have because when I run `rprojroot::thisfile()` I get `Error: 'thisfile' is not an exported object from 'namespace:rprojroot'` which means its not a primary function of the package. Also searching in the package help page theres no `thisfile()` function describe. – Alejandro Andrade Sep 05 '18 at 21:04
-
That's odd. Works for me: `> packageVersion("rprojroot")` [1] ‘1.3.2’ `> rprojroot::thisfile()` NULL – LmW. Sep 05 '18 at 21:49
-
An alternative is to copy the `thisfile_rscript` function from `thisfile.R` in the `rprojroot` package to your own script, and then use `thisfile_rscript()` in the place of `thisfile()` in your script: https://github.com/r-lib/rprojroot/blob/master/R/thisfile.R – LmW. Sep 05 '18 at 21:54
-
I just copied that function and the result is `NULL` so it doesn't work in your answer – Alejandro Andrade Sep 05 '18 at 22:51
-
The result should be NULL when you call it in a R console. Put this line in your R script `print(thisfile_rscript())`, save and run it from a command line terminal: `Rscript run.R` – LmW. Sep 05 '18 at 22:57
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179500/discussion-between-lmw-and-alejandro-andrade). – LmW. Sep 05 '18 at 23:02