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!
Related
I want to access data in a Shiny App directly via Google Analytics API and now have an google OAuth 2.0 authentication issue.
My approach was the following one:
1) Created a web aplication project in my Google project and downloaded the json File.
2) I added the json file to my app folder. So the structure of my app folder is the following:
app.r
test.json
3) Then I created a .Renviron file in the same app folder. My assumption for the error lays here, since I am not sure if I created it correctly. I created a txt file and named it .Renviron. And I inserted the following line: GAR_CLIENT_WEB_JSON = "test.json"
So now in my app folder are: app.r, test.json, and .Renviron
4) in app.R I have the following code:
library(shiny)
library(googleAuthR)
gar_set_client(scopes = c("https://www.googleapis.com/auth/analytics.readonly"))
# Define UI
ui <- fluidPage(
gar_auth_jsUI("auth")
)
# Define server logic
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
When testing with Sys.getenv("GAR_CLIENT_WEB_JSON") it always tells me, that it is empty.
And when running the app, I get the following error:
Error in gar_set_client(scopes = c("https://www.googleapis.com/auth/analytics.readonly")) :
No client JSON files found
Does anybody has an idea what I am doing wrong?
Just to confirm if the environment file is created properly, can you try creating your environment file this way:
Open up Notepad, paste in your environment variables
File > Save As...and find your local project folder, then under "Save as type" select "All Files", THEN type the File name ".Renviron"
Try again to see if the environment variable is being read.
Try stating the full/explicit filepath in the the .Renviron file when assigning both "GAR_CLIENT_WEB_JSON" and "GL_AUTH"
Eg:
GL_AUTH="/Users/yourUser/Desktop/yourFoldername/yourFilename.json"
GAR_CLIENT_WEB_JSON="/Users/yourUser/Desktop/yourFoldername/yourFilename.json"
Solved the error in my case.
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.
I know that if the application will contain 2 separate files: ui.R and server.R, I could run:
library(shiny)
shiny::runGitHub('username/repo_name')
Now, is it possible to run a shiny application hosted in Github which contains the ui and server in only one R file? For example:
library(shiny)
shiny::runGitHub('manolo20/shinytreemap')
Thank you.
According to the error message you get from shiny::runGitHub('manolo20/shinytreemap')
Error in shinyAppDir(x) : App dir must contain either app.R or server.R.
the anser is yes. you just need to use the name app.R for the R file containing the app. Also, this file must be at the top level of the repository unless you use the subdir argument.
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
I'd like to unzip a compressed .mdb file in the www folder of my shiny app, query it for data, and then remove it. Unzip() works on my local machine, but when I deploy the app at shinyapps.io, it has issues unzipping the file. Because I'm not able to read.table() the resulting file (it's an .mdb) I don't think unz() will work.
This code works when run on my local machine
Server:
require(shiny)
shinyServer(function(input, output) {
observeEvent(input$run,{ #Run Button
dbName=unzip('www/test.zip', list=T)
output$name=renderText({
paste(dbName[1])
})
db=unzip('www/ttt.zip', exdir='www', unzip=getOption("unzip"))
test1=read.csv(db) #.csv for simplicity, but my problem uses a .mdb
file.remove(db)
output$testcount=renderText({
paste(sum(test1))
})
})#/Run Button
})#/SS
ui:
shinyUI(
sidebarLayout(
sidebarPanel(width=3,
h5('ZIP test'),
p(align="left",
shiny::actionButton("run", label = "Run!")
),
textOutput(outputId = "name"),
textOutput(outputId = "testcount")
),
mainPanel(width=9,
plotOutput(outputId = "probs",height = "550px")
)
)
)
But fails when uploaded to Shinyapps.io.
Any idea of what I'm doing wrong here? I've tried passing the file path directly, and messing with the unzip= options, but to no avail. If I remove the second call, It will tell me the name just fine, but if I try to unzip the file, it breaks.
Any help is appreciated!
EDIT
I was able to get it to work by removing exdir='www', unzip=getOption("unzip") and just looking for the file in the root directory: test1=read.csv('file1.csv')
I'm using unzip() on shiny-server and everytime the function is called the content of the .zip is saved in the root directory of the app. I asume thats a problem for shinyapps.io.
In the documentation you can only specify the location where the file is with 'exdir' from what I read.