question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In R-4.2.0 on windows, package 'stats' in options("defaultPackages") was not found.

See original GitHub issue

Using R version 4.2.0 on Windows, this prior issue with loading stats.dll https://github.com/rdotnet/rdotnet/issues/127 seems to have resurfaced.

Here, I show that rnorm works with R 4.0.2 but fails with R 4.2.0.

Works with old R version 4.0.2

#r "nuget: R.NET"
open RDotNet

REngine.SetEnvironmentVariables("c:/program files/r/r-4.0.2/bin/x64", "c:/program files/r/r-4.0.2")
let  engine = REngine.GetInstance()
engine.Evaluate("set.seed(1);rnorm(1)").AsNumeric()
// val it: NumericVector = seq [-0.6264538107]

Fails with R version 4.2.0

#r "nuget: R.NET"
open RDotNet

REngine.SetEnvironmentVariables("c:/program files/r/r-4.2.0/bin/x64", "c:/program files/r/r-4.2.0")
let  engine = REngine.GetInstance()
(*
During startup - Warning message:
package 'stats' in options("defaultPackages") was not found 
val engine: REngine
*)
engine.Evaluate("set.seed(1);rnorm(1)").AsNumeric()
(*
Error in rnorm(1) : could not find function "rnorm"
In addition: Warning message:
'memory.limit()' is no longer supported
RDotNet.EvaluationException: Error in rnorm(1) : could not find function "rnorm"

   at RDotNet.REngine.Parse(String statement, StringBuilder incompleteStatement, REnvironment environment)
   at RDotNet.REngine.Defer(String statement, REnvironment environment)+MoveNext()
   at System.Linq.Enumerable.TryGetLast[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.LastOrDefault[TSource](IEnumerable`1 source)
   at RDotNet.REngine.Evaluate(String statement, REnvironment environment)
   at <StartupCode$FSI_0005>.$FSI_0005.main@()
Stopped due to error
*)

> engine.Evaluate("library(stats)").AsCharacter()
(*
Error: package or namespace load failed for 'stats' in inDL(x, as.logical(local), as.logical(now), ...):
 unable to load shared object 'C:/Program Files/R/R-4.2.0/library/stats/libs/x64/stats.dll':
  LoadLibrary failure:  The specified module could not be found.
RDotNet.EvaluationException: Error: package or namespace load failed for 'stats' in inDL(x, as.logical(local), as.logical(now), ...):
 unable to load shared object 'C:/Program Files/R/R-4.2.0/library/stats/libs/x64/stats.dll':
  LoadLibrary failure:  The specified module could not be found.

   at RDotNet.REngine.Parse(String statement, StringBuilder incompleteStatement, REnvironment environment)
   at RDotNet.REngine.Defer(String statement, REnvironment environment)+MoveNext()
   at System.Linq.Enumerable.TryGetLast[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.LastOrDefault[TSource](IEnumerable`1 source)
   at RDotNet.REngine.Evaluate(String statement, REnvironment environment)
   at <StartupCode$FSI_0006>.$FSI_0006.main@()
Stopped due to error
*)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
lrasmuscommented, May 13, 2022

Workarounds that are available:

  1. Add C:\Program Files\R\R-4.2.0\bin\x64 to the PATH environment variable using the standard Windows System Properties > Environment Variables
  2. Update C:\Program Files\R\R-4.2.0\library\base\R\Rprofile (per earlier comment)
  3. In calling application, immediately call engine.Evaluate("Sys.setenv(PATH = paste(\"C:/Program Files/R/R-4.2.0/bin/x64\", Sys.getenv(\"PATH\"), sep=\";\"))"); after initializing REngine (per earlier comment)
3reactions
lrasmuscommented, May 13, 2022

Awesome find @nhirschey!!! Thanks so much. rdotnet doesn’t itself explicitly call Rcmd_environ. I tried editing that file to update the PATH, and it didn’t appear to pick up my changed path. Even going so far as to remove that file entirely didn’t seem to make a difference for when rdotnet runs. I think that is only used for R CMD invocation.

But along those lines, I think the PATH changes are being made in C:\Program Files\R\R-4.2.0\library\base\R\Rprofile. At the bottom of that file, there is code to update the path with RTools. I can also edit it as such and have it work:

local({
    setRtools42Path <- 0
     setRtools42Path <- 1

    if (setRtools42Path) {
        rthome <- Sys.getenv("RTOOLS42_HOME", "c:/rtools42")
        rtpath <- paste0(rthome, "/x86_64-w64-mingw32.static.posix/bin;",
                         rthome, "/usr/bin")
        path <- Sys.getenv("R_CUSTOM_TOOLS_PATH", rtpath)
	## ADDED rbin
	rbin <- paste0(Sys.getenv("R_HOME"), "/bin/x64")
        Sys.setenv(R_RTOOLS42_PATH = rtpath)
	## UPDATED
        Sys.setenv(PATH = paste0(rbin, ";", path, ";", Sys.getenv("PATH")))
    }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Stat package will not load after installing RTools - General
Unable to load shared object on startup in RStudio only (R4.0.0 / RStudio ... Warning message: package 'stats' in options("defaultPackages") was not found...
Read more >
package 'stats' in options("defaultPackages") was not found ...
Go to the package window and see if the stats is checked or not. If it's not checked, check it and install/or update...
Read more >
R Installation and Administration
Replacing the case-changing functions was new in R 4.1.0 and the default on macOS (and on Windows since R 4.2.0). Replacement of the...
Read more >
R Broken Even after Reinstall : r/rstats
I am using newest version of r and rStudio on windows. Error: package or ... package 'stats' in options("defaultPackages") was not found.
Read more >
R Installation and Administration - 6 Add-on packages
R CMD INSTALL works in Windows to install source packages. No additional tools are needed if the package does not contain compiled code,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found