Moving .libPath from roaming profile to local drive. Need Help Please.

Hi All,

This is my first post here, and I'm pretty new to R, so please bear with me if I'm asking basic questions.

I'm having an issue with library paths. Until recently I had been getting around it by using
.libPaths("C:/R\ files/tidyverse") or similar to redirect the Studio to where I wanted. For the most part it has been working, albeit a bit clunky and irritating at times.

I'm now trying to create forms using shiny. If run the .libPaths code in the console and run the app, I will be prompted to install shiny. It will not install if I click on the pop-up, but will install if I use install.packages. This prompts a restart prior to installation, and then installs the package on the default roaming (network) profile. Repeat this process with "jsonlite" and any other package that the program thinks is missing and eventually it will run the app. However, as soon as I close the app and make any adjustments to the code and try to run tha app again, I then get the pop-up again prompting me to install shiny. Cue having to do the process above again.

It seems to me that the program is installing the package to the roaming profile and getting confused when the libpath is changed, however the rest of the code won't run if it isn't. Which brings me to my main question...

I want to run everything locally and not have the program referring to the roaming profile at all. I found the following instruction (https://www.accelebrate.com/library/how-to-articles/r-rstudio-library) to change the startup file, by adding a couple of lines of code telling Studio to default to the local library rather then the network library.

# custom start-up code
myPaths <- .libPaths()
myPaths <- c(myPaths[2], myPaths[1])  # switch nexus[1] to C[2]
#myPaths <- c(myPaths, 'C:/CUSTOMPATHNAME') # add new path
.libPaths(myPaths)  # reassigns the path

I applied this as administrator and then checked it. Opening 'Tools', 'Install Packages' it initially shows the network address as the library, but upon hitting the down arrow it shows the local library as the default.

Under user mode, the local library still does not appear as on option within 'Install Packages'.
Have I done something wrong here with my code? Or is it something to do with this bit of code, which I found further up in the start-up file.

## this will break if R is on a network share
.Library <- file.path(chartr("\\", "/", R.home()), "library")
.Library.site <- Sys.getenv("R_LIBS_SITE")
.Library.site <- if(!nchar(.Library.site)) file.path(R.home(), "site-library") else unlist(strsplit(.Library.site, ";"))
.Library.site <- .Library.site[file.exists(.Library.site)]

if(!nzchar(Sys.getenv("R_LIBS_USER")))
    Sys.setenv(R_LIBS_USER=
               file.path(Sys.getenv("R_USER"), "R",
                         "win-library",
                         paste(R.version$major,
                               sub("\\..*$", "", R.version$minor),
                               sep=".")
                         ))

invisible(.libPaths(c(unlist(strsplit(Sys.getenv("R_LIBS"), ";")),
                      unlist(strsplit(Sys.getenv("R_LIBS_USER"), ";"))
                      )))

local({
    popath <- Sys.getenv("R_TRANSLATIONS", "")
    if(!nzchar(popath)) {
        paths <- file.path(.libPaths(), "translations", "DESCRIPTION")
        popath <- dirname(paths[file.exists(paths)][1])
    }
    bindtextdomain("R", popath)
    bindtextdomain("R-base", popath)
    bindtextdomain("RGui", popath)
    assign(".popath", popath, .BaseNamespaceEnv)
})

I hope what I have written above makes sense and isn't full of holes. I'm afraid I don't know R (or any coding language) well enough to know how to fix this problem, but is CONSTANTLY tripping me over, so I'd really like to fix it! Any help would be very much appreciated.

UPDATE:

I found a thread showing how to change the lib path in the library base (start-up) file. I tried this fix as supplied by Marcus...
image
I managed to change the path from the roaming profile to the local R library, then realised that I could not write to this as a user (only admin). So, I made a new local folder C:/R_packages, and updated the startup code to reflect the new location, saved, and tested in user mode. It's still trying to install to C:/ Program Files/R/R_4.0.3/library. I've since returned the startup code to "R_USER", tested (returned to roaming profile) then tried the new library path again and still got this error...
image

I'm so confused. Please help.

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.