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

Some questions regarding usage #326

Closed
petobens opened this issue Apr 29, 2018 · 34 comments
Closed

Some questions regarding usage #326

petobens opened this issue Apr 29, 2018 · 34 comments

Comments

@petobens
Copy link
Contributor

petobens commented Apr 29, 2018

First of all thanks for a great plugin :).
I've recently started using it and I find it really good. However I've read the docs, skimmed over the code and there are still a few things I can't actually get my head around so i''ll ask a few question about the usage.

i) I want to have a mapping (for instance ,rf) that allows me to run a file (or a visual selection). Currently I have first to start R and then use either RSendFile or RDSendSelection. I want to avoid the manual start. I wrote the following function to do this:

function! s:RunR()
    let r_started = IsSendCmdToRFake()
    redraw!
    if r_started == 0
        call StartR("R")
    endif
    call SendFileToR("silent")
endfunction

but a) don't know if it is the correct way of doing it and b) no idea how to extend it to handle a range.

ii) I know I can use the RClearConsole mapping to clear the console but this mapping doesn't clear the global namespace (in the sense that if I defined and x variable then the x variable assignment is still in memory). Therefore: is there someway to clear the namespace?

iii) I didn't find a way to toggle the ObjectBrowser. I know I can open it with the RUpdateObjBrowser binding but I would a mapping to open and close it at will. Is that possible?

Once again thanks for the plugin and the future help! :)

@jalvesaq
Copy link
Owner

i) <LocalLeader>ao runs R CMD BATCH with the current file as input and opens the output in a new tab. There is no such functionality for selection only, but it would be easy change the function to check if there is a visual selection and send it instead of the whole file.

ii) <LocalLeader>rm sends rm(list=ls()) to the R Console.

iii) <LocalLeader>ro opens the Object Browser. When I first developed the Object Browser it did not automatically update. So, typing <LocalLeader>ro again would update it. Now, typing <LocalLeader>ro again moves the cursor to the Object Browser. However, since Vim already has commands to move from one window to another, perhaps it would be better if <LocalLeader>ro closed the Object Browser if it were already open.

@petobens
Copy link
Contributor Author

petobens commented Apr 29, 2018

i) ao runs R CMD BATCH with the current file as input and opens the output in a new tab. There is no such functionality for selection only, but it would be easy change the function to check if there is a visual selection and send it instead of the whole file.

I don't want this particular behaviour. I want to be able to run a file inside an interactive shell. If the interactive shell is not opened then first open it and the run; otherwise (if it is already opened) then just run it. Can that be achieved in a better fashion than my rudimentary function approach?

ii) rm sends rm(list=ls()) to the R Console.

Exactly what i needed :)

iii) ... perhaps it would be better if ro closed the Object Browser if it were already open.

Yep that's what i'm after. Do you plan to implement it? Also can the object browser be always opened as a vertical split on the very left of the screen?

Thanks for the quick reply!

jalvesaq added a commit that referenced this issue Apr 29, 2018
@jalvesaq
Copy link
Owner

i) Wouldn't it work if you created another function calling SendSelectionToR instead of SendFileToR?

@jalvesaq
Copy link
Owner

Also can the object browser be always opened as a vertical split on the very left of the screen?

Depending on what buffers you have open, it will not be on the very left, but you can start the Object Browser on the left of either the script being edited or the R console:

let R_objbr_place = 'script,left'
let R_objbr_place = 'console,left'

@petobens
Copy link
Contributor Author

petobens commented May 1, 2018

i) Wouldn't it work if you created another function calling SendSelectionToR instead of SendFileToR?

My use case is that I'm editing an R file and then I would like to press some mapping (for instance ,rf) and automatically run the file (or eventually a range): this means I want to avoid the double mapping of first opening the console and then running the file. Do think this is possible?

Depending on what buffers you have open, it will not be on the very left, but you can start the Object Browser on the left of either the script being edited or the R console

So basically there is no way to always open it at the very far right? (I prefer it that way).

Thanks for your help and patience! :)

@jalvesaq
Copy link
Owner

jalvesaq commented May 1, 2018

I want to avoid the double mapping of first opening the console and then running the file. Do think this is possible?

It might not work. "Starting R" really means more than starting R:

  1. Check nvimcom version.
  2. Update nvimcom if necessary.
  3. Check whether the files at ~/.cache/Nvim-R are up to date or not, and update them if necessary.
  4. Start R.
  5. Wait nvimcom startup.

So basically there is no way to always open it at the very far right? (I prefer it that way).

I can implement this. Currently, Nvim-R runs :belowright but I have just seen that there is also a :topleft command...

@petobens
Copy link
Contributor Author

petobens commented May 1, 2018

I can implement this. Currently, Nvim-R runs :belowright but I have just seen that there is also a :topleft command...

That would be perfect. Thanks

It might not work. "Starting R" really means more than starting R:

