3

So I have a R/shiny project on Github that require some packages, for example shinyjs, V8 and dplyr, and I have specified required(shinyjs) and library(shinyjs) in the code.

On my computer it works just fine and if I download a copy from Github that works too, but if I do it from a different computer I have to download the packages required manually.

Is there a way to make Rstudio install the required packages automatically when someone tries to run the application?

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
Ulgur
  • 75
  • 9

1 Answers1

2

This does that. Got the function here: malonypatr's install_load function

The screenshots are from RTVS, but I tested it in R-Studio too.

library(shiny)

install_load <- function (package1, ...)  {   

  # convert arguments to vector
  packages <- c(package1, ...)

  # start loop to determine if each package is installed
  for(package in packages){

    # if package is installed locally, load
    if(package %in% rownames(installed.packages()))
      do.call('library', list(package))

    # if package is not installed locally, download, then load
    else {
      install.packages(package)
      do.call("library", list(package))
    }
  } 
}

install_load("shinyjs")

shinyApp(
  ui = fluidPage(
    useShinyjs(), # Set up shinyjs
    # Add a CSS class for red text colour
    inlineCSS(list(.red = "background: red")),
    actionButton("btn", "Click me"),
    p(id = "element", "Watch what happens to me")
  ),
  server = function(input, output) {
    observeEvent(input$btn, {
      # Change the following line for more examples
      toggleClass("element", "red")
    })
  }
)

Loading:

enter image description here App:

Yielding:

enter image description here

Community
  • 1
  • 1
Mike Wise
  • 22,131
  • 8
  • 81
  • 104