Run Shiny App from Shortcut Windows 10 - r

I'm trying to create a shortcut on the desktop to run a Shiny app. I am stuck on creating a batch file to execute this and after scouring the web, I still haven't been able to get it to work.
I am on Windows 10.
At the moment I have a folder on the desktop called "test" with contents:
ui.R
server.R
run.R
test.bat
Within test.bat, I have:
"path to R.exe" CMD BATCH "path to my r script"
I double click on test.bat, and it flashes a window before closing.
How can I get this to work? Thank you very much in advance.

Probably you have solved it, but for someone who has the same question, I'm posting what worked for me. I created a .bat file like this:
"path/to/R.exe" -e "shiny::runApp('path/to/shinyAppFolder', launch.browser = TRUE)"
But I think this works as well:
"path/to/R.exe" -e "path/to/run.R"
You can always add a line with the pause command to your batch file so you can see whats going wrong with the script
Hope this helps

You have to set the R working directory to the folder containing your shiny files; or explicitly specify the path in your call to runApp().
Something like this:
test.bat
"path/to/Rscript.exe" "path/to/run.R"
run.R
library(shiny)
setwd("c:/users/username/Desktop/test")
runApp()

Related

Current path of an R script

I'm trying to make available a shiny app for some people at work but I don't want to change the working directory manually for everyone. I had plan to use the rstudioapi function getActiveDocumentContext() but it only works when the app is lunch from rstudio and i'm using R console because the app is deployed with a .bat file (describe in this page http://rstudio-pubs-static.s3.amazonaws.com/3269_a6682dfda37e411fb5e0e6699495cdc4.html). I tried a bunch of the answers here (Rscript: Determine path of the executing script) but neither work, and most of them I don't understand so I was not able to "fix theme".
As your linked SO question indicates, there are many solutions, my favorite is using rprojroot (I think it is probably the easiest). Using the simply shiny test_app example, you need to have this in your run.R:
library(shiny)
library(rprojroot)
folder_address = dirname(thisfile())
runApp(folder_address, launch.browser=TRUE)
I tested it on a Mac with the start script (test.command) below, and it works wherever you have the test_app folder:
#! /bin/bash
PWD="`dirname \"$0\"`"
cd "${PWD}"
Rscript "run.R"
On a Windows computer, you'll need to specify the path to Rscript.exe (or R.exe) in your test.bat:
"C:\Program Files\R\R-3.5.1\bin\Rscript.exe" "run.R"

R batch mode: How to see output/error of R on console screen?

While running R code in batch mode i need messages/errors/logs from R code to be shown on console screen instead of .Rout file.
I tried all the posts(links copied below) from stack overflow and none of them are showing me desired output.
running-r-in-batch-mode-print-to-screen?
unable-to-run-r-script-through-bat-files-in-windows-server
r-cmd-batch-output-in-terminal
batch code: i tried with tty0 as well.
"C:\Program Files\R\R-3.2.2\bin\R.exe" CMD BATCH --slave "Test.R" /dev/tty
Test.R code
print(1:10)
It will be very helpful if some has can help me in showing the correct way of writing the code to show the output in console screen.And i am working on windows environment.
Any help is much appreciated, thanks.
You can redirect the log that you want to print using stdout or stderr. Sample way to do that will be:
write("Hello World!!", stderr())
Here I am redirecting "Hello World!!" to stderr. If you use stdout() instead of stderr() in code snippet above then ideally it should write log to stdout. However I have seen some issues when trying to write into stdout.
I found out solution to my own question with the help of #shayaa and #abhiieor.
In case if some one needs to see their output in console they are suppose to use Rscript instead of R CMD BATCH in executable .bat file.
batch code:
"C:\Program Files\R\R-3.2.2\bin\Rscript.exe" Test.R
pause
Test.R code
print("-Hello World!!",stdout())
In above code stdout() will redirect output to console.

Creating a desktop icon for Shiny App

I have a shiny app that opens in the browser when I provide the following code in the base R prompt:
shiny::runApp("C:/Myapp")
I use windows 7. I am trying to create a desktop icon to avoid my client typing the above code every time he wants to use the app. I have created a desktop icon and the set the path in "Target" as follows:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp("C:\Myapp")
and in the "start in" box I have included
"C:\Myapp"
The app is not opening. I have tried changing the \ to / in C:/Myapp - doesn't work. However, when I try the following:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp()
that is, without referring to my app folder, rhe R program runs, loads the code shiny::runApp() and prints the message
Listening on http://127.0.0.1:4354
Can someone help on how to resolve this? I have tried various combinations of the above.
First, if your app folder is "C:\Documents\myApp", then your working directory should be "C:\Documents" (to insert in "start in" box).
Second, use ' ' for your inner quotes:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp('C:/Myapp')"
Third, consider launching your browser with your runApp command. Otherwise there might be nothing to see. (shiny::runApp('C:/Myapp', launch.browser = TRUE))
The following code saved as a '.bat' file worked for me on Windows 10 as suggested by RBloggers
start "" "C:\Program Files\R\R-4.0.3\bin\Rscript.exe" C:\RShiny\MyShinyApp\app.R /k
start "MyShinyApp" "http://127.0.0.1:4354/"

Trouble with path specifications when running RStudio from a console

I have R script within a plain text file named "TestFile.R".
I am running RStudio. I want to use the Windows console (cmd.exe) to pipe "TestFile.R" directly into the "R Script" editor of RStudio, without launching a new thread of RStudio.
This command in the console does exactly what I want:
rstudio -f TestFile.R
The contents of "TestFile.R" go straight into the RScript editor of the existing thread of RStudio.
It assumes, however, that "TestFile.R" is in the "bin" folder of RStudio, and also that
cmd.exe is running within that folder.
But I want to be able to do this from anywhere on my computer, using a console command like:
pathToRstudio\rstudio -f pathToTestFile\TestFile.R
To give an example, on my computer, this command fails:
C:\"Statistical packages"\RStudio\bin\rstudio -f E:\"my project"\TestFile.R
By trial and error, I discovered these solutions:
1/ omit the "C:\" part
2/ avoid quotation marks in the pathToTestFile.
So this console command works fine:
"Statistical packages"\RStudio\bin\rstudio -f E:\myproject\TestFile.R
Of course, I still am very restricted ; my default folder has to be "C:\", and I cannot have spaces in the path to TestFile.R, even though spaces within the Rstudio path are apparently OK !?
Could somebody please explain to me how to write this command in a way that is completely generic with regard to path specification?
I want to be able to run it from any folder, and have TestFile.R in any other folder. I do not want hassles about folder names containing spaces.

R/Shiny WebApp logs, running in shell (Ubuntu crontab)

I'm running, using crontab, an R/Shiny WebApp. The crontab expression looks like this:
#reboot R -e 'shiny::runApp(...)' | tee /var/log/shiny-server.log
I've also tried
#reboot R -e 'shiny::runApp(...)' &> /var/log/shiny-server.log
but does the same: It prints to the file the header when you initialize R from shell. But it doesn't print any text about the page, i.e. when it loads, when it uploads something, when it crashes...
Do you know how to pipe the output of the logs of the WebApp to the file?
Thank you
I have been searching in the web and I didn't find any solution.
Instead, I changed the philosophy. I generated myself some logs from inside the Shiny app with the help of the sink() function.
Thank you.

Resources