Interactively Sourced User Defined Function (UDF) Disappears from Environment When Another UDF is Sourced

Here is a description of the problem. It happens both inside a project and outside a project (outside of a project I have set the working directory).

  1. I create several user defined functions. The code does what I want it to do without errors.
  2. I source one of the functions e.g. load_data()
  3. The function load_data appears in the global environment
  4. I assign a value to a variable using the one of the functions e.g. x <- load_data()
  5. The variable x appears in the global environment
  6. I source another of the functions e.g. load_parameters e.g. y <- load_parameters()
  7. The function load_parameters appears in the global environment
  8. The function load_data disappears from the global environment along with the variable x

When I source all the functions the user defined functions in a single script I get no error messages but only the last user defined function sourced appears in the global environment

When I call all the user defined functions as a group in a single script I get error messages on all but the last user defined function e.g.
Error in load_data() :
could not find function "load_data"

getwd() returns the subdirectory I am working in or the project subdirectory as applicable
searchpaths() returns a list of paths which includes the global environment but does not explicitly include the working directory

I have worked with user define functions before and this has never happened. I recently updated the version of R and RStudio and guess that it is some sort of configuration problem or preference.

Hello Peter,

How do you 'source' your functions?
With base function source I see only functions added and not removed.

Are there only function definitions in the R-files that you 'source'?
No executable statements as rm to get a nice clean environment?

Han:

Thanks for your reply.
I source the user defined function using base "source" i.e. source("udffilename.R")
I clear the workspace out completely before starting.

I have tried uploading screen shots of the RStudio IDE screen but, as a new user, I am for some reason, prohibited from doing so. What you are seeing is what I have always seen...until now.

Peter

Thanks,

Peter

And the contents of such an R-file that you source, does it only contains function definitions?

If not can you show us such an R-file?

If you create an R-file with only the function

zero <- function () {0}

and source it, does the same happens: existing functions no longer exist?

Han:

Again...thanks so much for giving this your attention. I have identified the source of the problem. Your questions motivated me to think about what code was actually in the user define functions and why. The main calling program loads packages and clears the environment with " rm(list = ls()) ". I also had, no good reason, the command " rm(list = ls()) " at the top of each of the user defined function scripts. So every time a UDF was called it cleared the global environment. When I commented out this line in the functions all worked as expected. I am more than a little embarrassed. Thanks for asking the right questions.

Peter

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.