Installing required packages in Shiny app - r

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

Related

Error in library(sp) : there is no package called 'sp'

The sp package was installed using RStudio (through CRAN) but when R evaluates the code:
library(sp)
It throws an error
Error in library(sp) : there is no package called 'sp'
I noticed that it works for R versions 4.* but does not work for versions 3.*. What could be causing this?
If you first time to use 'sp', install.... :)
install.packages("sp")
library(sp)

load data error (Read10X, Seurat - Guided Clustering Tutorial)

I am trying to follow the tutorial from Seurat website.
https://satijalab.org/seurat/v3.1/pbmc3k_tutorial.html
I got an error when loading the data.
Load the PBMC dataset
pbmc.data <- Read10X("1_Guided_tutorial/pbmc3k_filtered_gene_bc_matrices.tar.gz")
Error in Read10X("1_Guided_tutorial/pbmc3k_filtered_gene_bc_matrices.tar.gz") :
Directory provided does not exist
I have set my working directory as the image showed. Can anyone let me know what is the problem? Thank you!
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("multtest")
library(dplyr)
library(Seurat)
library(patchwork)
# Load the PBMC dataset
pbmc.data <- Read10X(data.dir = "1_Guided_tutorial/pbmc3k_filtered_gene_bc_matrices.tar.gz")
I was trapped with this problem few minutes before, and I find the solution now.
Try to use these codes:
install.packages("Seurat")
library(Seurat)
install.packages(c('dplyr','patchwork'))
library(dplyr)
library(Seurat)
library(patchwork)
in order to install the environment for scRNA analysis

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

knitr package loading error stringr

I get the following error:
1Error in library(stringr) :
Package 'stringr' version 1.2.0 cannot be unloaded
when loading the following packages in my knitr document:
library(checkpoint)
checkpoint("2017-01-01")
library(stringr)
library(plyr)
library(tidyr)
library(dplyr)
library(knitr)
library(readr)
library(readxl)
library(ggplot2)
library(scales)
library(ggthemes)
library(lubridate)
library(xtable)
library(zoo)
library(gridExtra)
when I remove stringr then I get the error:
Error in mutate_impl(.data, dots) : could not find function "str_sub"
...any ideas? I am using Mac OSX Mavericks. I copied the files over from a Windows 7 (where it worked) and I'm assuming that has something to do with it.
Thank you in advance!
There is a conflict due to the stringr package being newer (2017-02-18) than your checkpoint date of 2017-01-01.
You could clear the conflict by moving the date you use to something later, i.e.
checkpoint("2017-02-18") # or later
Alternatively, if you find you have mixed dependencies that rely on packages that were not all current on the same date, I would recommend having a look at packrat (link), which is designed for that purpose.

rCharts dTable Error

Hi I'm new to rCharts and could definitely use some help. I'm getting the error below when I try to run the following.
Code:
dTable(airquality, sPaginationType = "full_numbers")
Error:
Error in as.character(tools:::httpdPort) :
cannot coerce type 'closure' to vector of type 'character'
Also when I try ?dTable I get the message:
No documentation for ‘dTable’ in specified packages and libraries:
you could try ‘??dTable’
I installed rCharts and devtools using:
install.packages("devtools")
and
install.packages("base64enc")
devtools::install_github("ramnathv/rCharts")
then I called both libraries using library(devtools) and library(rCharts).
Any tips greatly appreciated.
I created a fig directory under my current working directory, ran the following code and it created an html file with the sortable table in the fig directory:
#install.packages("devtools")
install.packages("base64enc")
require(base64enc)
library(devtools)
require(rCharts)
data("airquality")
table1 <- dTable(airquality, sPaginationType = "full_numbers")
table1$save('./fig/table1.html', standalone=TRUE)

Resources