-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix r profile #399
Fix r profile #399
Conversation
remove curly braces because they prevent the code from working add source vscode to the rix_init.R file
Could you give more details? I just tried running install.packages("dplyr") inside of a Nix session, and I do get the error message. Same for update.packages(). |
Hm, this is weird. On my machine (ubuntu noble) this did not raise the expected error. I tried making it reproducible using docker (and then encountered the issue #401 ), but the docker image then raised the expected error. So i need to investigate further why this happens with my settings in a non-docker environment. |
could you post the generated .Rprofile here? |
I couldn't reproduce my earlier problems; sorry, don't know what happened there. So I removed the gsub part which removed the curly braces and only kept the part for vscode-R. I also added a check for positron because this is also "vscode" but does work with vscode-R. https://positron.posit.co/extensions.html |
where you starting the session with |
No. Well, everything works fine now, so I am happy :P. |
@b-rodrigues any further input you need from me for this PR and the other PRs? |
can you explain what the Positron check does ? |
See https://positron.posit.co/extensions.html |
I still don't get it, what should not be called ? |
I need to know exactly what this fixes before merging though |
From my reading this seems to be something rix shouldn't "fix" |
With the current .Rprofile, vscode-R is not really usable. E.g. I guess you know this from using emacs. while after this "fix" this looks as it's supposed to be in vscode-R And this is only one example, similar things are true for plotting, see attached Namespace etc. So adding this one line is just something which vscode-R users need to do to get vscode-R working properly: https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher#advanced-usage-for-self-managed-r-sessions This is only one additional line, Rstudio has also specific settings and only if your editor is vscode, but not Positron, and if your session is interactive, this will be used. This will then source this file: |
I see but isn't vs code-r not mandatory with Positron? it seems to me that if someone wants to use vscode-r with Positron then it's on them to apply the suggested fix from the documentation, no? |
Ultimately, this has nothing to do with Nix nor rix, or did I misunderstand something? it is possible to append the .Rprofile instead of overwriting it so these users would need to append |
I only use vscode-R (tried Positron, but Copilot does not work, cannot use spacemacs keybindings using VSpaceCode as i am used to, devcontainer and ssh experimental) I just included checking if it's Positron because otherwise also Positron would source this file, which would not work if you read the Positron docs. |
Well, maybe it's not directly linked to nix or rix, but you also have special seetings for Rstudio or vscode (installing language server) and you create a specific R profile. |
OK I see I was confusing Positron and vscode |
I'll be testing and merging all PR this weekend hopefully |
Thanks! |
Could you describe the issue step by step? Because I can't replicate. I have installed vscodium-fhs from nixpkgs, and trying to plot seems to work fine (same for this is with an .Rprofile generated from the code in that's the one, right? |
Yes this is the right extension and everything works fine, which you can see because your plot looks good but also your terminal is attached to the code (becase it says R 4.4.2). And you have an "R interactive" session, which I am not able to create using nix shell. However, I think you don't use R in a nix-shell? Or are you opening vscode from within nix-shell? I am using my locally installed VSCode. So I open my locally installed VSCode and when I then open a R terminal liek this (or via VSpacecode with Spc-m-m) I get an i If I want to use R in nix I open a shell (zsh in my case) and run "nix-shell" and then "R" THen you can see the terminal is called If i click on this `R: (not attched), i get the following error (since the init.R of vscode-R is not sourced). And |
i open vscode from a running nix-shell, what happens if you do that ? |
if you don't want to start vscode from a nix shell then this extension might help https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector |
I haven't thought about these options thanks. However, both options had some issues (or probably the same issue): So using the extensions you proposed: Using vscode from within nix-shell very much the same: |
are you inside of nested shells? |
Out of curiosity: How do handle this, e.g. in Emacs? And for my understanding: What is the disadvantage of changing the Rprofile? At least I don't really know how to fix this with libPaths. And using nix-shell and R from within vscode is just a self-managed R terminal described int he documentation and similar to tmux, so don't think this is such a hacky solution |
A priori no disadvantage but I want to make sure I understand what's wrong. When I start vscode from a shell everything works as expected so I need to know what's about your setup that breaks things because it could be the symptom of a more general issue. Rstudio is the only editor that must be managed by Nix to work with Nix shell, but other could be locally managed. As for me with emacs, I have emacs installed from openSuse, but use nix-direnv https://github.com/nix-community/nix-direnv so when I start an R session in a folder with a nix-shell it gets loaded automatically |
can you share the .Rprofile and default.nix files here ? |
Usually I like to stay within the editor (earlier emacs,now vscode), and use extensions (projects) to navigate to my project and then open files and the R terminal from there (can use Spacemacs keybindings). I would still prefer adding this PR to make it work with self-managed R terminals for vscode users (I can't see why this would break things for others because it will only apply if you use vscode), and we could change it if this does actually break the setup for other users . |
And if you don't have a system wide library of packages, this works as well ? |
thanks for sharing your workflow (althouth I am not fully understanding nix-direnv). And to sum up: |
Ah, now I think I am finally understand the problem you have with the approach. |
I'd be quicker for you to rename the R library in your home/ so it doesn't get loaded than for me to setup vscode and extensions and whatnot 😉 |
Ah sorry, I though you mean having everything installed in /nix.
And as before when the code of this PR wasn't included I cannot attach the R terminal
|
So I added a check if the file exists (so this would be apply if someone uses VSCode interactively (not Positron), but does not have vscode-R installed (which is unlikely because you cannot do much without an R extension), but still. I hope this is what you meant? |
I'll be doing some final testing and then this should be good to merge. It also made me think that the vignette titled "interactive use" needs to be updated and describe all of this as well. |
So I installed vs code from the opensuse package manager and opened a project with a default.nix and got this message I already had these two extensions (perhaps from previous testing?) Starting the command palette and selecting "Nix-Env: select environment" and then restarting vs code works. Then, using I then tried with the code in your branch, but got following warning message:
and the .Rprofile is just
|
@b-rodrigues sorry, my bad. It worked before. But with the last commit, I put the last two parentheses at the wrong place and didn't test anymore. I tested it now and it worked for me. Here is the resulting .Rprofile
|
nice, I'll do some more testing and will likely merge tomorrow |
what does this mean ? not attached ? |
In contrast, I played around with starting code from nix-shell and somestimes encountered difficulties. So if i already had an instance of vscode running, then the new vscode, which was started from within nix-shell, showed again this one nix location only and one of my system library. So I am more happy with the solution starting vscode without nix-shell. |
@b-rodrigues |
if (file.exists(vscode_r_init)) { | ||
source(vscode_r_init) | ||
} else { | ||
message("No .vscode-R/init.R file found. If you want to use VSCode-R, you need to source it in your .Rprofile or start vscode from within nix-shell") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the extension called VSCode-R? Is it this one? https://open-vsx.org/extension/REditorSupport/r
Shouldn't it be REditorSupport?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well it's often called vscode-R and it's stored in the location .vscode-R
.
Here the github repo also says vscode-R https://github.com/REditorSupport/vscode-R.
As far as I know it's the only (or at least only official) R extension in vscode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enough
So this seems to be working fine, but I think that the best workflow likely is:
This is actually exactly how I do it with Emacs. Just put This is ready to merge, there's just one comment left for you to address. Many thanks for this contribution! |
Thank you. I will definitely try this out. And VSCode will then also activate the nix-environment? That would save two steps (but requires an extra package, and extensions and creating a file). |
Yep, once you install direnv, create an .envrc file and allow direnv in that directory, vs code will switch automatically and transparently. |
I've tried this and it worked great. |
I’ll be documenting this: #404 |
Good idea! |
@b-rodrigues I found that the resulting .Rprofile has some curly braces which prevent functions to work.
So I could call
install.packages()
and there was not stop message.Getting rid of the curly braces was quite difficult. Tried using expression or using subsitute but that did not work. So in the end i justed used gsub to remove them. Maybe not very elegant, but at least works. I tested that in my case it raises the expected error message. Not sure we can create a unit test for that, because it's testing the resulting environment?
Additionally, vscode-R did not work properly,
.vsc.attach
did not work and functionalities likeView
did not work.REditorSupport/vscode-R#1094
So I added the necessary source lines. I put this in an if statement only evaluating if the editor is vscode, so I think this should not mess up anything.
Looking forward to your feedback.