R seems to ignore the last line of .Rprofile? - r

It is very strange that R appears to ignore the last line of my .Rprofile.
For example, in .Rprofile
...
test<-function()1 # without line ending
And thus test is never loaded although the rest is loaded successfully. If I load it manually, source(".Rprofile"), test loads fine.
Is it the desired behaviour?
Under R studio with R 3.0 + in Linux
Can any one reproduce this?

This is a super annoying "feature" - the last line is silently ignored if it does not have a trailing newline.

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.

R Console thought the command hasn't finished yet and go to + mode

When I enter this code in R it works fine
R works
r <- c(0.33732951,0.43556465,0.30636046,0.62461099,0.50852866,0.50000000,0.58672964,0.29569920,0.44354880,0.10928014,0.39103592,0.15000000,0.20277678,0.05547002,0.34786088,0.59259372,0.22456183,0.52085869,0.13411332,0.14000000,0.73546313,0.70886082,0.60574358,0.51000000,0.74603590,0.63419784,0.56821964,0.50000000,0.30750708,0.25300000,0.57458777,0.43225991,0.36255808,0.31200000,0.36088987,0.23500000,0.67900000,0.52780663,0.84877634,0.60100000,0.52312114,0.88016779,0.54786992,0.44100000,0.71454795,0.40600000,0.63336988,0.48430697,0.62300000,0.85775039,0.49700000,0.86755325,0.28000000,0.41000000,0.29000000,0.49000000,0.47584950,0.54614545,0.38933141,0.42177569,0.44000000,0.38000000,0.30000000,0.35000000,0.43782491,0.59376254,0.40183930,0.47980812,0.64908555,0.70267977,0.51212255,0.36324971,0.37531746,0.40356716,0.47217357,0.63844986,0.30000000,0.24000000,0.33000000,0.53979562,0.41000000,0.20000000,0.35000000,0.62699337,0.54691447,0.93206856,0.46946850,0.65527331,0.53214520,0.87832701,0.41905965,0.63000000,0.38354612,0.56000000,0.35676991,0.29922638,0.26470026,0.18989366,0.73214090,0.74000000,0.79819873,0.74000000,0.68839278,0.75000000,0.68839278,0.68000000,0.30000000,0.12000000,0.45000000,0.06000000,0.32000000,0.44165042,0.49694187,0.63526724,0.53084984,0.45700000,0.28336175,0.24317002,0.28464587,0.16522301,0.41483438,0.62062299,0.24000000,0.32346029,0.15000000,0.62325274,0.24000000,0.41000000,0.23000000,0.32165450,0.30173569,0.03187675,-0.04782179,0.02956562,0.07973255,-0.22000000,-0.02696799,0.50000000,0.29494534,0.43000000,0.24679100,0.57558910,0.24645372,0.50000000,0.30096463,0.63082172,0.38295544,0.27817034,0.40538389,0.17616607,0.45000000,0.11803126,0.44000000,0.20000000,0.70900000,0.31000000,0.78000000,0.24900000,0.40600000,0.36000000,0.41900000,0.64812911,0.28077588,0.63883770,0.21744889,0.13203260,0.05807479,0.34675230,0.13066828,0.17070882,0.07549723,0.39777792,0.65397387,0.64048987,0.51913389,0.56000000,0.31000000,0.42000000,0.69214473,0.46503474,0.56777497,0.27300000,0.32500000,0.33400000,0.34400000,0.36447006,0.32547228,0.22000000,0.22000000,0.25000000,0.36074535,0.35000000,0.28000000,0.05000000,0.49300000,0.43500000,0.48750000,0.30000000,0.63750000,0.41000000,0.83236786,0.69647321,0.59323981,0.62000000,0.69000000,0.42600000,0.56100000,0.59600000,0.63761434,0.36827862,0.61000000,0.34526120,0.43000000,0.34074078,0.50395263,0.39705038,0.33000000,0.62919823,0.62000000,0.49965742,0.50000000,0.64770406,0.61000000,0.81511084,0.73821398,0.62482030,0.67288340,0.59189332,0.85976843,0.36461257,0.83993087,0.59600000,0.67200000,0.65700000,0.67500000,0.34870942,0.63444530,0.81000000,0.41428134,0.45614037,0.41797478,0.20000000,0.26000000,0.36000000,0.45000000,0E-8,0.01000000,0.20000000,0.23000000,0.38000000,0.47968275,0.72166665,0.50000000,0.12700000,0.38651119,0.68878395,0.71000000,0.42000000,0.70000000,0.39000000,0.44988642,0.42124197,0.34000000,0.11000000,0.42000000,0.37000000,0.74541280,0.61012712,0.50246344,0.57498891,0.45985678,0.54804849,0.58633763,0.36000000,0.55715993,0.63899544,0.72252703,0.75500000,0.78300000,0.80000000,0.51094514,0.51211435,0.30000000,0.29000000,-0.09000000,0.29200000,0.48527820,0.36800000,0.48100000,0.12555004,0.40900000,0.56200000,0.23400000,0.56300000,0.38000000,0.49100000,0.52327502,0.53200000,0.45552468,0.04390000,0.49628505,0.52600000,0.50675055,0.45000000,0.30000000,0.56000000,0.53000000,0.38000000,0.55000000,0.49400000,0.41300000,0.50100000,0.73800000,0.48615496,0.59296173,0.62349262,0.39200000,0.43689994,0.64200000,0.64601018,0.72488244,0.63960215,0.63960215,0.48739278,0.18000000,0.56000000,0.60000000,0.50500939,0.34000000,0.50000000,0.61000000,0.53033987,0.36425313,0.48600000,0.66400000,0.42800000,0.52248837,0.55429714,0.38759566,0.61466918,0.06676300,0.08029551,0.07456011,0.07456011,0.05173567,0.05000000,0E-8,0.09000000,0.76800000,0.45900000,0.84700000,0.65100000,0.79754237,0.58286220,0.31153461,0.21323135,0.51900000,0.38700000,0.75400000,0.60600000,0.73178375,0.63758279,0.62884328,0.61547860,0.63892541,0.14773733,0.34210000,0.23908753,-0.07010000,0.76740585,0.48100000,0.64600000);
but in when I just add more values to combine it doesn't work.
Going to finish the command mode
r <- c(0.33732951,0.43556465,0.30636046,0.62461099,0.50852866,0.50000000,0.58672964,0.29569920,0.44354880,0.10928014,0.39103592,0.15000000,0.20277678,0.05547002,0.34786088,0.59259372,0.22456183,0.52085869,0.13411332,0.14000000,0.73546313,0.70886082,0.60574358,0.51000000,0.74603590,0.63419784,0.56821964,0.50000000,0.30750708,0.25300000,0.57458777,0.43225991,0.36255808,0.31200000,0.36088987,0.23500000,0.67900000,0.52780663,0.84877634,0.60100000,0.52312114,0.88016779,0.54786992,0.44100000,0.71454795,0.40600000,0.63336988,0.48430697,0.62300000,0.85775039,0.49700000,0.86755325,0.28000000,0.41000000,0.29000000,0.49000000,0.47584950,0.54614545,0.38933141,0.42177569,0.44000000,0.38000000,0.30000000,0.35000000,0.43782491,0.59376254,0.40183930,0.47980812,0.64908555,0.70267977,0.51212255,0.36324971,0.37531746,0.40356716,0.47217357,0.63844986,0.30000000,0.24000000,0.33000000,0.53979562,0.41000000,0.20000000,0.35000000,0.62699337,0.54691447,0.93206856,0.46946850,0.65527331,0.53214520,0.87832701,0.41905965,0.63000000,0.38354612,0.56000000,0.35676991,0.29922638,0.26470026,0.18989366,0.73214090,0.74000000,0.79819873,0.74000000,0.68839278,0.75000000,0.68839278,0.68000000,0.30000000,0.12000000,0.45000000,0.06000000,0.32000000,0.44165042,0.49694187,0.63526724,0.53084984,0.45700000,0.28336175,0.24317002,0.28464587,0.16522301,0.41483438,0.62062299,0.24000000,0.32346029,0.15000000,0.62325274,0.24000000,0.41000000,0.23000000,0.32165450,0.30173569,0.03187675,-0.04782179,0.02956562,0.07973255,-0.22000000,-0.02696799,0.50000000,0.29494534,0.43000000,0.24679100,0.57558910,0.24645372,0.50000000,0.30096463,0.63082172,0.38295544,0.27817034,0.40538389,0.17616607,0.45000000,0.11803126,0.44000000,0.20000000,0.70900000,0.31000000,0.78000000,0.24900000,0.40600000,0.36000000,0.41900000,0.64812911,0.28077588,0.63883770,0.21744889,0.13203260,0.05807479,0.34675230,0.13066828,0.17070882,0.07549723,0.39777792,0.65397387,0.64048987,0.51913389,0.56000000,0.31000000,0.42000000,0.69214473,0.46503474,0.56777497,0.27300000,0.32500000,0.33400000,0.34400000,0.36447006,0.32547228,0.22000000,0.22000000,0.25000000,0.36074535,0.35000000,0.28000000,0.05000000,0.49300000,0.43500000,0.48750000,0.30000000,0.63750000,0.41000000,0.83236786,0.69647321,0.59323981,0.62000000,0.69000000,0.42600000,0.56100000,0.59600000,0.63761434,0.36827862,0.61000000,0.34526120,0.43000000,0.34074078,0.50395263,0.39705038,0.33000000,0.62919823,0.62000000,0.49965742,0.50000000,0.64770406,0.61000000,0.81511084,0.73821398,0.62482030,0.67288340,0.59189332,0.85976843,0.36461257,0.83993087,0.59600000,0.67200000,0.65700000,0.67500000,0.34870942,0.63444530,0.81000000,0.41428134,0.45614037,0.41797478,0.20000000,0.26000000,0.36000000,0.45000000,0E-8,0.01000000,0.20000000,0.23000000,0.38000000,0.47968275,0.72166665,0.50000000,0.12700000,0.38651119,0.68878395,0.71000000,0.42000000,0.70000000,0.39000000,0.44988642,0.42124197,0.34000000,0.11000000,0.42000000,0.37000000,0.74541280,0.61012712,0.50246344,0.57498891,0.45985678,0.54804849,0.58633763,0.36000000,0.55715993,0.63899544,0.72252703,0.75500000,0.78300000,0.80000000,0.51094514,0.51211435,0.30000000,0.29000000,-0.09000000,0.29200000,0.48527820,0.36800000,0.48100000,0.12555004,0.40900000,0.56200000,0.23400000,0.56300000,0.38000000,0.49100000,0.52327502,0.53200000,0.45552468,0.04390000,0.49628505,0.52600000,0.50675055,0.45000000,0.30000000,0.56000000,0.53000000,0.38000000,0.55000000,0.49400000,0.41300000,0.50100000,0.73800000,0.48615496,0.59296173,0.62349262,0.39200000,0.43689994,0.64200000,0.64601018,0.72488244,0.63960215,0.63960215,0.48739278,0.18000000,0.56000000,0.60000000,0.50500939,0.34000000,0.50000000,0.61000000,0.53033987,0.36425313,0.48600000,0.66400000,0.42800000,0.52248837,0.55429714,0.38759566,0.61466918,0.06676300,0.08029551,0.07456011,0.07456011,0.05173567,0.05000000,0E-8,0.09000000,0.76800000,0.45900000,0.84700000,0.65100000,0.79754237,0.58286220,0.31153461,0.21323135,0.51900000,0.38700000,0.75400000,0.60600000,0.73178375,0.63758279,0.62884328,0.61547860,0.63892541,0.14773733,0.34210000,0.23908753,-0.07010000,0.76740585,0.48100000,0.64600000,0.23908753,-0.07010000,0.76740585,0.48100000,0.64600000);
according to CRAN documentation:
"Command lines entered at the console are limited4 to about 4095 bytes (not characters).",
"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."
source: https://cran.r-project.org/doc/manuals/R-intro.html#FOOT4
If you introduce some line breaks in command may solve the issue.

