1
required_packs <- c("pdftools","readxl","pdfsearch","tidyverse","data.table","stringr","tidytext","dplyr","igraph","NLP","tm", "quanteda", "ggraph", "topicmodels", "lasso2", "reshape2", "FSelector")
new_packs <- required_packs[!(required_packs %in% installed.packages()[,"Package"])]
if(length(new_packs)) install.packages(new_packs)
i <- 1
for (i in 1:length(required_packs)) {
 sapply(required_packs[i],require, character.only = T)
}

Produces a warning for each new package. I have tried sapply outside a loop as well and the same warnings appear.

Warning in if (!character.only) package <- as.character(substitute(package)) :
  the condition has length > 1 and only the first element will be used
Matt Dietz
  • 37
  • 5
  • I don't get those warnings. Could you post the result of `sessionInfo()`? If you're getting the warning without `sapply`, maybe you should also post the simplest reproducible example you can put together. – user2554330 Dec 27 '21 at 16:34
  • Side notes: (1) no need to pre-instantiate `i <- 1` here, it is done automatically in the `for` loop. (2) Don't use `require` unless you intend to capture its return value *and do something with it*. Use `library` instead. (https://stackoverflow.com/a/51263513/3358272, https://yihui.org/en/2014/07/library-vs-require/, https://r-pkgs.org/namespace.html#search-path) (3) Since `i` will be length 1, there's no need for `sapply` here. And you can stop "counting" them and just iterate over the package names themselves: `for (pkg in require_packs) library(pkg, character.only = TRUE)`. – r2evans Dec 27 '21 at 16:39

1 Answers1

0

I think the problem is that you used T when you meant TRUE. For example,

    T <- 1:10
    require("stats", character.only = T)
#> Warning in if (!character.only) package <- as.character(substitute(package)):
#> the condition has length > 1 and only the first element will be used

Created on 2021-12-27 by the reprex package (v2.0.1)

Remember, T is a variable name in R. You can change its value. It is frequently a source of obscure bugs when you use it instead of the constant value TRUE.

user2554330
  • 37,248
  • 4
  • 43
  • 90