0

When using R studio I get an error trying to use for-loops

for(i in 1:4){print(i)} Error in check_reserved(for_var_name) : could not find function "check_reserved"

Anyone any clue how to solve this? I've updated R, RStudio and restarted the session as well

session info:

R version 4.1.1 (2021-08-10) Platform: x86_64-apple-darwin17.0
(64-bit) Running under: macOS Big Sur 10.16

Matrix products: default

LAPACK:
/Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

attached base packages: [1] stats graphics grDevices utils datasets
methods base

loaded via a namespace (and not attached): [1] compiler_4.1.1
tools_4.1.1

Thanks!

r2evans
  • 141,215
  • 6
  • 77
  • 149
nwb
  • 3
  • 1
  • A good start would be to look for packages from among [`"cran" "check_reserved"`](https://www.google.com/search?q=%22cran%22+%22check_reserved%22) and seeing what seems familiar (based on whatever work you are doing. Then, load that package with `library(packagename)`. – r2evans Oct 22 '21 at 13:56
  • But for should be a function from the base package so nothing should have to be loaded to use the function – nwb Oct 22 '21 at 14:09
  • The error has nothing to do with the call to `for`, it says it could not find the function `"check_reserved"`. – r2evans Oct 22 '21 at 14:13
  • I was assuming that there was code elsewhere that you did not include in the question. I've reopened. – r2evans Oct 22 '21 at 14:15
  • Have you ever used the `magicfor` package? I don't see it in your session info, but [`magicfor::magic_for`](https://github.com/hoxo-m/magicfor/blob/master/R/magic_for.R#L29) appears to be making that call. Check your `.Rprofile` and other startup files to see if somehow you are using the magicfor package anywhere else. (This includes any `.Rdata` files that R/RStudio are magically loading for you. I find the practice of using those files problematic, often for issues similar to this.) – r2evans Oct 22 '21 at 14:19
  • If you find something, restart R and RStudio (again, sorry) and see if that resolves it. – r2evans Oct 22 '21 at 14:19
  • 1
    BTW, this was discussed [here](https://community.rstudio.com/t/error-with-for-loop-from-the-globalenv/80191) as well. The thread went stagnant, so it's unclear if it was resolved; I suggest that if you have `magicfor` installed, unless you know you really need it ... *uninstall it*. While it might be a fun package for helping with some interactive work, in my opinion the premise of changing the behavior of the `for` primitive is flawed and prone to too many likely unintended side-effects (like this). – r2evans Oct 22 '21 at 14:28
  • 1
    That was indeed the problem, many thanks! And definitely uninstalled magicfor! – nwb Oct 22 '21 at 14:38

1 Answers1

0

The error is because of a previous use of magic::magic_for which was somehow being brought back into the environment. The apparent intent of the function (no changes since 2016) is to allow a for loop to store the results of a modified function, something like:

library(magicfor)  # Load library
magic_for(print)   # Call magic_for()

for (i in 1:3) {
  squared <- i ^ 2
  print(squared)
}
#> The loop is magicalized with print().
#> [1] 1
#> [1] 4
#> [1] 9

magic_result_as_vector()  # Get the result
#> [1] 1 4 9

It appears that the "magicalization" of the print function was preserved (perhaps in .Rdata), but on a session restart, the magicfor package was not available (or just not loaded). I'd think that if the magicfor package is truly required for a workflow, then (1) make sure it's loaded, and (2) submit a bug to the author so that it better accommodates this scenario.

Lacking that, my suggestion for now:

  • do not load .Rdata files ("saved session" or similar); many feel it is far better to have a reproducible workflow and regenerate the intermediate/final structures you need vice loading them; the exception to this suggestion would be for long-running processes, but even then I think the better case is to explicitly save/cache just those values, not the entire session;
  • similarly, check .Rprofile and other R startup files to remove any mention of magicfor;
  • perhaps nuclear, but a community thread mentioned uninstalling the magicfor package; it's unclear if that was part of the resolution or not, though it seems unlikely if the underlying cause was a stale reference in an .Rdata file.
r2evans
  • 141,215
  • 6
  • 77
  • 149