简体   繁体   中英

Unable to include all required packages in shiny app in server.R file code

I'm trying to run a shiny R app which requires certain packages to be included for accessing functions from those packages, here is the list of packages required that i'm including in my app:

   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)

the problem i'm facing is it seems that these lines of code are not executing when i run the app. i still get errors like:

Error : could not find function "%>%"

Error : could not find function "plotlyOutput"

so each time i run the app i have to select the installed packages from packages tab manually. is the code placement wrong? the code runs fine when commands are run on console before running app but its not working when placed at the beginning of server.R file

You need to put that at the start of your R script, like:

library(dplyr)
library(stringr)

shinyServer(

  function(input, output, session) {

Hope it helps.

Edit:

Then lets put all your packagenames into a list and then check if they are installed/loaded or not.

Create a list and put all your package names there.

mypackages <- c("packagename1", "packagename2", "packagename3")

Check if you have them installed

checkpkg <- mypackages[!(mypackages %in% installed.packages()[,"Package"])

Install the missing ones

if(length(checkpkg)) install.packages(checkpkg, dependencies = TRUE)

Put all this code before the library("packagex")

Install only Packages that are not already available in the system. Followed by loading the required packages.

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

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