You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ability to run code from @examples in Roxygen lines #147
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.
Off the top of my head, I think this could be done with a couple of modifications:
In src/selection.ts: Before or at the start of extendSelection(), check whether the line starts with #'
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!
@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.
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!
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.
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*#'.
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? 🤓
move the cursor at the upper left corner of the example, start of the first line of the example code
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.
Now simply copy and paste the code to the R console.
optionally 1. could be done with Selection tab and Column selection option
Reacted by Alexandre Courtiol and Daniel Vartanian
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).
Activity
andycraig commentedon Nov 27, 2019
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:
src/selection.ts: Before or at the start ofextendSelection(), check whether the line starts with#'src/lineCache.ts: ModifycleanLine()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!
nathaneastwood commentedon Nov 27, 2019
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
andycraig commentedon Nov 27, 2019
@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()intest/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 hittingCtrl-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 ingetSelection()after the call tocurrentDocument.getText().Thank you again for volunteering, and let me know if you have any trouble!
renkun-ken commentedon Nov 27, 2019
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:
Sending the selection should not convert
#' test(1)totest(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.gowerc commentedon Feb 4, 2022
@nathaneastwood , did you ever put any code together for this? If not I was thinking of trying to pick it up
nathaneastwood commentedon Feb 5, 2022
No I didn't.
Polkas commentedon Mar 8, 2022
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
to sth like, pseudo code.
amarquard commentedon May 16, 2022
Just switched to VS code from RStudio and really missing this feature
pat-s commentedon Jun 13, 2022
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? 🤓
andycraig commentedon Jun 13, 2022
@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 ...
nathaneastwood commentedon Jun 13, 2022
Nothing from me either, I'm too busy these days.
Polkas commentedon Jul 3, 2022
For me the work around is to use sth like:
optionally 1. could be done with Selection tab and Column selection option
marklhc commentedon Jan 15, 2023
Would love to see this feature too. That's one thing I really miss in RStudio when developing packages.
github-actions commentedon Jan 16, 2024
This issue is stale because it has been open for 365 days with no activity.
gowerc commentedon Jan 16, 2024
not stale
MOB-Habitat commentedon Aug 1, 2025
Is there any update on this :) Maybe there is another solution but haven't found it.
fawda123 commentedon Aug 15, 2025
Expressing my desire for this as well. Just made the switch from RStudio to Positron and am also missing this functionality.
ThomasSoeiro commentedon Sep 23, 2026
the concern raised by @renkun-ken in #147 (comment) has been ignored
@renkun-ken @eitsupi @Fred-Wu @grantmcdermott Should it be closed?
eitsupi commentedon Sep 23, 2026
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).
grantmcdermott commentedon Sep 25, 2026
I agree that it would be useful. Something to look at after 3.0.0 is released.