我正在尝试运行一个闪亮的R应用程序,需要包含某些软件包才能访问这些软件包中的函数,以下是我在应用程序中包含的所需软件包列表:
server.R:
library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
require(magrittr)
library(dplyr)
library(lubridate) #to convert date into day
library(DataExplorer)
library(gmailr)
library(purrr)
library(DT)
library(plotly)
library(shinycssloaders)
library(rgdal)
library(shinythemes)
library(magrittr)
我遇到的问题是,当我运行应用程序时,这些代码行似乎没有执行。我仍然会收到如下错误:
错误:找不到功能“%>%”
错误:找不到函数“plotlyOutput”
因此,每次运行应用程序时,我都必须手动从包选项卡中选择已安装的软件包。代码放置错了吗?在运行应用程序之前在控制台上运行命令时代码运行正常但在放置在server.R文件开头时它无法正常工作
答案 0 :(得分:2)
您需要将其放在R脚本的开头,例如:
library(dplyr)
library(stringr)
shinyServer(
function(input, output, session) {
希望它有所帮助。
编辑:
然后让我们将所有的包名放入一个列表中,然后检查它们是否已安装/加载。
mypackages <- c("packagename1", "packagename2", "packagename3")
checkpkg <- mypackages[!(mypackages %in% installed.packages()[,"Package"])
if(length(checkpkg)) install.packages(checkpkg, dependencies = TRUE)
将所有这些代码放在library("packagex")
答案 1 :(得分:0)
仅安装系统中尚不可用的软件包。然后加载所需的软件包。
#Installing Packages that are not already available in the system
list.of.packages <- c("ggplot2","readr","magrittr","dplyr","lubridate","DataExplorer","gmailr","purrr","DT","plotly","shinycssloaders","rgdal","shinythemes","magrittr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#Loading Packages
invisible(lapply(list.of.packages, require, character.only = TRUE))