Command Lines Error in Rstudio Console

Command Lines error in Rstudio console

In "An Introduction to R" manual (help.start()), Section 1.8:

Command lines entered at the console are limited to about 4095 bytes (not characters).

[footnote] Some of the consoles will not allow you to enter more, and amongst those which do some will silently discard the excess and some will use it as the start of the next line.

Based on the comments, there does indeed seem to be some variation across consoles in how this is handled. From the OP, we can conclude that RStudio does not allow you to enter more bytes. Currently, RStudio does not acknowledge or intend to address this.

I also found a 2006 conversation in the R devel mailing list where Brian D. Ripley explained the issue and the documentation above. It looks like the limit has changed since then (when it was 1024 bytes).


It's worth noting that it works fine in a sourced script (even when "hard coded"). Only the REPL seems to be at fault.

How to get R script line numbers at error?

This won't give you the line number, but it will tell you where the failure happens in the call stack which is very helpful:

traceback()

[Edit:] When running a script from the command line you will have to skip one or two calls, see traceback() for interactive and non-interactive R sessions

I'm not aware of another way to do this without the usual debugging suspects:

  1. debug()
  2. browser()
  3. options(error=recover) [followed by options(error = NULL) to revert it]

You might want to look at this related post.

[Edit:] Sorry...just saw that you're running this from the command line. In that case I would suggest working with the options(error) functionality. Here's a simple example:

options(error = quote({dump.frames(to.file=TRUE); q()}))

You can create as elaborate a script as you want on an error condition, so you should just decide what information you need for debugging.

Otherwise, if there are specific areas you're concerned about (e.g. connecting to a database), then wrap them in a tryCatch() function.

Make execution stop on error in RStudio / Interactive R session

I don't think that there is a way to prevent RStudio from running all the lines, when you select a section and press Ctrl+Enter. Rstudio is just running one line after the other. Even if stopifnot() is called inside of a function, all the lines after that function call will still be evaluated.

If your goal is simply to be informed when something goes wrong, before a lot of code is run in vain, maybe you could define a function similar to stopifnot() that will just go into an endless loop, if there is an error. You could then press Esc or the Stop-Button in RStudio to interrupt the program. Something like this:

waitifnot <- function(cond) {
if (!cond) {
message(deparse(substitute(cond)), " is not TRUE")
while (TRUE) {}
}
}

Now, you can run your example code with this function:

x <- 'test'
waitifnot(is.numeric(x))
print('hello world')

As expected, hello world is never printed. You will get an error message, telling you that something went wrong, and then the program will wait until you abort it manually.

This won't work well in any situation other than interactive mode. To avoid unpleasant situations, you could also let the function react differently, if it is not used in interactive mode, for instance like this:

waitifnot <- function(cond) {
if (!cond) {
msg <- paste(deparse(substitute(cond)), "is not TRUE")
if (interactive()) {
message(msg)
while (TRUE) {}
} else {
stop(msg)
}
}
}

This function will go into an endless loop only if run in interactive mode. Otherwise, it will simply abort execution by calling stop(). I have checked that this works as expected with Ctrl+Enter or the Source button in RStudio (endless loop) and with Rscript on the Bash command line (abort of the program).

How to show error line number in R studio

First, have a look at ?traceback.

There are many ways to debug R code/script. This is only one example.

In RStudio, from the Debug drop down menu option On Error, choose Error Inspector for (what I think is) the easiest debug mode for finding the line number of an error/bug. You can also choose Break in Code to show the highlighted line of an R script that contains the error.

When the error occurs, you can click either of the small areas marked Show Traceback and Rerun with Debug. The screen shot below shows the effect of clicking "Show Traceback" (hence it now says "Hide Traceback"). It tells me that the error occurred when R attempted to call sample (the third call). LENGTH had not yet been defined.

Sample Image

Logging console history with errors in R or Rstudio

A possible solution would be to use addTaskCallback or the taskCallbackManager with a function that writes each top-level command to your database. The callback will only fire on the successful completion of a command, so you would still need to call a logging function on an error.

# error handler
logErr <- function() {
# turn logging callback off while we process errors separately
tcbm$suspend(TRUE)
# turn them back on when we're done
on.exit(tcbm$suspend(FALSE))
sc <- sys.calls()
sclen <- length(sc) # last call is this function call
if(sclen > 1L) {
cat("myError:\n", do.call(paste, c(lapply(sc[-sclen], deparse), sep="\n")), "\n")
} else {
# syntax error, so no call stack
# show the last line entered
# (this won't be helpful if it's a parse error in a function)
file1 <- tempfile("Rrawhist")
savehistory(file1)
rawhist <- readLines(file1)
unlink(file1)
cat("myError:\n", rawhist[length(rawhist)], "\n")
}
}
options(error=logErr)
# top-level callback handler
log <- function(expr, value, ok, visible) {
cat(deparse(expr), "\n")
TRUE
}
tcbm <- taskCallbackManager()
tcbm$add(log, name = "log")

This isn't a complete solution, but I hope it gives you enough to get started. Here's an example of what the output looks like.

> f <- function() stop("error")
f <- function() stop("error")
> hi
Error: object 'hi' not found
myError:
hi
> f()
Error in f() : error
myError:
f()
stop("error")

Unable to run edit() command in RStudio

You may be missing or have to reinstall XQuartz 2.7.11, install it first then try running edit command.If it doesn't work then do the following.(This happens sometimes during an update)

1) Find the location of libcario.2.dylib using locate command from terminal/iterm-2 . Use command locate libcario.2.dylib, you should receive something similar with this location: /usr/X11/lib/libcario.2.dylib. Someone can use cd,find to locate the location as well.

2) If the location is found then use ln -s to create a link like below:
sudo ln -s /opt/X11 /usr/X11



Related Topics



Leave a reply



Submit