Deploying shinyapps on a desktop - r

I have a shiny app that runs perfectly on my computer and shinyapps.io. This app is being built for a client where i need to share it to run on their desktops. I have used the guidance given on this here.
The first line of my shiny server reads a RDS file and loads the data. I have copied the RDS within the shiny folder as given in the example shared above. My current folder looks like this:
C:/dist/
GoogleChromePortable
R-Portable
shiny - within shiny folder i have ui.R, server.R, lhg.RDS
run.vbs
runShinyApp.R
run.vbs
Randomize
CreateObject("Wscript.Shell").Run "R-Portable\App\R-Portable\bin\i386\R.exe CMD BATCH --vanilla --slave runShinyApp.R" & " " & RND & " ", 0, False
runShinyApp.R
.libPaths("./R-Portable/App/R-Portable/library")
browser.path <- file.path(getwd(),"GoogleChromePortable/GoogleChromePortable.exe")
options(browser = browser.path)
shiny::runApp("./Shiny/",port=8888,launch.browser=TRUE)
My server begins like this:
lhg <- readRDS(file.path(getwd(),"LHG.RDS"))
When I run the vbs file, it opens a browser, but throws a error message:
Error:object "lhg" not found
And everytime I run, the error creates a new file with some random numbers that contains the logs of the same error.
Can someone help please?

You might want to try out the RInno package (I just published it to CRAN last month). It helps automate these types of desktop deployments, and you should be able to get setup with:
install.packages("RInno")
require(RInno)
RInno::install_inno()
The directory containing ui.R & server.R is app_dir:
create_app(app_name = "Your app's name", app_dir = "path/to/app_dir")
compile_iss()
The resulting installer should install your app "as is," so you don't need to manage those tricky working directory issues. If it works in your dev environment, it should work after it is installed. If you'd like more resources on how to customize your installation, check out FI Labs - RInno

Related

Getting an error when trying to publish my shiny app to the server for others to view

My shiny app runs fine in my local environment, however, when I go to publish it online I get the error "An error has occurred. The application failed to start. Contact the author for more information."
When I review the error I get:
"Line 36 Paths should be to files within the project directory"
"Line 38 Paths should be to files within the project directory"
Here is the code for both of those lines:
36.
data <- read_excel("~/data/T Version/Values for R/Pricing 081021.xlsx") %>%filter(Date_of_Indication == max(Date_of_Indication))
38.
portfolio <- read_excel("~/data/T Version/Values for R/Portfolio for R 081021.xlsx")
I'm a bit stumped as to why it isn't working, any help would be much appreciated.
Thanks
The online app doesn't have access to your local directory. You need to either include the data in the code itself with something like dput() or include the URL to the raw data file via GitHub or something similar.
For example, I use code like this to read a csv from my GitHub repository for my online apps:
dat<-read.csv("https://raw.githubusercontent.com/UserName/RepoName/DataInput.csv")
But note that the repo has to be set to be publicly available (not private).
A potentially helpful link: https://shiny.rstudio.com/tutorial/written-tutorial/lesson7/

R - Why am I getting an error message in a shiny server?

I'm currently usinga shiny server using DigitalOcean with Ubuntu as the operating system. I created a survey with the hopes of collecting user inputs and writing them in a csv file.
Locally, it works (i.e. responses get saved in a local folder). However, when I developed a shiny app within my new R Studio server to test it, there is an error which reads:
"Error: cannot open the connection"
This occurs when I press the submit button. I think the error occurs because my shiny server doesn't have rights to save files in my desired folder, i.e.
/home/my_user/test
The folder I'd save the responses would be located in that shiny folder in my new R Studio directory.
I've checked extensively on the website and I found:
Write permission from shiny app
Permission denied while using write.csv on ubuntu server
This is my saveData function:
saveData <- function(data) {
fileName <- sprintf("%s_%s.csv", humanTime(), digest::digest(data))
write.csv(x = data, file = file.path(responsesDir, fileName), row.names = FALSE, quote = TRUE)
}
The response directory (responsesDir) is
/home/my_user/test/csv_files
I've tried the following:
sudo chown shiny:shiny /var/shiny-server/test/
It says that there is no such file or directory.
Am I missing something very obvious? Thank you so much!

Temp files automatically deleted in R shiny app - Error in file: cannot open the connection

I created a R shiny app that automatically runs every day using a batch file.
Everything works fine when lauching the app, but the next day it crashes and I get the following message:
Warning in file(open = "w+") :
cannot open file
'C:\Users\bertin\AppData\Local\Temp\RtmpKiBPOU\Rf3f835d1a66' : No such file or directory
Warning: Error in file: cannot open the connection
[No stack trace available]
Actually this issue is related to the tempdir() folder created by the R session executing the shiny app. This folder is automatically deleted after a certain time. Do I have to delete all Temp files on each refreshing? Or on the contrary is it needed to prevent R from deleting all shiny temp files on Temp folder? Thanks!
Edit - Here is how to intentionally generate the error:
tempdir()
dir.exists(tempdir())
library(shiny)
# Windows shell required
shinyApp(
ui = fluidPage("Please reload to see me fail."),
server = function(input, output) {
shell(paste("rmdir", dQuote(
normalizePath(tempdir(), winslash = "/", mustWork = FALSE), q = FALSE
), "/s /q"))
}
)
By now I've found a setting in Windows 10 (Storage Sense) concerning the deletion of temporary files, which seems to be active by default.
Navigate as follows and uncheck:
Settings
System Storage
Storage Sense
Change how we free up space automatically
Delete temporary files that my apps aren't using
With the deletion of your temp directory also session data gets lost. But if I understand your question correctly, this is not relevant for your Shiny Application.
So if you don‘t need any session data from yesterday you could call ‘.rs.restartR()‘ to restart your R session and thus setting a new temporary directory. You will probably get an error that your last session could not be saved (as the directory doesn‘t exist anymore).
After this you should be able to start your Shiny App again.

