Uploading csv file to shinyApps.io - r

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.

Related

Include .R Files in Shiny Dashboard

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.

r opencpu root dir for reading csv

I have opencpu (single server) up and functioning. My first function will open a dataset from a csv file stored on my hard drive.
Where should I deploy the csv file? (I tried my apps www directory, but it doesn't work)
In sum: within an opencpu app, where do I deploy a file so that this line of code will work?
indf <- read.csv(".\\nouns-categorical_R1.csv")
The answer is simple to find.
add print(getwd()) to your opencpu function script.
First call the function using POST
The working directory can then be retrieved by calling a GET request for the url ending in "console"
The answer is that the working directory is a temp directory:
.....AppData/Local/Temp/Rtmp0qr704/ocpu_session_3780fc520c8"
This means you cannot store csvs in the working directory. This working directory changes every time you start opencpu.
It is possible to use full path to the csv when calling read.csv(). However, you need to watch for security issues and file privileges once deployed on Ubuntu.

R - copy files from folder (Shiny App)

I'm working on loading up a Shiny App IO. I use an R package that downloads data into a subfolder in my directory and saves two .RData files.
I'm having issues on the Shiny App IO server. I need to load the two .RData files. Locally, I can set the relative path using (~project/source-data). Shiny App does not respond to this.
I can set it as a working directory using a relative path, (./source-data), however, this is not ideal as I have further data manipulation to do at the parent level directory and I can't seem to set the working directory back to the parent level in Shiny App.
Here is what I had moved forward with:
wd = getwd()
sd = (paste(getwd(),"/source-data",sep=""))
sd2 = list.files(sd, full.names = TRUE)
file.copy(from=sd2, to=wd)
My solution, although not ideal, is to copy the two .RData files to the parent directory. At that point, the rest of my code will run smoothly. It works locally, but not on Shiny App.
Does anyone have experience with a similar problem and solution? Either helping me direct the Shiny App IO server to the sub-directory and back to the parent directory once I load in the two .RData files, or copying the files to the parent directory?
I've seen solutions that work on a local R environment, but not one that satisfies the conditions of a Shiny App IO server environment.
Thank you in advance.

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.

Reading a local file into a ShinyApp hosted on server

I have a shiny app hosted on a server, and part of its functionality is to read local files. I realise there is the very helpful fileIput function in shiny package - and I may use it instead - but for now I would like to learn about using file paths. The problem I face follows:
I am using the tm package, which allows users to read text files either from a directory (using DirSource("filePath"))or using inidividual files (using VectorSource("filePath")).
initialCorpus<- reactive({
if(input$confirm==0)
return()
isolate({
if(input$corpusType=="dir"){
myPath<- input$filePath
myCorpus<- Corpus(DirSource(myPath))
myCorpus
}
else if(input$corpusType=="vector"){
myPath<- input$filePath
myFile<- scan(file=myPath,what="character",n=-1, sep="\n")
myCorpus<- Corpus(VectorSource(myFile))
myCorpus
}
...
The same function works fine and reads in text files when I am using my shiny app locally. However, when I upload my app to shinyapp, and then try to upload a local file, I am unable to read in files.
So, why is it not possible to read in local files by shinyApp when the file path is used? It may be a basic question, but I want to learn.
Thanks in advance.
PS. I would be happy to link to my application if required, it's just that I would like to show my application when it's properly functioning.
I think you can solve your problem by isolating the part where you source your files : isolate({DirSource("filePath")})

Resources