Ok. So can you suggest how should i write a funciton that actually checks that these steps finish and then runs the code?

@petobens
Copy link
Contributor Author

petobens commented May 1, 2018

Also (continuing with my nvim-R questions), have you ever tried using deoplete for automatic omicompletions?

I'd like to do something like similar to what you suggested here. I tried adding the following to use omnicompletions immediately after a .:

call deoplete#custom#var('omni', 'input_patterns', {
        \ 'r' : ['[^. *\t]\.\w*'],
  \}
\)

but whenever I type data. the cursor moves just before the dot. Maybe this is a question better suited for @Shougo.

@jalvesaq
Copy link
Owner

jalvesaq commented May 1, 2018

So can you suggest how should i write a funciton that actually checks that these steps finish and then runs the code?

Please, look at the code of CheckIfNvimcomIsRunning and WaitNvimcomStart in R/common_global.vim. You could try checking if the value of g:rplugin_nvimcom_port no longer is 0.

Also (continuing with my nvim-R questions), have you ever tried using deoplete for automatic omicompletions?

No, because ncm-R is working fine, and I can't see any advantage of completing keywords only after a .. This is useful for C structures, not for the R language.

@petobens
Copy link
Contributor Author

petobens commented May 1, 2018

You could try checking if the value of g:rplugin_nvimcom_port no longer is 0
Awesome. The following almost works

function! s:RunR(mode)
    if g:rplugin_nvimcom_port == 0
        call StartR("R")
        while g:rplugin_nvimcom_port == 0
            sleep 300m
        endwhile
    endif
    if a:mode == 'file'
        call SendFileToR("silent")
    elseif a:mode == 'visual'
        call SendSelectionToR("silent", "down")
    endif
endfunction
au FileType r nmap <Leader>rf :call <SID>RunR('file')<CR>
au FileType r vmap <Leader>rf :call <SID>RunR('visual')<CR>

however when i used the visual mapping the selection is send twice. Any ideas why?

No, because ncm-R is working fine

Ok. I might give it a try.

@jalvesaq
Copy link
Owner

jalvesaq commented May 2, 2018

when i used the visual mapping the selection is send twice. Any ideas why

Probably you are sending two lines. Try this:

au FileType r vmap <Leader>rf <Esc>:call <SID>RunR('visual')<CR>

@petobens
Copy link
Contributor Author

petobens commented May 2, 2018

Excellent thanks! Feel free to close. Thanks for the awesome support.

@jalvesaq jalvesaq closed this as completed May 2, 2018
@petobens
Copy link
Contributor Author

petobens commented May 2, 2018

Sorry one last thing. Imagine that I close the console from within the console by using q() rather than the \rq mapping. Then if I want to re start it nvim-r gives the warning message:

As far as I know, R is already running. If it is not running, did you quit it from within nvim (command \rq)?

Is it possible to add an option to restart the console if nvim-r doesn't find it? (instead of giving the warning message). I ask this because closing from within the console i something I do quite often and currently i need to restart vim if I want to reuse it.

jalvesaq added a commit that referenced this issue May 2, 2018
Note: it works only when running R within the built-in terminal emulator.
See: #326
@jalvesaq
Copy link
Owner

jalvesaq commented May 2, 2018

It should work now if running R inside Vim/Neovim built-in terminal. If running R in an external terminal emulator, you have to press <LocalLeader>rq after closing R manually to actually clear some Nvim-R's internal variables.

@petobens
Copy link
Contributor Author

petobens commented May 2, 2018

Awesome. Best plugin support ever. Thanks.

If running R in an external terminal emulator, you have to press rq after closing R manually to actually clear some Nvim-R's internal variables.

I'm indeed using tmux. There is no way for nvim-r to actually run rq by itself with it doesn't find the pane? (instead of forcing the user to do it?)

@jalvesaq
Copy link
Owner

jalvesaq commented May 2, 2018

There is no way for nvim-r to actually run rq by itself with it doesn't find the pane?

It actually does this if you try to send a line to R.

The terminal emulator is run as a job, but it is detached because, otherwise, it would be killed if you closed Neovim. That's why Nvim-R cannot detect when the terminal has quitted.

@dryya
Copy link

dryya commented May 2, 2018

@petobens See this issue for deoplete/nvim-r completion support. Shougo/deoplete.nvim#745
The config I settled on is:

call deoplete#custom#option('omni_patterns', {
    \ 'r'   : ['[^. *\t]\.\w*', '\h\w*::\w*', '\h\w*\$\w*', '\h\w*\w*', '\h\w*(w*']
\})

This essentially causes deoplete to be a dumb pipe to nvim-r supplied omnicomplete. I lose ultisnips showing up in the completion menu, but I generally just type them out and manually expand them anyways.

@petobens
Copy link
Contributor Author

petobens commented May 3, 2018

