Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

👋 Hey - what an amazing effort this is to have R in vscode. Thanks to all.

Is there any way to enable the "re-run previous" in vscode to mimic the behaviour in the RStudio IDE? I am to be honest fairly confused how keybindings work. I have this one for run individual lines of code:

  {
    "key": "cmd+shift+enter",  
    "command": "editor.debug.action.selectionToRepl"
  },

Thanks!

You must be logged in to vote

Replies: 2 comments · 6 replies

Comment options

I can't think of a way to get 're-run previous' functionality at the moment. I don't think it would be too hard to add this feature if it's something you would be interested in implementing yourself.

I didn't realise 're-run previous' existed in RStudio. As a matter of interest, when do you usually use this feature? Run some code in the editor, write and run some code in the console to change some variable values, 're-run previous' to re-run the code with the updated variable values?

You must be logged in to vote
4 replies
@boshek
Comment options

Yeah - I specifically use it all the time with ggplot2 because you often change one parameter to see if it did what you want. Inevitably it doesn't so you change it again and re-run that same bit of code.

Any suggestions on where to start on implementing something like this? I'd love to contribute but haven't ever even approached messing around with vscode in this way.

@andycraig
Comment options

I experimented a little bit now in RStudio and here's my understanding of how Re-Run Previous works:

  1. If no text was selected when previous command was run, Re-Run Previous effectively runs Ctrl+Enter as if the cursor was still at the location it was at when code was run previously. That is, if the text has changed since the code was run, it intelligently selects text as if Ctrl+Enter was used again.
  2. If text was selected when previous command was run and it has not changed, Re-Run Previous runs that text again.
  3. If text was selected when previous command was run and it HAS changed, Re-Run Previous does nothing.

Here's where the vscode-R run commands are set:

// run code from editor in terminal
'r.nrow': () => rTerminal.runSelectionOrWord(['nrow']),
'r.length': () => rTerminal.runSelectionOrWord(['length']),
'r.head': () => rTerminal.runSelectionOrWord(['head']),
'r.thead': () => rTerminal.runSelectionOrWord(['t', 'head']),
'r.names': () => rTerminal.runSelectionOrWord(['names']),
'r.runSource': () => { void rTerminal.runSource(false); },
'r.runSelection': (code?: string) => { code ? void rTerminal.runTextInTerm(code) : void rTerminal.runSelection(); },
'r.runFromLineToEnd': rTerminal.runFromLineToEnd,
'r.runFromBeginningToLine': rTerminal.runFromBeginningToLine,
'r.runSelectionRetainCursor': rTerminal.runSelectionRetainCursor,
'r.runCommandWithSelectionOrWord': rTerminal.runCommandWithSelectionOrWord,
'r.runCommandWithEditorPath': rTerminal.runCommandWithEditorPath,
'r.runCommand': rTerminal.runCommand,
'r.runSourcewithEcho': () => { void rTerminal.runSource(true); },

Most (all?) of these are implemented in rTerminal.ts. In general, they select text from the editor and then call runTextInTerm(text).

When these commands are run, we would need to save the cursor location (if no selection) or the selection range and text (if there was a selection). Then, a new command Re-Run Previous would need to be added that chooses text based on that saved cursor location and then calls runTextInTerm(text).

Instructions on how to get set up to make edits to the vscode-R extension are here: https://github.com/REditorSupport/vscode-R/wiki/Contributing Hopefully they are clear but if you get stuck let us know.

@boshek
Comment options

Ok this is a great start and perusing rTerminal.ts has be very useful to know what I've been missing. That feels like the keys to the kingdom.

One question:

we would need to save the cursor location

Could you elaborate on this, specifically how these are saved? My typescript is a bit lacking though I can make sense of the most of it (especially since I've got the dev environment setup). But the save step has me a little confused. How/where is that happening? Even just an example would be much appreciated.

Thanks for helping me out here.

@andycraig
Comment options

Sure! At the moment these cursor locations aren't saved. We would need to create a variable to hold them, like this at around line 17:

let previousRunSelection: textEditor.selection | undefined = undefined

(Note that textEditor.selection is just my guess and might not be the right type here.)

Then for example in runSelection() at line 41 we would add something like

previousRunSelection = textEditor.selection

Then we'd add a new function like runPrevious() that would be something like

export async function runPrevious(): Promise<void> {
    if (previousRunSelection === undefined) {
        // no code has been run yet    
    }
    else if (previousRunSelection.start === previousRunSelection.end) {
        // previous run did not use a selection, so run the code as if cursor is still at previousRunSelection.start
    }
    // handle other cases
}
Comment options

You can also install Quarto along with it's VS Code extension then set keyboard shortcuts to do the following:

  • run all cells
  • run cells above
  • run cells below
  • run current cell
  • run selected lines

Pro:

  • works with other languages (Python, Julia)

Con:

  • another program & extension to install
You must be logged in to vote
2 replies
@boshek
Comment options

I definitely have quarto installed and use it all the time. It is just that re-run previous that I am really after.

@kvten
Comment options

Then either run cells above or run current cell would work. Unless you're running code from console/terminal?

Here's how I would tackle your situation:

library(ggplot)
data <- read_csv(...)
data %>%
   ggplot(aes(...) +
   geom_point()

Make your changes to code chunk 3 and use your run current cell keybind. Or run cells above if you made changes to chunks 1 or 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.