How to load package from different location when using the command prompt?

Hi,

I am making a shiny app. I want to install and load my packages from a different location. I use the .Renviron file to set the R_LIBS_SITE variable. Explained in this topic.

When I run the script with a .bat file. It works. But when I run the lines of the .bat file in a command prompt, it 'igonores' the .Environ file (I think) and choses the wrong location for the packages.

Can somebody help me with this problem?

Here some code and the files that are present in the app folder.

File 1: Launchapp.R

setwd("O:/.../Test")

message(paste0("Working directory: ",getwd()))
message(paste0("R_LIBS_USER = "),Sys.getenv("R_LIBS_USER"))
message(paste0("Dir exists = ",dir.exists(Sys.getenv("R_LIBS_USER"))))
message(paste0("R_LIBS = "),Sys.getenv("R_LIBS"))
message(paste0("Dir exists = ",dir.exists(Sys.getenv("R_LIBS"))))
message(paste0("Libpath[1] = ",.libPaths()[1]))

library(shiny,lib.loc = .libPaths()[1])

shiny::runApp(appDir = "app.R",launch.browser = TRUE)

File 2: App.R
Old Faithful Geyser Data app.

File 3: .Renviron

R_LIBS=O:/location1
R_LIBS_SITE=O:/location1
R_LIBS_USER=O:/location1

File 4: Test.bat

set R= "C:\Program Files\R\R-4.1.3\bin\i386\R" --no-restore --no-save
%R% < "O:\...\Test\Launchapp.R" > Logfile.log
pause

Result running app from .bat file:

Working directory: O:/.../Test
R_LIBS_USER = O:/location1
Dir exists = TRUE
R_LIBS = O:/location1
Dir exists = TRUE
Libpath[1] = O:/location1

Listening on http://................

Result running app from command prompt:

Working directory: O:/.../Test
R_LIBS_USER = C:\Users\name_user\Documents/R/win-library/4.1
Dir exists = TRUE
R_LIBS =
Dir exists = FALSE
Libpath[1] = C:/Users/name_user/Documents/R/win-library/4.1
Error in library(shiny, lib.loc = .libPaths()[1]) :
  there is no package called 'shiny'
Execution halted

Thank you in advance!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.