@Aenda the problem with this approach is that it is too slow and blocking. I actually read that issue and posted some comments there.

@dryya
Copy link

dryya commented May 3, 2018

Have you figured out a solution that fixed the issue in your comment above? I consider what I'm using more of a hack than a real solution, to be honest. Working looking into what it would take to implement better deoplete compatibility in nvim-r like in vimtex is on my to do list, but I don't think I'll get to it for a while.

@petobens
Copy link
Contributor Author

petobens commented May 3, 2018

Nope. I asked (in that linked issue) if @jalvesaq and @gaalcaras have any ideas/pointers.

@jalvesaq
Copy link
Owner

jalvesaq commented May 3, 2018

@gaalcaras has done a great job with ncm-R, and I have made some adjustments in Nvim-R to ncm-R. So, if someone wants to develop integration with deoplete, it is better if the infrastructure created to ncm-R is used.

@petobens
Copy link
Contributor Author

petobens commented May 7, 2018

@gaalcaras do you think it is possible for you to write a deoplete source? (since you already have the know how)? Mmm I want to avoid installing a new completion framework just for one specific language (and in fact this would be extra helpful for other people using deoplete)

@petobens
Copy link
Contributor Author

petobens commented May 7, 2018

I think this is particularly relevant because nvim-completion-manager is no longer actively maintained (whereas deoplete is): roxma/nvim-completion-manager#12 (comment)

@petobens
Copy link
Contributor Author

petobens commented May 7, 2018

The terminal emulator is run as a job, but it is detached because, otherwise, it would be killed if you closed Neovim. That's why Nvim-R cannot detect when the terminal has quitted

@jalvesaq Consider the following GIF:
may-07-2018 16-33-58

What I would like is that instead of nvimr showing the can't find pane message, for it to actually run (in this case) \rq and restart R (and avoid myself doing \rq and then \rs). Is that possible? Since nvim-r actually detects that there is no pane it might as well actually quit the nvim-r session right? Thanks once again

@jalvesaq
Copy link
Owner

jalvesaq commented May 7, 2018

I don't use advanced Tmux features... Can it hide a pane? Then what is the message when you try to send lines to a hidden pane?

@petobens
Copy link
Contributor Author

petobens commented May 7, 2018

Sorry I don't follow. I actually quit the pane, that's why i get the can't find pane. My question is if it possible to add some conditional or try/catch block for this situation. Or at least if you can point it toward where this is implemented and I can take a look.

@jalvesaq
Copy link
Owner

jalvesaq commented May 7, 2018

I guess I can do what you requested. I did the following:

CTRL-A :split-window
nvim something
CTRL-A z

This makes the R Console disappear, but if I send lines to R they keep being evaluated.

@jalvesaq
Copy link
Owner

jalvesaq commented May 7, 2018

What I would like is that instead of nvimr showing the can't find pane message, for it to actually run (in this case) \rq and restart R (and avoid myself doing \rq and then \rs). Is that possible? Since nvim-r actually detects that there is no pane it might as well actually quit the nvim-r session right?

In my case, I use the space bar to send lines, and it is not impossible that I press the space bar by mistake after quitting R. So, I don't think it is a good idea to restart R when trying to send a command to it. Moreover, I have already commented that starting R means also waiting for nvimcom.

@petobens
Copy link
Contributor Author

petobens commented May 7, 2018

Are you willing to consider to add an option to enable this behaviour?

@jalvesaq
Copy link
Owner

jalvesaq commented May 7, 2018

It's not easy to implement. The starting point is below, but it's buggy and I won't fix it:

https://github.com/jalvesaq/Nvim-R/tree/auto_start

If you want to implement it, you have to do it not only for R running in an external terminal emulator, but also for R running in a Vim buffer, in a Neovim buffer, and as the RGui.exe (on Windows). Please, don't ask me to do this.

@petobens
Copy link
Contributor Author

petobens commented May 8, 2018

If you want to implement it, you have to do it not only for R running in an external terminal emulator, but also for R running in a Vim buffer, in a Neovim buffer, and as the RGui.exe (on Windows). Please, don't ask me to do this.

Wow, that's a lot of different scenarios. Mm I'm definitely not skilled enough to implement it myself :(

@petobens
Copy link
Contributor Author

petobens commented Apr 3, 2019

You could try checking if the value of g:rplugin_nvimcom_port no longer is 0

Hi @jalvesaq . I believe this is no longer working. After running call StartR('R') I now always get

E121: Undefined variable: g:rplugin_nvimcom_port

Any pointers? Thanks!

@jalvesaq
Copy link
Owner

jalvesaq commented Apr 3, 2019

For cosmetic reasons (to avoid too many global variables), I turned g:rplugin into a dictionary. Please, try g:rplugin.nvimcom_port.

@petobens
Copy link
Contributor Author

petobens commented Apr 3, 2019

Thank you!

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

No branches or pull requests

3 participants