How to show error line number in R studio - r

Is it possible to see the syntax error or runtime error line number (highlight also) generated after running the code in R studio?
I searched the options but couldn't find.

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.

Related

Line magic function `%%pycodestyle` not found

I am using pycodestyle_magic as a linter in Jupyter. I am following the instructions at
https://github.com/mattijn/pycodestyle_magic
But I get error with 1-cell checking be it '%%pycodestyle' or '%%flake8'.
1st ERROR
# 1st CELL
%load_ext pycodestyle_magic
# 2nd CELL
%%pycodestyle
a=1
print(a)
2nd ERROR
# 1st CELL
%load_ext pycodestyle_magic
# 2nd CELL
%%flake8
a=1
print(a)
1st ERROR
I'm basing this on your image you posted for '1st error' and not the inaccurate code you posted:
Note that the error in your first error was saying line magic and not cell magic because it was not looking for cell magic anymore. You can see it will look for cell magic on the first line of a cell by putting in %%fake_magic as a first line and then 2 + 2 as a second line of cell. Running that you'll see UsageError: Cell magic %%fake_magic not found.
Hence, your first 'error' is that you are missing that the first line of the cell where you want to use cell magic becomes special. You cannot have something else on the first line where you want to use cell magic, even if it is commented out. In the image, you have #%%flake8 above %%pycodestyle. If you remove that line, it should work.
2nd ERROR
I'm basing this on your image you posted for '2nd ERROR' and not the inaccurate code you posted:
You've stumbled upon a bug currently involving flake8. (Maybe same or more bugs seem to prevent the %%flake8 magic from working at all for now, see comments.)
The solution/workaround is very similar to the '1st ERROR'. Remove the line you were trying to comment out because it being there is causing an issue.
The reasoning appears to be complex about the way the %%flake8 cell magic appears to work behind the scenes so that you cannot have comments in the code content at this time or it won't work. (And the second time it sees that comment symbol, it throws the error you see.) This bug has been reported here.
But the solution/work-around for now is straightforward. Remove the complexity you added, and see if it works.
In case of the %%flake8 magic, running the demo notebook as shown there doesn't work as shown, and so there is indeed a bug in the current version that was introduced by actually a bug in flake8 that the extension uses, see the specific comment here and the link to the underlying issue over at the flake8 repo. (In fact the extent that it doesn't work may be greater than the notebook reflects because as discussed in the comments, even cells without commented lines fail to report any formatting issues. The demo actually doesn't have any without and so I got distracted noting it only didn't show the buffer error for the first one in the demo notebook but since it had a commented line I thought it was still related.)The other way to workaround it at present is to install an older version of flake8 as pointed out here. The solution was merged though and should be available soon, and so just avoiding the triggering code in simple cases like yours is probably easier.
Note about the inaccurate code:
Post code for what gave you the issue, not what you think gave you the issue. Part of why you are asked to provide code as text the way you ran it, it is so those looking into it can run it the same way. Plus they don't have to type and can easily run it as you did. Beyond that, there's more reason behind it: you can try to see if what you observed originally matches what you are seeing now.
If you had run what you posted under the heading '1st ERROR' as the code block text, you would have seen it without the error you showed in your image. Usually you'd then discern the error/difference yourself. In other words, starball's comment was trying to point you to why you should be doing that, and sorted that 1st error out yourself. That would ultimately make your point about the fragile nature of %%flake8 magic have more impact because it isn't diluted by report of an error that isn't really an error. Plus, you may have realized what was necessary to avoid '2nd ERROR' because that code block works without error no matter how many times you run it.
Technical notes:
Anyone wishing to try out pycodestyle_magic with some of this code and the demo notebooks can click here to launch such a session served via the mybinder.org. service with most things necessary already set up. Once the session starts up in the classic Jupyter notebook interface, you need to do some further preparation to use pycodestyle_magic. As detailed here, you can install it with pip install flake8 pycodestyle_magic. So open a new notebook with the Python kernel backing it using New drop down on the upper right side and then choose the Python3 kernel. When it opens, make a cell that has %pip install flake8 pycodestyle_magic and run it. Then put %load_ext pycodestyle_magic as a cell and run that. You can now try the magic with code or pull the demo notebooks over and run those.

Error in file(out, "wt") : cannot open the connection when selecting functions

I know that this error has been asked often but I've checked most of the questions and none of them have the same cause as mine, at least on the surface.
I work in Rstudio. The console lets you write functions and offers suggestions, for example if I start typing "ex" the first option will be the "exp" function:
(example)
The problem is that everytime that the suggestion box appears there is a small time (usually a second or two) before a pop up window appears, saying "Error retrieving help. R code execution error" and in the console the text in red says: "Error in file(out, "wt") : cannot open the connection". Fortunately the console command that I'm writing doesn't get deleted but it is still very annoying and sometimes I want to use the suggestion box to save time but it feels that I have to rush to select the function before Rstudio punishes me.

Print list of all error messages after running an R script

Apologies if there is an obvious answer to this question, but I haven't found one.
All I'm looking for is the error equivalent of warnings() - I want to run a script, then get a list of all the errors that occurred when running the script.
Have had a look at traceback() and it seems like it might do what I want - the help file says "The default display is of the stack of the last uncaught error" - but can't figure out how to make it return all uncaught errors rather than just the last one.
My suggestion would be, save the script (e.g. script.R) then run the whole script using source('script.R'). This will stop at every error. If you just want to see if errors exist, this will be a good way to do it.

Error with a "Selection: " prompt

I have a weird recurring error and I am hoping to get some help here. The issue is that R would break into an error prompt (see below) whenever I refer to a nonexistent function the moment I type ( following the incorrect function name (and before I even hit enter):
> asdf(
Selection:
The word Selection would be in purple (prompt color, as set in R preferences). At this point I have to enter 0 to go back to the regular prompt. Entering other numbers does not give any useful information as in traceback.
There are various ways that can lead to this. However, I have not been successful in replicating this from a --vanilla launch (while this suggests it's something with my profile, I haven't been able to isolate the cause). And even if I launch with my usual profile I would not see it when I first launch R, and it would take some time before this happens.
Once I see this error happens once, I would be able to consistent reproduce it until I restart R.
I can also trigger the error by selecting characters.
> xx <- 1
> xx
(right now the cursor is right behind the last x in xx) So far so good, but now if I try to select the last x (either by using the trackpad, or by pressing shift-left-arrow key) R would break into the same error.
In the status bar at the bottom of the R console window I would see
executing: try(gsub('\\s+','',paste(capture.output(print(args(x))),collapse='')),silent=TRUE)
I have R 3.2.2 on Mac OS X (10.11), but I have seen this error for a long time (I think more than a year).
While the error does not lead to any serious problems, it is very annoying. Anyone can give me some idea on how to fix this?

Strange debug results in R Studio

I must predicate this by saying that I am still quite new to RStudio/R so I hope this is not user error. However when debugging I am getting some strange results whilst in debug mode.
1) The code jumps to another part of the code without apparently being called.
2) When this happens the highlighter only partially highlights the code it jumps to but the arrow in the margin points to this partially highlighted line
3) The code then returns to place it jumped away from originally
I get the warning "Debug location is approximate because source source code is not available". Is this related to the problem?
You can replicate the results by installing the package dlm:
https://cran.r-project.org/web/packages/dlm/dlm.pdf
p<-3
G=diag(c(0.9963,0.9478,0.7740))
W=diag(c(0.0026^2,0.0027^2,0.0035^2))
C0_est=solve(diag(p)-G%*%t(G))*W
mod2<-dlm(m0=c(0.0501,-0.0251,-0.0116),C0=C0_est,FF=X,V=0.000000001*diag(m),GG=diag(c(0.9963,0.9478,0.7740)),W=diag(c(0.0026^2,0.0027^2,0.0035^2)))
debugonce(dlmForecast)
dlmForecast(mod2, nAhead=5, sampleNew=2)
When you step through using F10 you will find that the code jumps to L7 from Lines 28,32, 54 and 57 and to L4 from line 47.
Q1.) Why does this partial highlighting of the line being debugged mean?
Q2.) What causeses the code to jump backward and forwards like this with no apparent function call?
Kind Regards
Baz
Most R packages don't include copies of the package's source code (the keep.source option controls this behavior; you can read more about it here). When you debug code from this kind of package, RStudio has to guess at both what the code looked like originally and where the current execution point is in that code. This process includes some heuristics and does occasionally misinterpret the execution point. So what you're seeing is not the actual execution point jumping around, but RStudio making the wrong guess about where it is in the deparsed code.
Code from your own R scripts (and from packages with keep.source) have source references (see R journal article) which allow RStudio to line up the code and execution point precisely.
I wasn't able to reproduce this with your example (it appears to reference a variable m that it doesn't declare); if you can resolve that and send it to me (jonathan at rstudio dot com) I'll see if we can improve the heuristics so this case works better.

Resources