ERROR: cannot open the connection in R Shiny

I went through all of the steps found here, and even got the following message without error:
Application successfully deployed to https://user-name.shinyapps.io/projectFolder/
However, I get the ERROR: cannot open the connection message when trying to run the program. Here are the contents of the folder (projectFolder) to which I directed R Studio:
ui.R # contains only ui code
server.R # contains only server code
script.R # my full script, which contains global, ui, and server code
gomap.js # used for mapping app
styles.css # used for Shiny App
data.csv # my global data to be hosted on shinyapps.io
Here's a sample of the different scripts:
ui.R
ui <- shinyUI(navbarPage("Tab title", id="nav",
tabPanel("Interactive map",
div(class="outer",
tags$head(
includeCSS("/Users/user/Documents/R/projects/styles.css"),
includeScript("/Users/user/Documents/R/projects/gomap.js")
),
#### more UI code ####
))
))
Might the issue be because of the filepaths above? Do I need to setwd at the top of both the ui.R and server.R files? Or is it because within script.R you can find the full code for ui.R and server.R (perhaps this is redundant and I need to create a global.R file with just the data loading and manipulation?
The overarching question is, how do you break up your files to load onto shinyapps.io?
GBR24, some things you can try:
Relative Paths
Set up your wording directory as to where your ui.R files and server.r files are and then use relative lowercase paths to your subdirectories like css when deploying, not full ones with \user\Me\MyR\Project1\ ...etc.
Path layout example:
directory with ui.r file which will be
--css subdirectory
--data
--www
so when you call your data that you have placed in data subdirectory use:
myfile <- file.path("data", "data.csv")
dat <- read.csv(myfile, header=T)
NO CAPS
This could be a problem with capitalisation of file names and paths. This has just started to happen to me. On deploying in RStudio I get a review issues dialogue when publishing content with a "filepaths are case-sensitive on deployment server warning".
So, for example, Shiny server wants serverhead.R not serverHead.R. Solution is to change your file names to lowercase. It seems to be okay with .R extension capitalised for now.
github windows users: You need to remind Github that you want lowercase so it does not push files back with CaseNotLowered.R
In Gitshell, you force the file name:
git mv -f OldName newname
Thanks to Github Support and answers here.
Look at logs
You can check on your deployment from RStudio using this command for clues. From console command line, with your account and app name:
rsconnect::showLogs(account = "myshinyioaccount", appName = "myapp")
EDIT it was formerly shinyapps::showLogs (thanks conrad-mac)
For example I could see a filename problem before the connection error message:
... 2016-07-12T13:13:26.061123+00:00 shinyapps[555]: Error in file(filename, "r", encoding = encoding) :
2016-07-12T13:13:26.060971+00:00 shinyapps[555]: 2: eval.parent
2016-07-12T13:13:26.061126+00:00 shinyapps[555]: cannot open the connection
Hope this helps!

Shiny App Error: /v1/applications/ 400 - Validation Error Execution halted

Hi I'm having a million problems trying to publish my app to shiny.io.
Firstly, I have Rtools 3.2 installed in my computer and set to the Path, but it is not recognized in the registry. Nevermind, this code should fix it:
install.packages("installr")
library(installr)
install.Rtools(choose_version = FALSE, check = TRUE, use_GUI = TRUE,
page_with_download_url = "http://cran.r-project.org/bin/windows/Rtools/, keep_install_file=TRUE")
install.packages("devtools")
library(devtools)
devtools::install_github('rstudio/shinyapps')
Next, to deploy my app to my shiny.io account:
library(shinyapps)
shinyapps::setAccountInfo(name='xxxx', token='xxxxxxxxxx', secret='xxxxxxxx')
Then my app starts running in a browser, and I click publish to my shiny account. However, when the app is being deployed, it shows the following error:
Preparing to deploy application...Error: /v1/applications/ 400 - Validation Error
Execution halted
Any ideas what the problems may be? Thank you.
I had the same error returned. In my case the problem was the name of the app itself. Deployed apps must have names at least 4 characters long with no spaces.
Setting an application name solved this problem for me. My application directory contained a space.
deployApp(appName = "myapp")
I had the same problem, however, my app name was fine and even adding 'appName =' did not help. Just a side note that this issue came up because I changed the name of my folder in effort to change the name of my app in shinyapp.io
The only thing that worked for me is publishing through the "Publish" button of Rstudio on upper right. I would recommend publishing using that instead of command. You can select files you do not want to publish within the App folder and you can publish the app under a different name then your local name.
I also had similar errors and the issue was resolved after I changed the name of the directory that holds the "app.R" file from only 3 characters to more than 4 characters.

Resources