39

Currently I have a R shiny app, to run it I open up RStudio and execute

setwd("C:/Users/Me/Desktop/R/ShinyProject2")
library(shiny)
......
runApp()

From a R script located in my directory.

I am sending the app for review purposes to a co-worker who doesn't know how to use R.

Is there an easy way to write an executable that directly opens the UI without having to open R studio to execute the code?

starball
  • 20,030
  • 7
  • 43
  • 238
Green Demon
  • 4,078
  • 6
  • 24
  • 32
  • Check into [linking libR](http://cran.r-project.org/doc/manuals/r-release/R-exts.html) with C and you should be sorted. If you'd like to know more, just comment and I'll try to find some code for you to crib. – hd1 Apr 08 '13 at 17:55
  • If you have a fileserver system at your workplace (where you share folders with colleagues) you can skip the first three points. Just install R and the packages to run shiny in the shared folder. It's easier to maintain because all your colleagues work with the same R version + package then. – Jonas Tundo Apr 10 '13 at 11:45

8 Answers8

26

This can be done now. You can create a standalone shiny app, that runs on computers without needing to install R nor any library. There is a relatively simple way of doing it (currently I've done it only for Windows users, but something for MacOS should be around too), following these detailed steps: http://www.r-bloggers.com/deploying-desktop-apps-with-r/ .Other option could be uploading the app on the Shiny server.

starball
  • 20,030
  • 7
  • 43
  • 238
NicolazziE
  • 261
  • 3
  • 3
25

RStudio != R

There is a simple command-line interface to R, which you can run on Windows by running R.exe in the bin folder of your R installation.

There's also Rscript.exe, which can run an expression or a script file. For example:

C:\Program Files\R\R-2.15.2\bin\RScript -e hist(runif(1000))

will (given the right paths) create a PDF file with a histogram in it.

So,

  • your co-worker needs an R installation
  • you need that installation to have all the packages to run shiny
  • or you add a bunch of install.packages() lines to your code
  • you need to give them a folder with your shiny code
  • you add a windows .BAT file for them to click
  • they run that, it calls Rscript.exe which starts the shiny package you gave them

Or get it hosted on the RStudio guys' public shiny server, but then we can all see it.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • much thanks. dont think i can upload to shimmer, theres too much company secrets at stake. – Green Demon Apr 08 '13 at 19:01
  • 2
    If you have a fileserver system at your workplace (where you share folders with colleagues) you can skip the first three points. Just install R and the packages to run shiny in the shared folder. It's easier to maintain because all your colleagues work with the same R version + package then. – Jonas Tundo Apr 11 '13 at 06:55
12

You can now use the RInno package for this type of thing. To get setup:

install.packages("RInno")
require(RInno)
RInno::install_inno()

Then you just need to call two functions to setup an installation framework:

create_app(app_name = "myapp", app_dir = "path/to/myapp")
compile_iss()

If you would like to include R, add include_R = TRUE to create_app:

create_app(app_name = "myapp", app_dir = "path/to/myapp", include_R = TRUE)

It defaults to include shiny, magrittr and jsonlite, so if you are using other packages like ggplot2 or plotly, just add them to the pkgs argument. You can also include GitHub packages to the remotes argument:

create_app(
    app_name = "myapp", 
    app_dir  = "path/to/myapp"
    pkgs     = c("shiny", "jsonlite", "magrittr", "plotly", "ggplot2"),
    remotes  = c("talgalili/installr", "daattali/shinyjs"))

If you are interested in other features, check out FI Labs - RInno

Jonathan Hill
  • 1,745
  • 15
  • 25
  • RInno says it does not support Linux but is there a version or alternative that does? – Vass Jan 04 '20 at 16:46
9

I faced the same problem and used the two following solutions that both worked fine.

Publish the app on shinyapps.io

Advantage: the app is accessible anytime from anywhere.

Drawback: only 25 active hours per month.

Method:

  1. Go to https://www.shinyapps.io/ and create a free account
  2. Configure rsconnect to link R with your new shinyapps account (step by step explanations in the shinyapps documentation)
  3. In Rstudio, click "publish" (next to run app button)
  4. Get the app address from the shinyapps.io dashboard and send it to your co-workers.

Share app on LAN

Advantage: as much active hours as you need.

Drawbacks: requires being on the same local network (or use a VPN). The app must be running constantly on a computer connected to this network. Furthermore, it cannot perform computation for more than one user at a time.

Method: You need to run the app on your computer and share it on the LAN by changing the runapp command to:

runApp(host="0.0.0.0",port=5050)

Then the app is accessible on http://[your-IP-address]:5050 (see this: Hosting LAN Shiny apps run from command line)

JohnBee
  • 1,720
  • 1
  • 15
  • 19
4

There's now a new way to use to build a truly stand-alone Shiny app exe via "Electron" and "Photon". See following links for packages/plug-ins, etc.

That said, RStudio's shinyapps.io or a Binder deployment is likely a better choice in accessibility and development.

Arthur Yip
  • 5,810
  • 2
  • 31
  • 50
4

Below is a quick fix. go to the project directory in command line by cd. then

 R -e "shiny::runApp(port=8000,host = getOption('shiny.host', '10.10.10.10'))"

now the URL 10.10.10.10:8000 will be active.

To keep the URL alive even after you log off, nohup can be used. Obviously, not the desired setup for a production environment.

 nohup R -e "shiny::runApp(port=8000,host = getOption('shiny.host', '10.10.10.10'))" &
sjd
  • 1,329
  • 4
  • 28
  • 48
3

I know this is a really old discussion but I have recently faced this issue and after trying a few options I found that using shinyshortcut package is the best :

library(shinyShortcut) shinyShortcut(shinyDirectory = getwd(), OS = .Platform$OS.type, gitIgnore = FALSE)
T.V.
  • 793
  • 12
  • 33
0

The solution proposed by Spacedman worked for me. In the following, I'd like to give some practical examples on how to.

Step 1. Assuming that your coworkers don't know R and RStudio, they still needs R installed on their PC. They don't necessarily need RStudio.

Step 2. They need shiny library all the libraries in your shiny app installed. But suppose they don't know how to install them.

They have to run the following R code once (with some example libraries):

# collect here all the libraries used by your shiny app
install.packages(c('shiny', 'ggplot2', 'magrittr'), repos='http://cran.us.r-project.org')

If they don't have privileges to install or they want to install these additional libraries somewhere else, then specify where they have to be installed:

install.packages(c('shiny', 'ggplot2', 'magrittr'), 'E:/some_path/rlib', repos='http://cran.us.r-project.org')

Being very lazy, without using R at all they can call a batch file. You can embed this R lines into a code named "installer.R" and call it from a batch file named "installer.bat" containing the following:

set RSCRIPT=C:\Programme\R\R-4.1.0\bin\Rscript.exe
set SPATH=C:\Documents\R_codes

call "%RSCRIPT%" "%SPATH%\installer.R"

This is assuming that their current version is R-4.1.0 and the variable SPATH specifies the location of "installer.R"

Now, we are (almost) ready to start shiny.

Step 3. Normally, when you run your shiny app from R-Studio you include the command library(shiny) in your shiny code and click the icon button "Run App" in R-Studio. But without R-Studio you have to load shiny and then run the app with command lines. For me it worked by using two R codes. The first, let's call it "loader.R" containing:

library(shiny)
runApp('E:/path_to_your_app/myApp.R', launch.browser=TRUE)

And the second is your app "myApp.R":

library(shiny) # you can uncomment this because already in loader.R but leaving it does not harm

ui <- ...

server <- ...

shinyApp(ui = ui, server = server)

The option launch.browser=TRUE will open the app in a browser. By default, shiny runs with the option "window" inside R-Studio, which without R-Studio won't work.

Suppose that you have installed the necessary libraries in the path E:/some_path/rlib. Then, you have to specify it in the loader.R code with .libPaths:

.libPaths("E:/some_path/rlib")
library(shiny)
runApp('E:/path_to_your_app/myApp.R', launch.browser=TRUE)

Step 4. Create a batch file called say "call_siny_app.bat" to call your shiny app. This will contain:

set RSCRIPT=C:\Programme\R\R-4.1.0\bin\Rscript.exe
set SPATH=E:\path_to_your_app

call "%RSCRIPT%" "%SPATH%\loader.R"

The code calls "loader.R" (SPATH has to specify the correct path) which then calls "myApp.R".

You do the steps from 1 to 4 and your coworkers only have to run call_siny_app.bat.