My shinydashboard app works on my machine but not on shinyapps.io - r

and thanks for any help!
I keep getting the same error message when trying to publish the app on shinyapps.io:
The application failed to start (exited with code 1).
I have already commented the setwd() and library(shiny) as I have learned from other posts, but so far no luck. This is the screenshot of the error.
I am new to this, so any support is greatly appreciated.

It looks to me like you are using an absolute file path in your script. shinyapps.io won't understand a file path specific to your machine.
Instead, try putting the files you need to read in a folder (e.g. 'InputFiles') and put that folder in the same place as your scripts. Change your scripts to refer to files using relative file paths like: 'InputFiles/file1.csv'.
When you run the code locally make sure to set the working directory to the same directory your scripts are in. When you publish to shinyapps.io make sure to include your scripts and the 'InputFiles' directory.
Here's a great explanation of how these work: https://docs.rstudio.com/shinyapps.io/Storage.html#Storage

The solution came to me after reading Thomas's post. I had an R script (which did all the statistics and plots for my dashboard) stored in the same folder where the shiny UI and server were stored. After moving this script file to a different folder, the problem was solved. I do not quite understand why this fixed the issue, but I hope this article helps people facing similar issues.

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

Rstudio setwd not there when creating new file

When I setwd using Rstudio IDE GUI, then I go to create a new file... the default working directory is not the current working directory. What is a better Workflow? Any .Rprofile commands to always save in the working directory?
pic 1
pic 2
As mentioned in the comments, using a project would certainly help. Projects will always set the working directory to the project root folder on opening. Additionally, previous commands and working environment is saved (if you want it to be), so you can get right back to where you were. This is especially useful if you are working on different assignments.
Additionally, you can use getwd() to get your current working directory. Remember that the RStudio file-browser doesn't update when you set your working directory in R.

How to set default working folder in Rstudio for new session?

I face the problem with changing of working directory.
Here are my steps to reproduce it:
Start R session (The working directory matches the one set in the settings)
Change current working directory and start coding
Start one more new Session (The working directory for the second session becomes the one used in the previous session but not the one set in the settings)
This is serious problem if i work with folders which contain many files as new session just fails to start
You can use the following at the beginning of each script:
# set the R scripts working directory
library(rstudioapi)
current_path <- getActiveDocumentContext()$path
if (getwd() != current_path){
setwd(dirname(current_path ))
}
This piece of code will define the working directory as the location of the script, it's really useful when working with multiple scripts that has lots of dependencies and they fail due to wrong working directory
EDIT
After understanding better the desired behaviour from the comments, you should wrap you code in a project hierarchy and set the the default working directory of the project to the desired one.
The code I added above is suitable for the case you want your scripts to run regardless of the session working directory

Unable to use correct file paths in R/RStudio

Disclaimer: I am very new here.
I am trying to learn R via RStudio through a tutorial and very early have encountered an extremely frustrating issue: when I am trying to use the read.table function, the program consistently reads my files (written as "~/Desktop/R/FILENAME") as going through the path "C:/Users/Chris/Documents/Desktop/R/FILENAME". Note that the program is considering my Desktop folder to be through my documents folder, which is preventing me from reading any files. I have already set and re-set my working directory multiple times and even re-downloaded R and RStudio and I still encounter this error.
When I enter the entire file path instead of using the "~" shortcut, the program is successfully able to access the files, but I don't want to have to type out the full file path every single time I need to access a file.
Does anyone know how to fix this issue? Is there any further internal issue with how my computer is viewing the desktop in relation to my other files?
I've attached a pic.
Best,
Chris L.
The ~ will tell R to look in your default directory, which in Windows is your Documents folder, this is why you are getting this error. You can change the default directory in the RStudio settings or your R profile. It just depends on how you want to set up your project. For example:
Put all the files in the working directory (getwd() will tell you the working directory for the project). Then you can just call the files with the filename, and you will get tab completion (awesome!). You can change the working directory with setwd(), but remember to use the full path not just ~/XX. This might be the easiest for you if you want to minimise typing.
If you use a lot of scripts, or work on multiple computers or cross-platform, the above solution isn't quite as good. In this situation, you can keep all your files in a base directory, and then in your script use the file.path function to construct the paths:
base_dir <- 'C:/Desktop/R/'
read.table(file.path(base_dir, "FILENAME"))
I actually keep the base_dir assignemnt as a code snippet in RStudio, so I can easily insert it into scripts and know explicitly what is going on, as opposed to configuring it in RStudio or R profile. There is a conditional in the code snippet which detects the platform and assigns the directory correctly.
When R reports "cannot open the connection" it means either of two things:
The file does not exist at that location - you can verify whether the file is there by pasting the full path echoed back in the error message into windows file manager. Sometimes the error is as simple as an extra subdirectory. (This seems to be the problem with your current code - Windows Desktop is never nested in Documents).
If the file exists at the location, then R does not have permission to access the folder. This requires changing Windows folder permissions to grant R read and write permission to the folder.
In windows, if you launch RStudio from the folder you consider the "project workspace home", then all path references can use the dot as "relative to workspace home", e.g. "./data/inputfile.csv"

When making a Shiny App, where does your Data Frame live?

If I have a csv file that I want to be a part of a reactive shiny app, where does my csv file need to exist, and what part of the code needs to call it? Can it exist on AWS?
Does it need to exist in the "www" folder with images? Does it need to be called inside the server? And what if I want to deploy it to shinyapps.io?
Any quick help would be amazing. Thanks a ton.
(This is probably a simple question, but I've been through numerous tutorials and samples and just haven't found an answer.)

Resources