Skip to content
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

Support for {renv} projects #93

Open
wurli opened this issue Mar 21, 2024 · 18 comments
Open

Support for {renv} projects #93

wurli opened this issue Mar 21, 2024 · 18 comments
Labels
enhancement New feature or request

Comments

@wurli
Copy link
Contributor

wurli commented Mar 21, 2024

NB, this issue has previously been discussed at jalvesaq/Nvim-R/issues/445.

Problem description

When a project uses {renv}, {nvimcom} is not available, meaning a good deal of R.nvim breaks. The reason for this is that in {renv} projects the .libPaths() are reset, meaning R can no longer find {nvimcom}, which will have been installed by R.nvim into the libpath where most other package installations also live.

Current workaround

The workaround people seem to have been using is to:

  • Create a new libpath directory somewhere, e.g. called nvimcomlib
  • Create a symlink within this libpath that points to the existing nvimcom installation
  • Set the RENV_CONFIG_EXTERNAL_LIBRARIES environmental variable to point to this new libpath, which tells {renv} it can also look for package installations here

There are a few issues with this workaround:

  • You need to go through these steps each time you update R
  • Symlinks may be hard to create, e.g. on Windows without admin privileges
  • It requires some esoteric knowledge, both about {renv} and R.nvim

Suggested approach for seamless {renv} support

  • When R.nvim is installed, instead of dropping {nvimcom} into the user's default libpath directory, keep this package in its own dedicated directory somewhere else
  • Then, let R know that it can also look for package installations in this alternative location, e.g. by setting the R_LIBS environmental variable before R starts up
  • At the same time, set the RENV_CONFIG_EXTERNAL_LIBRARIES environmental variable to the same value, thereby making {nvimcom} available in {renv} projects

I'm not that familiar with with what happens when R.nvim is installed, so I don't know how realistic/well informed this approach is. However, {renv} is pretty widely used - a quick GitHub search reveals 9.1k repos with a renv.lock file - so in my humble opinion, it's worth seriously considering adding official support.

@PMassicotte
Copy link
Collaborator

PMassicotte commented Mar 21, 2024

That is interesting, I just opened like 5-6 old projects using renv and I had no issues. When I open such project, I see that nvimcom is being installed.

@wurli
Copy link
Contributor Author

wurli commented Mar 21, 2024

Interesting indeed! Have you previously done any additional setup to get renv working with R.nvim?

@PMassicotte
Copy link
Collaborator

Nothing I can think of. I just had a look inside one of the renv.lock file and nvimcom is not even listed. Humm I am not sure what is goin on!

@wurli
Copy link
Contributor Author

wurli commented Mar 21, 2024

That's very strange - if renv installs nvimcom, it must have already cached it or be configured to know where to look for it, since nvimcom isn't on CRAN. Either way, I'm 99% sure it won't Just Work™ from a fresh system. Would be great if others could test and confirm 🙏

@wklimowicz
Copy link
Contributor

I've had this problem before and just tested it now. I bypass renv by setting export R_PROFILE_USER="~/.Rprofile" in my .zshrc, which bypasses the project .Rprofile and essentially deactivates renv.

The reason for this is that with R_PROFILE_USER unset, renv loads, and tries to install R packages but loading my ~/.Rprofile first. I have a messy .Rprofile which has library calls to remotes and rlang which breaks the renv process. I'm sure I could fix this by tidying my .Rprofile.

If I open a renv project without R_PROFILE_USER and no ~/.RProfile everything works fine for me, nvimcom compiles and R.nvim loads with no issues.

@hongyuanjia
Copy link
Contributor

I can confirm that R.nvim works fine with {renv} with no issue. The only step needed is to completely exit Neovim after renv::init() and open Neovim again. {nvimcom} will be installed in renv folder but will not appear in renv.lock.

@jalvesaq jalvesaq added the enhancement New feature or request label Apr 27, 2024
@PMassicotte
Copy link
Collaborator

Can we close this? I am using renv all the time and I am facing no issues. @wurli are you experiencing any current problems with renv?

@Nand1sh
Copy link

Nand1sh commented Jul 27, 2024

@PMassicotte I believe I am facing the same issue. rnvimserver dumps core upon first interaction with a renv enabled project. I could not replicate this in my journal for scripts or projects that do not use renv. Also, as @wurli suggested, this problem occurs only after a fresh boot. If I were to first open a regular project and then move on to one with renv enabled then everything works great.

