vscode-R icon indicating copy to clipboard operation
vscode-R copied to clipboard

"Run Current Chunk" command for R scripts.

Open chadbot opened this issue 4 years ago • 6 comments

One of the nicest features of working in RMarkdown files is the ability to send whole code chunks (or multiple chunks) to the terminal with a single command/shortcut. It would be great to implement something similar in R files based on code blocks.

For example, consider the following code. When the cursor is anywhere in block_a (including the block indicator itself), the command would execute (send to terminal) all of the code between block_a and block_b (and possibly even move the cursor to the first line of the block_b).

## block_a ----

x <- rnorm(100)

## block_b ----

hist(x)

R code blocks follows a pretty simple pattern, and the extension's outline functionality already recognizes these blocks as a way of organizing R scripts, so I imagine (hope?) this would be relatively easy to implement.

chadbot avatar Aug 30 '21 13:08 chadbot

Thanks for the request! At the moment, we support jupyter-style code chunks in R documents. I.e.

# %% chunk 1
x <- 5
# %% 

# %% chunk 2
x + 5
# %% 

ElianHugh avatar Aug 31 '21 02:08 ElianHugh

Currently, the ## block_a ---- style is only supported in document outline provided by language server to be consistent with RStudio, while the Jupyter style code chunk # %% chunk 1 is supported in vscode-R.

renkun-ken avatar Aug 31 '21 03:08 renkun-ken

The ## block_a ---- style is similar to ## title in Markdown. It is considered as a heading rather than code chunk. # %% chunk 1 is similar to Rmd code chunks.

I guess we could add # %% chunk to document outline as code chunks in language server to be consistent with Rmd.

renkun-ken avatar Aug 31 '21 03:08 renkun-ken

Aha! I had no idea Jupyter-style chunks were supported in R. This is basically the functionality I was looking for. Just tested it out and it works great.

The one nice thing about using ## block_a ---- indicators is that these chunks can be referenced and run in RMarkdown files (as shown here). So there's a direct relationship between the R chunk and an associated Rmd. When writing/testing a chunk that's referenced in an Rmd file, it's nice to be able to quickly run the whole thing at once.

Of course, it's easy enough to just use both chunk indicators as needed. But I wonder if there's a simple way to let users choose which style chunks they're using in R--or something along those lines.

chadbot avatar Aug 31 '21 11:08 chadbot

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

github-actions[bot] avatar Sep 01 '22 02:09 github-actions[bot]

unstale

ElianHugh avatar Sep 01 '22 02:09 ElianHugh

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

github-actions[bot] avatar Sep 03 '23 01:09 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Sep 17 '23 01:09 github-actions[bot]