Using here::here() or similar with shiny

Hi all,

I have noticed that both the "here" and "rprojroot" packages seem to be designed only for interactive use.

What are people using solve similar use cases for shiny apps?

Thanks!

Rob

Hi @schuffr,

I have been using here to deploy apps and reports on a RStudio Connect server and it works without problem - if there is a project file or in fact any of these in you app/report folder:

  • contains a file .here
  • contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
  • contains a file DESCRIPTION with contents matching ^Package:
  • contains a file remake.yml
  • contains a file .projectile
  • contains a directory .git
  • contains a directory .svn

then here figures out where things are even when you deploy outside your development environment.

1 Like

Hi @valeri, thank you for the reply. For some reason I am not able to get here to work as I expect it to with a shiny app. When I add the following lines to the default shiny app created by rstudio

library(shiny)
# add these 4 lines after library(shiny)
library(here)
here::set_here()
print(getwd())
here::dr_here()

Interactively I get this result:

> runApp()
Created file .here in /Users/schuffr/Documents/Work/RStudio/OCTRI/CRIReporting/r/testing_here
[1] "/Users/schuffr/Documents/Work/RStudio/OCTRI/CRIReporting/r/testing_here"
here() starts at /Users/schuffr/Documents/Work/RStudio/OCTRI/CRIReporting, because it contains a file matching `[.]Rproj$` with contents matching `^Version: ` in the first line

notice that is seems .here file is not detected even though I manually set the working directory to the "testing_here" directory first.

Either I am doing something wrong, or maybe here doesn't look in the current directory first for the .here file ... or maybe something else.

Hi @schuffr,

interesting indeed. When I went to read the documentation of here, this is what it says (highlighting mine)

EDIT: It seems like it actually matters what was your working directory at the load time of the here package.

Project Root

Starting with the current working directory during package load time, here will walk the directory hierarchy upwards until it finds a directory that satisfies at least one of the following conditions:

  • contains a file .here
  • contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
  • contains a file DESCRIPTION with contents matching ^Package:
  • contains a file remake.yml
  • contains a file .projectile
  • contains a directory .git
  • contains a directory .svn

Notice the word "upwards". Seems that in your case here finds a project file .Rproj in /Users/schuffr/Documents/Work/RStudio/OCTRI/CRIReporting and stops there as it found one of the files it is searching to determine your project location - so it never gets to /Users/schuffr/Documents/Work/RStudio/OCTRI/CRIReporting/r/testing_here where your .here file is. At least that is what I think happens. So ... typically your R project should be set up to be in the same folder where your main app files are and this wouldn't conflict as in this case. Just to test if that is correct you could rename your Rproj file to something like Rproj_ and see if here will behave differently.

So after further testing it seems to be the case that if there is an rproj file in the same directory as app.R here works as expected. But in the absence of an rproj file in the same directory as app.R the presence of a .here file seems to be ignored even if the initial working directory is set in rstudio to a sub directory of the directory containing app.R.

This is rather unexpected it seems and I wonder if it's a bug that I should report.

For what it's worth, I can just mention that since we use git in my organisation and I am trying to keep my projects in non-nested locations, I never had to manually create a .here file, since here will find the .git folder and/or the project file (I don't always have my projects as R projects necessarily) in the right place.