0

I have to separate files, one to start a simulation (the main file) and a file with the required functions.

In the main file CalcPriceSystem is called:

priceSystemInit <- CalcPriceSystem(df.firmDistribution, shopMatrixInit, utilityMatrix, 
                                 prices, initialPrice, repetitions,
                                 quantity, profit)

In the function file I define the function CalcPriceSystem:

CalcPriceSystem <- function(df.firmDistribution, shopMatrix, utilityMatrix, prices, initialPrice, repetitions , quantity , profit, ...){
  response.df <- data.frame(matrix(, nrow=repetitions, ncol=0))
  for(i in 1:length(unique(df.firmDistribution[,1]))){
    competitors <- which(unique(df.firmDistribution[,1])[i]==df.firmDistribution[,1])
    response.df <- cbind(response.df, BestResponsePar(utilityMatrix[,competitors], shopMatrix[,unique(df.firmDistribution[,1])[i]], 
                                   prices, initialPrice[competitors], repetitions, quantity , profit ,
                                   #shopMatrix[,df.firmDistribution[1,1]] value where firm i resides
                                   marketNumber = unique(df.firmDistribution[,1])[i]))
  }
  #Order the response alphabetically
  response.df <- response.df[,order(names(response.df))] 
  #Rearrange PriceLevelMarket variables to the last columns
  response.df <- response.df[,c(names(response.df)[-grep("Price",names(response.df))], 
                                names(response.df)[grep("PriceLevel",names(response.df))])]
  return(response.df)
}

If I source the main file before I source the function file there is no error reported and computation works fine. It works also fine if I write the functions into the main file, but I'd like to place them into a separate one.

And now to the strange behavior: If I source the function file BEFORE the main file, R throws an unused argument error. Even if the R Environment (data, functions) is exactly the same as above.

Error in CalcPriceSystem(df.firmDistribution, shopMatrixInit, utilityMatrix,  : 
  unused arguments (initialPrice, repetitions, quantity, profit)

Why is that?

Thanks

Carsten Oppitz
  • 132
  • 2
  • 11
  • You must have multiple definitions of `CalcPriceSystem`. Are you trying this in a clean R session? The "main" file should throw the error "could not find function" if you try to call a function that does not exist yet. This problem does not really seem possible in the way you describe it. Please take the time to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that we can copy/paste and run to re-create the problem ourselves. – MrFlick Oct 30 '14 at 14:14
  • @MrFlick Thank you for your hint of multiple definitions! I still had a source reference in the "main" file referring to an outdated "function" file and not to the correct function file. Correcting the source reference to the "function" file solved the issue. – Carsten Oppitz Oct 31 '14 at 12:20

0 Answers0