[R] How to write an error to output

jim holtman jholtman at gmail.com
Wed Oct 16 13:58:18 CEST 2013


Will this work for you:


mydat = data.frame(A = c(19, 20, 19, 19, 19, 18, 16, 18, 19, 20), B =
c(19, 20, 20, 19, 20, 18, 19, 18, 17, 16))

if (length(mydat$A) > 10)

{
write.csv(data.frame(error = "A has length more than 10"),
'result.csv', row.names = FALSE)
stop("A has length more than 10")
}else

if (max(mydat$B) > 18)
{
write.csv(data.frame(error = "max B exceeds limit"), 'result.csv',
row.names = FALSE)
stop("max B exceeds limit")
}else

{result = mydat$A + mydat$B

    if (length(result) > 0)

{
         write.csv(data.frame(result = result), 'result.csv', row.names = FALSE)
         }
}

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Wed, Oct 16, 2013 at 7:01 AM, Katherine Gobin
<katherine_gobin at yahoo.com> wrote:
> Dear R forum,
>
> The example below is just an indicative one and I have constructed it. My real life data and conditions are different.
>
> I have a data.frame as given below
>
> mydat = data.frame(A = c(19, 20, 19, 19, 19, 18, 16, 18, 19, 20), B = c(19, 20, 20, 19, 20, 18, 19, 18, 17, 16))
>
> if (length(mydat$A) > 10)
>
> {
> stop("A has length more than 10")
> }else
>
> if (max(mydat$B) > 18)
> {
> stop("max B exceeds limit")
> }else
>
> {result = mydat$A + mydat$B
>
>     if (length(result) > 0)
>
> {
>          write.csv(data.frame(result = result), 'result.csv', row.names = FALSE)
>          }
> }
>
> # -----------------------------------------------------------------
>
> When i execute above code, I get message
>
> Error: max B exceeds limit
>
> If all conditions are met, obviously I am getting an output as result.csv
>
> If result.csv is generated, I am able to capture and show the output in front end. However, if the process couldn't be run owing to the violation of conditions, the error is produced. How do I capture this error (and express it as csv file) so that I can show it as a comment in front end.
>
> Kindly guide.
>
>
> Katherine
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list