Output/the next command prompt overwrites long command in R

I'm seeing a very strange issue with R (v 3.3.2, running on the command line of a CentOS 7 linux box). If I execute a command that goes over one line in the terminal, the output appears at the beginning of the second line of the command. This didn't happen with previous versions of R, though it does happen with later versions I've installed. I am using Putty as the terminal, but it also happens if I use xterm.
To describe it better, it looks like this:
> tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaal=1 [before hitting enter]
> tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> aaaaaaaaaaaaaaaal=1 [after enter, typing overwrites the a's now]
If the multiline command outputs something, that output occurs on the second line and overwrites the a's. If there is an error, then the error message terminates properly and the next prompt follows the error message. So does anyone have any ideas? Thanks in advance!
Update: After some more experimenting, I've found another odd behavior. If I use CTRL-R to search for a previous multiline command, and then execute it, the output or subsequent prompt appears at the same location in the command as where the cursor was. Here's another example:
(reverse-i-search)`1':tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal=1[cursor on one before hitting enter]
[hit enter and then:]
>tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaal=1
aaaaaaaaal=>
Pretty strange. Hard to describe so its hard to find any evidence of other problems like this on stack-exchange or elsewhere. My feeling is it has to do with the readline library but it is strange because this doesn't happen in the shell command line, only in R as far as I can tell.
Update 2: I've found a potentially more revealing way to replicate this issue. If you manually add a bunch of newlines (by jamming CTRL-V CTRL-J repeatedly), then hit enter, the next prompt jumps right back up to underneath the last one. This is not what happens in the underlying shell (bash for me), or in previous versions of R that I can check.
Just to have the answer out there in case anyone else has this issue, the problem was that my system was using an old version of the readline library, 6.2, and the issue disappeared when updating it to the current version, 7.0. So it would seem that even though later versions of R will install with the outdated readline, they don't exactly work correctly.

RStudio does not display any output in console after entering code

The problem is that when I run the code, there's no return in the console; I mean it does run the code, but does not return any output.
For example, if I write
v <- c(1, 2, 3, 4, 5)
v
I would expect in return
[1] 1 2 3 4 5
But it's not working.
I have version RStudio Version 0.98.1079 and R Version 3.1.1
Possibility 1 (until the + sign was mentioned): I was wondering if you had been doing a tutorial where they were demonstrating the sink function and you hadn't gotten to the point where it was reversed.
> sink('out.txt') # diverts all output to a disk file
> v <- c(1,2)
> v # output went to file
> sink() # sets the output back to the console
> v
[1] 1 2
Another way would be to call closeAllConnections:
> sink('out.txt')
> v
> v
> closeAllConnections()
> v
[1] 1 2
Possibility 2: To address the lack of response with a "+" showing at the Rstudio console ... that is a sign that the R parser "thinks" the entered text has not completed a full R command. It may indicate that you haven't typed a closing bracket or parenthesis. If typing one or two of those is unsuccessful and you keep getting mor +'s then you may be successful with typing the [esc]-key. If it is showing up immediately after a restart then you should check your code for correctness and make sure that the .Rdata file is deleted from your working directory. If you don't know what that means then you may need to search for the methods appropriate to your operating system. You could also have an error in the code of one of your .rprofile files.
In any case these two possibilities have nothing to do with Rstudio per se and everything to to with the typical behavior of an R console session in pretty much any IDE.
Do the lines still start with a "+"? It is also possible you forgot to close the brackets of a function. Try "}".
I had the same issue and none of the tips mentioned here were working.
Session > Restart R did the trick for me, possibly suggesting that I had a similar problem as andrewH but was not patient enough to wait for R to behave again.
This is a very old question, but I just had the same problem with a different cause, so I thought I would describe it here case it should be useful to someone else. I was getting the regular command prompt, with nothing more, no matter what I typed at the command line. I tried multiple returns, escape, sink, traceback, closeAllConnections (which did give me a response, "error: unexpected ) in (), but then went back to the command prompt and ignored a second traceback).
Anyway after half an hour or so of pulling my hair out, up pops "View(Mid2)". Mid2 is a tibble with 8.5 million observations of 88 numeric variables. I must have tapped it in the environment pane accidentally. I suppose it just took that long for the viewer to render it. I assume that all the other things I did hit at once, because RStudio crashed immediately thereafter.
The interesting thing about this particular version of the problem is what didn't happen. The red stop sign in the upper right of the console window, that lights when R is busy, didn't light. That is unfortunate -- but understandable, if the RStudio viewer is a different process. But also, when my computer is working hard on a really big computation or IO task, the fan usually starts, but it didn't. Don't know why. . I took its absence, incorrectly, to mean no such computation was underway.
If the lines in console are starting with "+".
Save your work and close the 'RStudio' or other tool which you are using and Start it again, it worked for me.
If you are using R Studio Cloud, refresh or re-opening won't work.
Only clue from the above posts or answers is your console will always start with '+'
In my case I tried all possibilities of closing braces.
And ")" worked for me when I typed that into the console and press enter.
sink() function did nothing in R Studio Cloud
A simple mistake might have also caused this problem:
A rather lengthy command left abandoned in the console is blocking the appearance of the result line.
Thus, the console only shows that line, but the result from any code run from the source, will not appear.
To solve this, just switch to the console, remove any remaining command and try again.
Experiencing something like that explained here as an unresponsive console to the R-Code running was just devastating for me when I experienced it. But luckly although I tried every trick explained in this page, it did not work for me. At last I clicked on the "To console" option available just below the Environment, History, Connections, Tutorial Tab on the R Studio. It solved the puzzle for me just now.
The best solution I've found is closeAllConnections and/or sink which almost always work
But as a stop gap measure, View()'ing always works. It's sort of a pain but whatever you wanted to print out, surround by View and you can see it

Suppress the extra white space from compiling in R

Whenever I compile my R code, I get a lot of extra white spaces that just clog up my worksheet.
Is there any way to suppress this, without changing the code to be on the same line using semicolons? I'm currently working in Sage Worksheets, on the cloud.
I've made some changes to slightly reduce whitespace in R mode for SageMathCloud (https://cloud.sagemath.com); restart your project to get this update. However, due to how Sage works they might not be sufficient. If you evaluate the following in a cell (in sage mode, or put %sage at the top), it will eliminate a lot of additional whitespace in %r mode.
def r_eval0(*args, **kwds):
return sage.interfaces.r.R.eval(sage.interfaces.r.r, *args, **kwds).strip().replace('\n\n','')
sage_salvus.r_eval0 = r_eval0

Resources