@PMassicotte
Copy link
Collaborator

What is the value of

setwd = "nvim",

@Nand1sh
Copy link

Nand1sh commented Jul 27, 2024

:echo getcwd() in Neovim's command mode and getwd() in R console both point to my renv project's directory namely "/home/nand1sh/Study/LWE_RESEARCH".

@PMassicotte
Copy link
Collaborator

Do you use built-in terminal or something else?

@Nand1sh
Copy link

Nand1sh commented Jul 27, 2024

Yes, I use the built in terminal for R in R.nvim.

@PMassicotte
Copy link
Collaborator

That is interesting. I can not reproduce the problem at the moment. When I open a renv project for the first time, I see that nvimcom is being installed (takes 2–3 seconds). Then I am ready to go. Do you have any logs to share?

@Nand1sh
Copy link

Nand1sh commented Jul 31, 2024

Hey, this helps! If I wait for a few seconds and then start R, I get no issues. Only if I start R immediately after opening a .R file in a renv enabled project do I get this-

Jul 31 16:30:43 archlinux systemd-coredump[1096]: Process 1059 (rnvimserver) of user 1000 terminated abnormally with signal 11/SEGV, processing...
Jul 31 16:30:43 archlinux systemd[1]: Created slice Slice /system/systemd-coredump.
Jul 31 16:30:43 archlinux systemd[1]: Started Process Core Dump (PID 1096/UID 0).
Jul 31 16:30:43 archlinux systemd-coredump[1097]: [🡕] Process 1059 (rnvimserver) of user 1000 dumped core.
                                                  
                                                  Stack trace of thread 1059:
                                                  #0  0x00007a498ab79fad pthread_cancel (libc.so.6 + 0x90fad)
                                                  #1  0x00005a7681e249bb n/a (/home/nand1sh/Study/LWE_RESEARCH/renv/library/linux-arch-rolling/R-4>
                                                  #2  0x0000000000000000 n/a (n/a + 0x0)
                                                  ELF object binary architecture: AMD x86-64
Jul 31 16:30:43 archlinux systemd[1]: [email protected]: Deactivated successfully.

However, unlike you I do not get a prompt saying that nvimcom is being installed. Is it because I have the --quiet argument enabled for R.nvim?

@jalvesaq
Copy link
Member

The R_args config option is used only to launch R in the terminal emulator for interactive use. The message that nvimcom is being installed is always displayed with vim.api.nvim_echo() (see scripts/before_rns.R, line 115, and lua/r/server.lua, line 73).

@PMassicotte
Copy link
Collaborator

Hey, this helps! If I wait for a few seconds and then start R, I get no issues. Only if I start R immediately after opening a .R file in a renv enabled project do I get this-

Jul 31 16:30:43 archlinux systemd-coredump[1096]: Process 1059 (rnvimserver) of user 1000 terminated abnormally with signal 11/SEGV, processing...
Jul 31 16:30:43 archlinux systemd[1]: Created slice Slice /system/systemd-coredump.
Jul 31 16:30:43 archlinux systemd[1]: Started Process Core Dump (PID 1096/UID 0).
Jul 31 16:30:43 archlinux systemd-coredump[1097]: [🡕] Process 1059 (rnvimserver) of user 1000 dumped core.
                                                  
                                                  Stack trace of thread 1059:
                                                  #0  0x00007a498ab79fad pthread_cancel (libc.so.6 + 0x90fad)
                                                  #1  0x00005a7681e249bb n/a (/home/nand1sh/Study/LWE_RESEARCH/renv/library/linux-arch-rolling/R-4>
                                                  #2  0x0000000000000000 n/a (n/a + 0x0)
                                                  ELF object binary architecture: AMD x86-64
Jul 31 16:30:43 archlinux systemd[1]: [email protected]: Deactivated successfully.

However, unlike you I do not get a prompt saying that nvimcom is being installed. Is it because I have the --quiet argument enabled for R.nvim?

I am using auto-open in my config, so it is likely waiting for the initialization to be done before loading R. My guess is that you just have to wait 2-3 seconds before opening the terminal in an renv project.

@Nand1sh
Copy link

Nand1sh commented Aug 3, 2024

I see. Thanks for the clarification @jalvesaq and @PMassicotte!

@PMassicotte
Copy link
Collaborator

Is this still relevant? I am using renv on daily basis and I have no issues so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants