### ** Examples ... Error: could not find function building packages in R
Asked Answered
R

1

1

I have created a package named test and I have a function named lad inside it. When I build it and after check it with cran=TRUE, I receive the following error. Any idea what's going wrong?

* checking examples ... ERROR
Running examples in 'test-Ex.R' failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: lad
> ### Title: LAD
> ### Aliases: lad
> 
> ### ** Examples
> 
> lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")
Error in (function (par)  : object 'sum.abs.dev' not found
Calls: lad -> optim -> <Anonymous>
Execution halted
Error: Command failed (1)

Here's the code inside power.R function which is in the R folder of my test package.

sum.abs.dev<-function(beta,a=land,b=farm)
{
  total<-0
  n<-length(b)
  for (i in 1:n)
  {
    total <- total + abs(b[i]-beta[1]-beta[2]*a[i])
  }
  return(total)
}

#' LAD
#' 
#' Minimize the sum of absolute deviations from the residuals
#' @param y A value showing the first column of the data frame
#' @param x A value showing the second column of the data frame
#' @param data A value showing the link to the data frame in CSV format
#' @return The square of the input
#' @export
#' @examples 
#' lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")

lad <- function(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")
{
  library(stats)
  dat <- read.csv(data)
  dat.x <- dat[[x]]
  dat.y <- dat[[y]]
  fit<-lm(dat.y~dat.x)
  beta.out=optim(fit$coefficients,sum.abs.dev)$par

  return(beta.out)
}

Here are the commands I ran so far before check:

build("/Users/mona/test")
build("/Users/mona/test", binary=TRUE)
check("/Users/mona/test", cran=FALSE)

When I click Build & Reload I don't receive any problem and here's what I receive:

> library(test)

Attaching package: ‘test’

The following object is masked _by_ ‘.GlobalEnv’:

    lad

enter image description here

Riotous answered 1/5, 2014 at 21:25 Comment(7)
you should create another piece of roxygen documentation like you did for sum.abs.devMichale
Thank you. But can you please take a look at my updated question?Riotous
I kept everything the same as you have, put sum.abs.dev inside of lad, right above the beta.out, and changed sum.abs.dev<-function(beta,a = dat.x,b = dat.y). Does that work?Michale
Is this homework? Regarding asking people on SO to do your homework, please read this.Ofay
I know I should have had self-study tag but the other 4 tags beside R were really important! I wanted to delete but it didn't let me! So I don't know how to delete it!Riotous
If you had bothered to read the link in my comment, you would have found that (1) the "homework tag is deprecated", and (2) "Be aware of school policy. If your school has a policy regarding outside help on homework, make sure you are aware of it before you ask for / receive help on Stack Overflow.". So what is your school policy? "you must write programs by yourself". And you keep posting homework questions...Good luck!Ofay
I have asked my instructor and he has encouraged us highly to ask for help in stackoverflow when we are stuck.Riotous
A
1

The following achieves what you want, I believe:

lad <- function(y, x, data) {
  dat <- setNames(read.csv(data)[, c(x, y)], c('x', 'y'))
  sum.abs.dev <- function(beta, data) {
    with(data, sum(abs(y - beta[1] - beta[2] * x)))
  }
  fit <- lm(y ~ x, dat)
  optim(par=coef(fit), sum.abs.dev, data=dat)$par
}

lad(y = "farm", x = "land", data="FarmLandArea.csv")

#  (Intercept)            x 
# -605.2293682    0.3642028 
Avner answered 2/5, 2014 at 2:18 Comment(9)
Thank you jbaums this gives the right answer for the output. However when I run the check with cran=TRUE I receive the error I have added in the updated post Running examples in 'test-Ex.R' failed do you know where I should add the file test-Ex.R ? Also what should I add there? just this : lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv") ? thanks for the help.Riotous
What's is the example?Avner
on a side note how should I create a plot with the numbers given by the lad? Can you give me a hint?Riotous
I created a whole new project and now I have a new problem this time with DESCRIPTION file apparently. It'd be great if you check here: #23420823Riotous
Side note: depends what you want to plot. I imagine the points, along with the line described by the parameters estimated by optim? If so, try dat <- read.csv('FarmlandArea.csv'); plot(dat$farm ~ dat$land); pars <- lad(y = "farm", x = "land", data="FarmLandArea.csv"); abline(pars[1], pars[2]).Avner
The question is where in @example should I write these lines? I want to write these lines just when I run example(lad) not inside the lad function.Riotous
Should I write it inside jalal.Rd here ? \examples{ pars<-lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv"); dat <- read.csv(data); plot(dat$farm ~ dat$land); abline(pars[1], pars[2]) }Riotous
The way you've written the example in roxygen looks fine to me. If you start a fresh workspace and manually load just the functions that are in your package, does the example work?Avner
No, every time I run it, it overwrites whatever I write in the .Rd file.Riotous

© 2022 - 2024 — McMap. All rights reserved.