简体   繁体   中英

Installing required packages in Shiny app

I have a shiny app that I want everybody to be able to run using runGitHub with the only pre requisite of having the shiny packaged installed.

In order for all the needed packages to be installed and loaded in the person´s computer the first time he runs the program, my code in server.R starts with:

if (!require("pacman")) install.packages("pacman")
pacman::p_load("maptools","dplyr","data.table","reshape2","ggplot2","plyr","rgdal","rgeos","shinyjs","scales","DT","readxl")

library(maptools)
library(dplyr)
library(data.table)
library(reshape2)
library(ggplot2)
library(plyr)
library(rgdal)
library(rgeos)
library(shinyjs)
library(scales)
library(DT)
library(readxl) 

Nevertheless, I just tested it in someone elses pc and the following error shows up:

Error in library(shinyjs) : there is no package called ‘shinyjs’

After I installed shinyjs manually, the following showed up:

Warning: Error in library: there is no package called ‘maptools’
Stack trace (innermost first):
46: library
45: eval [helper.R#1]
44: eval
43: withVisible
42: source
 3: runApp
 2: runUrl
 1: runGitHub
Error in library(maptools) : there is no package called ‘maptools’

And so on. This is my first shiny app, so I don't know how am I supposed to achieve this. My complete code can be accessed by running:

runGitHub("Mapas_BBVA_municipios","IArchondo",display.mode="showcase") 

There are chances that the packages might have some dependencies along with it, so all packages with dependencies need to be installed. To resolve this for every new user, you can perform check and install(if necessary) like this.

#list of packages required
list.of.packages <- c("pacman","maptools","dplyr","data.table","reshape2","ggplot2","plyr","rgdal","rgeos","shinyjs","scales","DT","readxl")

#checking missing packages from list
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]

#install missing ones
if(length(new.packages)) install.packages(new.packages, dependencies = TRUE)

Hope this helps.

This works for me:

list_of_packages = c("ggplot2","pacman")

lapply(list_of_packages, 
       function(x) if(!require(x,character.only = TRUE)) install.packages(x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM