在Shiny app中安装所需的软件包

时间:2017-07-27 09:15:54

标签: r shiny

我有一个闪亮的应用程序,我希望每个人都能够使用runGitHub运行,只需要安装闪亮的包装。

为了在第一次运行程序时在个人计算机上安装和加载所有需要的软件包,我在 server.R 中的代码以:

开头
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) 

尽管如此,我只是在别人的电脑上测试了它,并出现以下错误:

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

手动安装shinyjs后,出现以下内容:

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’

等等。这是我的第一个闪亮的应用程序,所以我不知道我应该怎么做到这一点。可以通过运行来访问我的完整代码:

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

2 个答案:

答案 0 :(得分:2)

packages可能会有一些dependencies,因此需要安装所有具有依赖关系的包。要为每个新用户解决此问题,您可以像这样执行检查和安装(如有必要)。

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

希望这有帮助。

答案 1 :(得分:2)

这对我有用:

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

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