-1

Suddenly, R Studio has been giving me hard-to-reproduce errors in which it references old variable names and code that no longer exist in my script. Here is a short replicable example:

LoadInfectionsDrugs <- function() {
}

meta <- read.csv("tblhctmeta.csv")
infections <- read.csv("tblInfectionsCidPapers.csv")
drugs <- read.csv("tbldrug.csv")

workspace <- "w4v3.RData"
load(workspace)
print(head(input))
print(head(meta))
print(head(drugs))
print(head(infections))
LoadInfectionsDrugs() #This line generates the error - loads and prints correctly if this is commented out.

If I comment out the last line, it runs and prints just fine. If I leave the last line in, though, I get the error "Error in unique(input$PatientID): argument "input" is missing, with no default.

Thing is, the LoadInfectionsDrugs function used to (and normally does) have several arguments, including "input", which is a data frame with a column called PatientID. So this clearly seems to be referencing old code.

If I comment out the "load(workspace)" and "print(head(input))" lines, it also runs fine.

So my hypothesis is that, in my main code, when I call "save.image(workspace)", it is saving not only objects, such as the dataframes I'm creating, but also functions. Then, when I change the function in code, but load the workspace, it is replacing the functions I've defined in code with the definitions as they were when I last saved the workspace.

I didn't know that .RData files saved function definitions. I thought they just saved variables. Is there a way to save only the objects you've defined (i.e. dataframes), and not the functions?

  • 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 and desired output that can be used to test and verify possible solutions. If we don't have data we can't run and test the code. – MrFlick Dec 09 '21 at 04:02
  • Unfortunately, I don't know how to reproduce this error in a simpler form than copy/pasting my (large) code file. I don't actually think the problem is being generated by my code itself. I think the issue may be with R Studio or something.It's making references to old code. It's been referencing variable names that I used to have in the code but no longer do, as well as whole lines of code that were then commented out. – AllAmericanBreakfast Dec 09 '21 at 04:19
  • 1
    In my experience it's far more likely that you are not running the code you think you are rather than RStudio somehow remembering old versions of code. If the code is too large to simplify to make a reproducible example that's not a good sign. You want to take out as much code as possible but still trigger the exact same error. Or start from scratch in a new file and slowly copy over pieces till you trigger the same error again. – MrFlick Dec 09 '21 at 04:22

1 Answers1

0

https://www.oreilly.com/library/view/r-in-a/9781449377502/ch12s02.html

Today I discovered the difference between saving objects and saving my entire workspace. If you save the workspace, you save everything, including function definitions and such. If you change the code and then load the workspace, R will start using your old functions, not the ones you've got in your most recent version of the script.