Skip to content

Add ability to run code from @examples in Roxygen lines #147

Description

@nathaneastwood

In RStudio, a really neat feature is the ability to run code written next to Roxygen tage (#') by using Ctrl (Cmd) + Enter. The Roxygen tags do not get sent to the console. In VSCode I have to highlight the code and hit Ctrl (Cmd) + Enter. Not a particularly huge concern, but a nice to have.

An example:

#' print("hello world")
my_fun <- function() ...

Having the cursor on the first line and hitting Ctrl (Cmd) + Enter would produce [1] "hello world" in RStudio but in VSCode it would send my_fun to the console.

Activity

  1. andycraig commented on Nov 27, 2019

    @andycraig
    Collaborator

    Hi, I really like this feature from RStudio too.

    Off the top of my head, I think this could be done with a couple of modifications:

    1. In src/selection.ts: Before or at the start of extendSelection(), check whether the line starts with #'
    2. In src/lineCache.ts: Modify cleanLine() to take a parameter that allows it to remove #' from the start of a line if identified as being in 'Roxygen mode' from 1.

    In src/selection.ts, checkForBlankOrComment() might need a modification too.

    I can probably do this some time in 2020, but very happy to provide assistance to anyone who'd like to have a go at implementing it a bit earlier!

  2. nathaneastwood commented on Nov 27, 2019

    @nathaneastwood
    Author

    I've never developed in TypeScript but I'm happy to give it a go - it can't be too different from R or Python, right? :D

  3. andycraig commented on Nov 27, 2019

    @andycraig
    Collaborator

    @nathaneastwood Fantastic! I’ll remove the ‘help wanted’ tag. The very first TypeScript I wrote was for sending code to the terminal too, so it seems like an appropriate place to start :)

    There are unit tests for extendSelection() in test/extension.test.ts. I suggest adding some for code written next to Roxygen comments. Then you can develop by running them, which is much quicker than repeatedly firing up R consoles and hitting Ctrl-Enter.

    There are some brief instructions on how to run a development version of vscode-R here: https://github.com/Ikuyadeu/vscode-R/blob/master/CONTRIBUTING.md

    I also recommend installing the ‘tslint’ VSCode extension.

    I thought of one more modification that will probably be necessary, which is removing the #' again in getSelection() after the call to currentDocument.getText().

    Thank you again for volunteering, and let me know if you have any trouble!

  4. renkun-ken commented on Nov 27, 2019

    @renkun-ken
    Member

    I'm wondering which is the desired behavior: if the selection contains both #'-started comments and normal code, it seems we should treat the comments as comments.

    For example, if selection contains the whole block of the following code:

    #' Test code
    #' @examples
    #' test(1)
    test <- function(x) {
      x + 1
    }

    Sending the selection should not convert #' test(1) to test(1) and should send only the function code to terminal.

    It seems that the correct behavior should be if the selection only contains #' lines, then #' should be removed and sent to terminal. Otherwise, the comment should be left as it is.

  5. gowerc commented on Feb 4, 2022

    @gowerc
    Contributor

    @nathaneastwood , did you ever put any code together for this? If not I was thinking of trying to pick it up

  6. nathaneastwood commented on Feb 5, 2022

    @nathaneastwood
    Author

    No I didn't.

  7. Polkas commented on Mar 8, 2022

    @Polkas

    I am not a TypeScript player still this looks to be only a one regex to add ...

    We want to update this function

    The core source function

    Now for each line (this is a string) we want to replace ^\s*#'\s* with nothing.
    it will be great to have some validation that each line in selection start with ^\s*#'.

    from

        if(useRepl && vscode.debug.activeDebugSession?.type === 'R-Debugger'){
            await sendRangeToRepl(selection.range);
        } else{
            await runTextInTerm(selection.selectedText);
        }
    

    to sth like, pseudo code.

        if(useRepl && vscode.debug.activeDebugSession?.type === 'R-Debugger'){
            await sendRangeToRepl(selection.range);
        } else{
            var txt = selection.selectedText;
            // this if for performance
            if (txt.includes("#'")) {
              var txt_lines = txt.split("\n")
              var txt_lines_clean = txt_lines.forEach(function (value) {
                 value.replace(/^\s*#'\s*/, "")
               })
              txt = txt_lines_clean.join("\n")
            }
            await runTextInTerm(txt);
        }
    
  8. amarquard commented on May 16, 2022

    @amarquard

    Just switched to VS code from RStudio and really missing this feature

  9. pat-s commented on Jun 13, 2022

    @pat-s

    Using vscode more often (again) lately, I was also heavily missing this one - @andycraig @nathaneastwood any plans/motivation to tackle this in the near future? 🤓

  10. andycraig commented on Jun 13, 2022

    @andycraig
    Collaborator

    @pat-s No plans from me I'm afraid - I miss this feature when I use R but these days I don't use R very much ...

  11. nathaneastwood commented on Jun 13, 2022

    @nathaneastwood
    Author

    Nothing from me either, I'm too busy these days.

  12. Polkas commented on Jul 3, 2022

    @Polkas

    For me the work around is to use sth like:

    1. move the cursor at the upper left corner of the example, start of the first line of the example code
    2. click with the mouse holding down shift + alt (on mac shift + option), on the down right corner of the code block. It is important that the mouse click is on the right (column height) of the most extreme line of code.
    3. Now simply copy and paste the code to the R console.

    optionally 1. could be done with Selection tab and Column selection option

  13. marklhc commented on Jan 15, 2023

    @marklhc

    Would love to see this feature too. That's one thing I really miss in RStudio when developing packages.

  14. github-actions commented on Jan 16, 2024

    @github-actions

    This issue is stale because it has been open for 365 days with no activity.

  15. gowerc commented on Jan 16, 2024

    @gowerc
    Contributor

    not stale

  16. MOB-Habitat commented on Aug 1, 2025

    @MOB-Habitat

    Is there any update on this :) Maybe there is another solution but haven't found it.

  17. fawda123 commented on Aug 15, 2025

    @fawda123

    Expressing my desire for this as well. Just made the switch from RStudio to Positron and am also missing this functionality.

  18. ThomasSoeiro commented on Sep 23, 2026

    @ThomasSoeiro
    Collaborator

    the concern raised by @renkun-ken in #147 (comment) has been ignored

    @renkun-ken @eitsupi @Fred-Wu @grantmcdermott Should it be closed?

  19. eitsupi commented on Sep 23, 2026

    @eitsupi
    Member

    I believe this is technically feasible, but I'm unsure if it's possible with the current vscode-R architecture.
    Contributions are welcome (though it's not my priority).

  20. grantmcdermott commented on Sep 25, 2026

    @grantmcdermott
    Contributor

    I agree that it would be useful. Something to look at after 3.0.0 is released.

  21. added this to the 3.x milestone on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions