Creating a desktop icon for Shiny App - r

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/"

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

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"

Run Shiny App from Shortcut Windows 10

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()

Run shiny application with arguments in terminal

Like in the topic, I'd like to run shiny app with parameters. I need to specify the database file's path from which I will grab the data. The problem is that the file changes sometimes thus I have to modify the file.path every time.
This is the command I use when running application from terminal
R -e "shiny::runApp('../Shiny_visualization')"
I tried
R -e "shiny::runApp('../Shiny_visualization')" --args 'db_path' yet I got an error.

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.

Resources