Problems with Libraries and Rscript in Windows?

I am trying to plan to deploy an R script that I have developed in RStudio for use at the Windows command line with Rscript and I am having problems with the libraries and library paths (presumably).

Is there a straightforward approach to handling this?

My scripts use: dplyr, pdftools, lubridate, and data.table libraries.

When I run it in RStudio, it seems to run fine.

When I try to run the straightforward approach to this code, I get:

C:\Users\...\Documents\Analytics\data2>Rscript HCCReport.R  throw-away
Loading required package: pdftools
Loading required package: dplyr
Loading required package: lubridate
Error in library(pdftools) : there is no package called 'pdftools'
Calls: source ... eval -> source -> withVisible -> eval -> eval -> library
In addition: Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'pdftools'
2: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'dplyr'
3: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'lubridate'
Execution halted

C:\Users\...\Documents\Analytics\data2>Rscript -e print(.Library)
[1] "C:/Users/.../Documents/Analytics/dist/R-Portable/App/R-Portable/library"

I have looked around online for different suggestions which seem to be along the lines of adding different R library paths to a library path. But when I try to put that code into the script it doesn't seem to make a difference:

.libPaths(c("C:/Users/.../Documents/Analytics/dist/R-Portable/App/R-Portable/library",
            "C:/Users/.../Documents/R/R-3.4.4/library", .libPaths()))

print(.libPaths())
# [1] "C:/Users/.../Documents/Analytics/dist/R-Portable/App/R-Portable/library"
# [2] "C:/Users/.../Documents/R/R-3.4.4/library"                               

Run with Rscript at the command line I get:

C:\Users\...\Documents\Analytics\data2>Rscript HCCReport.R  CCD_XXX_YYYY_2018-12-20.pdf
[1] "C:/Users/.../Documents/Analytics/dist/R-Portable/App/R-Portable/library"
[2] "C:/Users/.../Documents/R/R-3.4.4/library"
Loading required package: pdftools
Loading required package: dplyr
Loading required package: lubridate
Error in library(pdftools) : there is no package called 'pdftools'
Calls: source ... eval -> source -> withVisible -> eval -> eval -> library
In addition: Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'pdftools'
2: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'dplyr'
3: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'lubridate'
Execution halted

Thanks in advance for your comments.

Sorry for the very late reply here! Did you end up getting your problem solved? The best way to handle this is usually to just install packages into the user library (which is the default). Then just print(.libPaths()) in your script before loading packages if you are concerned about weirdness happening, just to be sure the library paths are set to what you expect.

Hopefully, munging the libpaths manually is not actually necessary and could cause unexpected problems for yourself! Definitely let us know if you are still having trouble!

One mental model to have here is that you are trying to cook a dish, the packages are your ingredients, and .libPaths() is the address of your pantry. Usually, leaving .libPaths() alone is fine, because any ingredients will be added to or available from that same default pantry when you install.packages().