R - copy files from folder (Shiny App) - r

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.

Related

Unsure where image_write downloads to in shinyapps.io

I'm attempting to make a public shinyapps.io website, and I'm trying to use image_write to create a file into a local directory.
The following code works on my local R studio code:
image_write(im.resized, path = paste0(output_file_directory, file_name), format = "jpg")
When I run the code on the shinyapps.io website, the code runs without error, but I'm not sure where it downloads the file to. I know that the output_file_directory part isn't the issue, so I'm a little lost. Any help would be much appreciated!
On shinyapps.io it is not possibly to store permanently data, due to:
"Shinyapps.io is a popular server for hosting Shiny apps. It is designed to distribute your Shiny app across different servers, which means that if a file is saved during one session on some server, then loading the app again later will probably direct you to a different server where the previously saved file doesn’t exist."
See here:
https://shiny.rstudio.com/articles/persistent-data-storage.html

Problem with directory when deploying app on shinyapps

I wrote a golem app and wanted to deploy it on the shinyapp.io. Unfortunately, every time I try to do it the following error comes up (in logs):
Warning in loadSupport(appDir, renv = sharedEnv, globalrenv = NULL) :
Loading R/ subdirectory for Shiny application, but this directory appears to contain an R package. Sourcing files in R/ may cause unexpected behavior.
All files related to my project are stored in one directory, where my golem project was initially created. I also checked and set manually working directory to 'R' folder (where app_server and app_ui are stored). Unfortunately when I deploy my app the mentioned error comes up again. Moreover, every time I close my project in RStudio I save workspace image to '.RData' file (this file is also stored in main directory) - maybe here is a problem (but I also tried to deploy w/o this file and it failes either). I really don't know where the problem lies and what this error means.
Interestingly, regular (single) app.R can be deployed on shinyapps without a problem.
Since Shiny 1.5, if you run a shiny app with a subdir called R/, it will load every function stored in it automatically. You can avoid this setting the autoload option to FALSE, doing:
options(shiny.autoload.r=FALSE)
What I do (I'm not sure whether it is best practice) is setting that up just before calling shiny::runApp(). For intance, I usually have a launch() function in my package, which calls shiny::runApp(). Including the option within this launch() function should fix the issue.
Nonetheless, the message is a warning, not an error, and it is possible that everything is working properly in your shiny app.

How to set RStudio R console working directory from r-markdown chunk

I am attempting to make a workflow as foolproof as possible using rmds and Git. Here, new rmd files and updates to old files are served via Git which is used by RStudio shell.
The issue is with different working directories between individual rmd files and the R console in RStudio. By default, the shell from which Git is used opens to the working directory of the R console. However, by default, rmd files work in the folder they are residing in, which is necessarily not the same as the R console working directory.
I am aware of the solution that sets the working directory of all cells within one rmd file (below), but this will not change the working directory of the R console, only chunks.
knitr::opts_knit$set(root.dir = "path/to/desired/wd")
Hence, I'd ask if anyone is aware of a way of forcing the R console working directory to match the rmd working directory. Ideally, this would be done within an rmd cell in a way that executing one cell would take care of matching the working directories automatically.

Access to Shiny app in another computer within the same network

I've created a Shiny app that runs perfectly when I run it in my computer (I have the "ui.R" and "server.R" files in a directory of my desktop).
But I want to other people in my workplace (also using R and RStudio) can also run it.
I copy the "ui.R" and "server.R" files to a directory in another computer, so anyone with the permissions can access this app.
The problem is that when I move this files to other computer's folder I can't run the app anymore. It gives me the error "No Shiny application exists at the path..."
I set the working directory to this new path where I put the files, so I don't know where the problem is. I have lecture and writing permissions to this folder.
I'm using Windows 7.
A much easier way would be keep server.R and ui.R in one script without copying to a specific area.
shinyApp(ui = ui, server = server)

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