0

I am having some problems in R. Below is my code. It worked fine until recently and now i get the error:

Error in list(test_drive_enquiries, TV.spend, Display.spend, Social.spend) : invalid 'envir' argument

Here is my code:

library(caret)
# Data partition ----------------------------------------------------------

set.seed(222)
ind<-sample(2,nrow(mtcars), replace=T, prob=c(0.7,0.3))
train<-mtcars[ind==1,]
test<-mtcars[ind==2,]



# Custom control parameters for Machine Learning -----------------------------------------------
# 
custom<-trainControl(method="repeatedcv",
                     number=10,
                     repeats=5, 
                     verboseIter = T)
###Models


set.seed(1234)
model<-train(mpg~wt+qsec,
                        train,
                        method='lm',
                        trControl=custom)
summary(model$finalModel)

The error occurs at the model building stage. This same error also occurs if I open a different Rmarkdown document which has a different dataset, with a different regression model.

Also, I can't seem to clear the environment with rm(list=ls())

I get the following error: Error in rm(list = ls()) : unused argument (list = ls())

MrFlick
  • 195,160
  • 17
  • 277
  • 295
awz1
  • 419
  • 2
  • 12
  • Have you tried this in a fresh R session? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Did anything else happen very recently? Did you update R or install any new packages? – MrFlick Aug 05 '20 at 23:03
  • 1
    For part 2 of your question (and do be aware that multipart questions are not considered well-formed on SO) .... perhaps you have over-written the `rm` function. I'm not sure how you would get rid of the over-written value if it were defined in a manner that didn't accept function names to be remove. When you type "rm" at the console do you see an argument list that looks like `function (..., list = character(), pos = -1, envir = as.environment(pos), inherits = FALSE)` ? – IRTFM Aug 05 '20 at 23:04
  • I have added a reproducible example. I get the same error with the mtcars dataset – awz1 Aug 05 '20 at 23:09
  • 1
    Well, I don't get any error. So I suggest quitting without saving. Remove any existing ,Rdata files and restart with a fresh session. – IRTFM Aug 05 '20 at 23:10
  • Typing in rm i get the following function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, pattern, sorted = TRUE) { if (!missing(name)) { pos <- tryCatch(name, error = function(e) e) if (inherits(pos, "error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) warning(gettextf("%s converted to character string", sQuote(name)), domain = NA) – awz1 Aug 05 '20 at 23:10
  • I cannot replicate your error running the code above. Again, was this in a new R session for you? If not, what does `sessionInfo()` show and what does `conflicts(detail=TRUE)` show. – MrFlick Aug 05 '20 at 23:12
  • Some package or code has over-written the stock version of `rm` and removed it's `list` argument. Do remove any .Rdata file and then show all the library calls. At the moment your error is not reproducible. – IRTFM Aug 05 '20 at 23:12
  • You can try `base::rm(list = ls())`, but it general, it's [not recommended to use `rm(list=ls())`](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/). It doesn't reset your environment like people think it does. – MrFlick Aug 05 '20 at 23:14
  • conflicts(detail=TRUE) gives me $.GlobalEnv [1] "list" "rm" $`package:methods` [1] "body<-" "kronecker" $`package:base` [1] "body<-" "kronecker" "list" "rm" – awz1 Aug 05 '20 at 23:18
  • sessionInfo() R version 3.6.3 (2020-02-29) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363) Matrix products: default locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.6.3 tools_3.6.3 – awz1 Aug 05 '20 at 23:19
  • Thank you problem seems to have resolved with the base::rm(list = ls()),. Any ideas why this was happening in the first place? – awz1 Aug 05 '20 at 23:27
  • Voting to close. The questioner obviously has overwritten the key functions `list`, `rm` and `body<-`, so there's not a lot of value in trying to help him out unless he first fixes those key aspects of R programming. He's been given specific advice on how to do that which he has so far not chosen to follow. – IRTFM Aug 05 '20 at 23:28
  • So you "solved" the problem of the corrupted `rm` but if you have an overwritten version of the `list` function we cannot possibly help you. Fix your installation of R! – IRTFM Aug 05 '20 at 23:30
  • Somehow you created versions of `list()` and `rm()` in your global environment which took over for the "true" functions causing the error. Functions in R aren't protected so you can overwrite existing (even critical) functions if you are not careful. Remove those "bad" versions and you should be fine. – MrFlick Aug 06 '20 at 01:38

0 Answers0