Manage paths when deploying R Shiny? - r

I need some advice on how to take a working app from your local machine onto a web deployment.
I tried deploying an App to Shinyserver.io, but I have path errors. It cannot find my utilities code in utils-fun.R.
Error message
The application failed to start.
Error in eval(expr, envir, enclos) :
could not find function "GetSettings"
For example: my server.R is in an App directory
library(shiny)
code...
source("../code/utils-fun.R")
... rest of code
How do you help RShiny know what it needs to take with when it is deploying?
Should the structure of your directories rather be more like this.
-Root or App directory
ui.R
Server.R
-- code (as subdirectory where my functions are)
-- data (rds and data files)
With everything in one directory, underneath the ui.R /server.R files?
I see from using-source-in-shiny that I need to add local = TRUE to my source but is that all you need?
Thanks I would appreciate any sage advice of how you implement R Shiny.

For tidiness I keep my source files in a folder called "files" alongside ui.r and server.r. Since the working directory for a shiny app is the folder where ui.r and server.r are kept you can use source("files/script.r").

Related

Why cannot I call the app when I save the R script as app.R into the working directory?

First of all I set my working directory as following
setwd("C:/Users/asus/Desktop/App-1")
Then I can call the app with the following code:
runApp("C:/Users/asus/Desktop/App-1")
But I cannot call the app via :
runApp("App-1")
The script in the directory is named as "app.R" but it appears in the directory as "app".
Whereas, in the R webpage it says if I create a new directory named App-1 it should have worked. But It does not. Many thanks.

How to use 'shinytest' with 'golem'?

I'm new to 'golem'. So far I did my Shiny apps as packages, with the app in the folder inst/app, and then to use 'shinytest' I made a folder inst/app/shinytest. How to do with 'golem'? There's no app folder: inst/app only contains www, and the UI and the server are some functions in the package.
So I need to know where to put the 'shinytest' script and how to deal with 'testthat' and the expect_pass function? (I don't want to use testServer)
I've found. It suffices to create a file app.R in the app folder with these contents:
mypackage::run_app()

Deploy app.R in a server without index.html

I'd like to access a Shiny app, app.R via web. In which directory does the app.R need to be stored?
Shiny's default sample apps have their own directories, e.g., /srv/shiny-server/sample-apps/hello. Inside the directory are ui.R and server.R files. The app is linked in index.html with <iframe src="./sample-apps/hello/". I deleted ui.R and server.R files in hello/ directory and place my app.R file there. But I'm now getting error.
Is it possible to run the app without an index.html file? If index.html is must, how do I link the app? I'm perfectly file RStudio's output when app.R is run. No fancy layout is necessary.
For normal webservers: one can't just place the .R file on a server, one actually has to execute it using R. When using a webserver with other content, you can then use proxying (ProxyPass in Apache) to deliver contents, and possibly embed it.
For shiny-server, I'm not sure. It seems to follow the server.R / ui.R pattern. an existing app.R could be refactored like this: ssume you have an app.R with something();shinyApp(ui = verticalLayout(...), server=function(input, output){...}), you could refactor into a server.R with something(); server <- function(input, output){...} and a ui.R with ui <- verficalLayout(...). For the sake of clarity, I put the server / ui assignment on the very bottom of the file.

R shiny: Retrieve home directory of a shiny app

Is there a function or a way to retrieve the path of root home directory of a shiny app?
The shiny app I run creates multiple directories and changes working directory. But at point in the app, I need to jump to the home directory. getwd() won't work unless I run it first and save it as a variable or something.
I am thinking of something like the below command used to read a file from a package home directory on any platform without knowing the actual path.
#get root dir
system.file()
# read a file from the root dir of a package
a <- system.file("bla",package="foo")
The approach has to work locally, on shiny-server and shinyapps.io. (In situations where I cannot set the path manually).

Why my app doesn't deploy on shinyapps.io?

I followed the same commands shown in shinyapps.io for uploading my app but I get the following error:
> library(shinyapps)
> shinyapps::deployApp("/Users/mona/CS764/demo")
Error in lint(appDir) : Cancelling deployment: invalid project layout.
The project should have one of the following layouts:
1. 'shiny.R' and 'ui.R' in the application base directory,
2. 'shiny.R' and 'www/index.html' in the application base directory,
3. An R Markdown (.Rmd) document.
Here's the structure of my files:
When using deployApp() you must deploy the directory containing your ui.R and server.R files (or shiny.R / www/index.html if using a custom UI). The files can not be a subdirectory.
Additionally, its important to note that any R files in the directory or any subdirectory will be parsed, so its important that they have valid syntax.

Resources