User-function, formal object defined but not found by code

The business I'm working on is constantly analyzing ELISA (immunoassay), and therefore I program a function that accepts a csv machine read version for optical densities and performs a statistical regression called 4PL analysis.This is essentially a 4pl function for dummies, which uses drc package. I have most of the code written, but now I'm trying to put it in a function format (it works fine outside the function structure).

Here is my problem. I define the forms () of my function as follows:

elisa<-function(file="data.csv",wd="~/Desktop",standards=c(1,2),orient=horizontal, limit=TRUE,graph.4pl=FALSE,Conc.graph=FALSE){ body of function} 

What is now in other forms is not particularly important, but I ran into two problems. Here is the code for the first part of the block.

 rm(list=ls()) setwd(wd) library(drc);library(reshape2);library(ggplot2) data<-read.csv(file,head=TRUE, colClasses=c("character")) 

If the community in its wisdom thinks I need to include more, I will, but leave it now.

Problem:

 elisa("Mock data.csv") Error in setwd(wd[1]) : object 'wd' not found 

This error will appear. As you can see, wd IS is defined

 formals(elisa) $file [1] "data.csv" $wd [1] "~/Desktop" $standards c(1, 2) $orient horizontal $limit [1] TRUE $graph.4pl [1] FALSE $Conc.graph [1] FALSE 

Also, if I predefine wd as "~/Desktop" in the global environment, the error for wd goes away, but I get this

 wd<-"~/Desktop" elisa("Mock data.csv") Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection 

Either I'm completely immersed in how I define my forms, or I come across some very strange arguments that convey problems. Any ideas?

+5
source share
1 answer

The problem is that you delete all your formats with the first line, rm(list=ls()) .

For instance:

 f <- function(a=1) { rm(list=ls()) print(a) } f() ## Error in print(a) : object 'a' not found 

When you define wd in a global environment (i.e., on the stack above your function), your function will work (at least to this point), because rm(list=ls()) will only remove variables in your current environment ( i.e. stack of function calls). In this case, your function will use the values ​​for the variables defined in the global environment.

+6
source

All Articles