0

I know this has probably been answered a million times, but nothing I seem to do works.

I am running a function that I created, the function is stored in a R script, and I am sourcing and running the function in Rmarkdown.

example <- function(){
EP <- read_excel("filepath")
EP <- Project_EP_Rename(EP)
}

Error in MyPackage::Project_EP_Rename(EP) : object 'EP' not found.

I have tested by putting print(exists(EP)) right in between these lines and that printed TRUE. I am calling library(MyPackage) in the RMarkdown, and this script is located in a subdirectory of the Project file, so I thought that maybe the package wasn't being called. I check using print("MyPackage" %in% .packages()), reran and that came but true. I even tried changing the code to

EP <- MyPackage::Project_EP_Rename(EP)

That still didn't work. I ended up importing the file in the rmarkdown, and then running it through the function example(EP), and changed the code to this, and that worked.

example <- function(datafile){
EP <- datafile
EP <- Project_EP_Rename(EP)
}

Why exactly is this happening? I am very confused.

Sam Edeus
  • 41
  • 6
  • I was having a tough time following, but it seems like `read_excel()` is failing so then when it goes to the next line the object `EP` does not exist and it then kicks the error out. –  Nov 14 '19 at 22:08
  • I used print(exists(EP)) to see if it existed before running EP <- Project_EP_Rename(EP)), and it printed true. I should also state that if I run the code line by line it works perfectly fine. Its only when I call it as a function does it throw an error. – Sam Edeus Nov 14 '19 at 22:10
  • 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. – MrFlick Nov 14 '19 at 22:13
  • 1
    Second the reproducible example. Only other thing I can think of is that `MyPackage::Project_EP_Rename(EP)` itself is trying to use an object named `EP` that does not exist. So the function definition has named that single parameter something else, not `EP`. `Project_EP_Rename <- function(Not_EP) { ... tries to reference EP ...}` –  Nov 14 '19 at 22:23

1 Answers1

0

You created a function called example and not Project_EP_Rename in your example. But let's assume you have the specification of the function somewhere else in your script and Project_EP_Rename has one argument which is the input object (called EP in your case). If you don't create the EP object before you run the Project_EP_Rename correctly says object 'EP' not found. Probably, running the example function instead of Project_EP_Rename would work as it first creates the EP object by EP <- read_excel("filepath") and then executes the Project_EP_Rename function.

By the way, in your case I don't see any reason why to create example function with no argument like you have at the beginning.

opplatek
  • 23
  • 3