Include .R Files in Shiny Dashboard - r

I am now the only responsible for the R shiny Dashboard at work and have got a question.
I know that the ui.R , global.R and Server.R are started automatically from the Server. Now I can see a production.R file, with a function "doSomething()" used in the Server.R file, but the production.R file isn't included or called.
If I move an .R file into the Server Directory, would I be able to access to its functions and variables automatically? In my eyes it has to be so, because everything works fine and the function from the production.R is really used.
Greetings an thanks :-)

Yes, every .R file in the same directory as the ui.R and server.R should be called when running Shiny.
You might have to source the file in your server.R script.

Related

Calling global.R in an app.R file on shiny server; unable to call dataframes in for global use

I am developing a shiny app which is an exploratory data analysis tool. At this point, I am trying to optimise so that data is loaded in once but is readily available for all users. I am aware of the scoping materials on this i.e. Scoping I have the code to load the data in for the app within a global.R file like so:-
#global.R file
library(feather)
dataframe1<-read_feather("df1.feather")
dataframe2<-read_feather("df2.feather")
These are the dataframes that I need to call once and make available to all users of the app. However, I am calling the ui and the server code all within app.R and I have a hunch that this could be problematic.
I have tried a few implementations as guided by what I have been able to see on the web but it is obvious that I am not doing something properly. Here are some examples of what I have tried
source('global.R', local = T)
ui<-fluidPage({
#code for ui
})
server<-function(input,output,server){
# code for server
}
shinyApp(ui,server)
Where the outcome is this:-
The second thing I have tried is having the global file called in onStart parameter in the shinyApp(), but I have the same error.
The global file is saved in the GitLab repo that I am using to deploy this app on shiny server. The app.R script Can any one show me how I can correctly call the global.R file, so that the data is loaded in once and made available to all users of the app? Do I need to split the app.R file into a ui.R and a server.R file?

global.R don't start

I'm wondering why global.R doesn't start when i launch my app from Rstudio.
It seems, of what i understood, that global.R must be run once as i launch my app, but instead Rstudio give me an error that it could not find the function that i have defined in the global.R file.
After running global.R by hand with ctl+alt+r then i can launch my app and it recognize the function and works well all during the time i use the R session. I'm not publishing on the web but works just in local.
Do i missed something?
Thanks.
(R version 3.5.2 and shiny 1.2)
The content of global.R is usable only if the shiny app is made of server.R and ui.R. The content of global.R is ignored if the app is made of a unique app.R file.
Splitting your app.R in server.R and ui.R will
resolve your issue.
Since the OP asked about why global.R is not read when the architecture relies only a single app.R, it is because of how the app is started.
If it is started via:
shinyApp(ui = ui, server = server)
Then the application is considering these two functions without running anything beforehand, i.e. neglecting global.R contents. There is, however, the onStart parameter that can be supplied to shinyApp to run something before the app starts, and its description resolves a great deal of confusion:
onStart: A function that will be called before the app is actually run. This is only needed for shinyAppObj, since in the shinyAppDir case, a global.R file can be used for this purpose.
It looks to me that when the application is split into server.R and ui.R, global.R is automatically included by means of running the app through shinyAppDir.
The solutions I found in case one wants to strictly use a single app.R code file and perform some routines prior to starting the app are:
Tinker with onStart parameter inside shinyApp call
Source global.R as #tic-toc-choc points out

How to find the name of the server you are on in Shiny

When I develop a shiny package locally I pull from local sources and when I upload the ui.r and server.r files I need to change the paths of things.
Is there a way I can find out which server I am on in shiny?
That way I can use an if statement to choose between paths and I don't need to manually change stuff when I upload.
Thank you.

how to deploy shiny app that uses local data

I'm deploying my shiny app and I don't know how to input my a local dataset. I keep getting Error: object "data" not found. Here is my path to shiny folder.
library(shinyapps)
shinyapps::deployApp('C:\\Users\\Jeremy\\Desktop\\jerm2')
In this directory (jerm2), I have 3 things: ui.R, server.R, and my local dataset, a .csv called proj.csv.
In the server.R file,
I set data<-read.csv("proj.csv")
I just don't know how to get Shiny to pick up my datasets.
You may want to add a subdirectory in your shiny folder called "Data" and put proj.csv there.
Then, in your server.r put:
data<-read.csv("./Data/proj.csv")
That will make it clear where the data is when the app is deployed to the ShinyApps service.
I ran into this same problem. It turned out that I did not have my working directory pointing to my shiny app at the time I used shiny.io to save and deploy my app.
Be sure that if you're loading the data that the code reflects that your shiny app is the working directory.
Otherwise you will get a log error that looks something like this
cannot open compressed file 'C:/Users/Joseph/Documents/data/data.rda', probable reason 'No such file or directory'
What I did was to write the csv under a sub folder (i.e. data/) of the shiny app directory and then added data<-read.csv("/Data/proj.csv") in server.r (as indicated in the answer). I didn't put the dot and it works.
Another thing is, when you publish it, don't forget to publish both the shiny app and the file in the shiny app folder.

Uploading csv file to shinyApps.io

My app runs fine locally and I am able to successfully deploy my app to the shinyapps.io server, but I get the following error message when I try and load the app in my browser using the shinyapps URL: "Error object 'data' not found.' I think this is because the 'data' variable reads from a csv file on my local directory. Is there a way I can upload this csv file to the shinyapps server? I've tried looking this up but I've found nothing.
Here's the code I am using to read in the files. I'm getting the file from the same working directory as my server.R and ui.R. Thanks
server.R
library(shiny)
college = read.csv("college.csv")
ui.R (I added to this to see if it fixes the problem, but it doesn't)
library(shiny)
college = read.csv("college.csv")
Currently I was facing a similar trouble.
Reading here and there, I realized that you can create a script called global.R in the same dir with ui.R and server.R.
On this file (global.R) you can load libraries and, in this case, objects previously saved on a dir, for example, called data.
I created the object and the saved it with saveRDS(df, "./data/df.RDS"). Then loaded it from the data dir with something like
df <- readRDS("data/df.RDS")
on the global.R
That works for me.
Best practice would be to place your data in a folder, say ~/<application name>/data and then call your data from your server.R treating your application's directory (/<application name>/) as the current working directory.
e.g. I save my files as RDS objects in ~/ImputationApp/data/ and then read them in with:
foo.rds <- readRDS("data/foo.rds")
Even though what you describe should run, double check your filepaths for the datafiles you are trying to load and any stray setwd() commands that could be mucking up the works. A common misstep is to put the fully qualified path to your data on your machine in your server.R.
I know it's too late but I believe creating a folder named www in your directory and placing the csv there should solve the problem.

Resources