Skip to content

Running chunk should submit code in single "block" #3520

Closed

Description

@hadley

I'm not quite sure how to explain this, but ideally here's a reprex:

  1. Create an Rmarkdown file and add this chunk:

    ```{r}
    x <- 1
    1
    ```
    
  2. Execute the chunk by pressing command-shift-enter

  3. Navigate to the console and press up arrow

Ideally the history would contain the entire chunk, rather than a series of individual lines

Activity

  1. ronblum commented on Sep 21, 2018

    @ronblum
    Contributor

    Are you looking for something similar to when multiple lines are entered at once in the console?

    1. x <- 1
    2. Press shift-enter
    3. 1
    4. Press enter
    5. Press the up-arrow key

    chunk_history

  2. hadley commented on Sep 21, 2018

    @hadley
    MemberAuthor

    Yes, exactly!

  3. jmcphers commented on Sep 21, 2018

    @jmcphers
    Member

    It works this way because we need R to evaluate each line individually -- that way we can avoid sending further lines to R when an error occurs, and can also track the production of artifacts such as plots to their originating lines.

    We might be able to paper over this in the history so that it still looks like all of the lines went to R in one go.

  4. ClaytonJY commented on Sep 25, 2018

    @ClaytonJY

    @jmcphers could a single expression at least be one blob in the history, as it is for non-Rmd code? E.g. an expression like

    data.frame(
      x = 1,
      y = 2
    )

    appears in history line-by-line when run from an Rmd (whether the whole chunk or ctrl-enter), but is one history entry when run from an R script (ctrl-enter, shift-enter, etc) or the console as demonstrated above.

    I think the expression-specific behavior is a regression, as at some point in the past the expressions were combined in history, though I couldn't tell you how long ago that was; at least 3 months, possibly much longer, and I tend to run latest-release versions.

  5. pqmatagi commented on Jan 2, 2020

    @pqmatagi

    Belated vote for a change here.

    Perhaps a separate issue, but for executing a selection from within a code chunk, I definitely think the approach for source files should be followed, i.e. a single blob as noted above. This use case is much more likely to involve subsequent history fiddling by the user than the full chunk, I suspect. And it's just plain inconsistent.

    Even in the complete chunk case, executing top level expression by expression makes much more sense than line by line, if the parsing can be done to allow this. (No error or artifact linking downside, is there?)

    Note that line by line history with incomplete statements is not only pretty useless for experimental modifications of the executed code from the console, it makes it painful to navigate to other parts of the history that may or may not have come from the notebook code.

    I also feel like there's been a regression, though I can't validate that (and wasn't there multi-line functionality in the History pane at one point?).

  6. MarcoPortmann commented on Jan 10, 2020

    @MarcoPortmann

    For quite some time I thought something was fundamentally wrong with the debugging options of RStudio. From another R IDE, I was used that the following code would stop at Var1 <- MyFun(Var1) such that Var1 finally takes the value 12 and not 14 when the code is send using ctrl+enter.

    Var1 <- 12
    MyFun <- function(x) x/"Error"
    Var1 <- MyFun(Var1) 
    
    Var1 <- Var1 + 2
    Var1
    

    I am clearly in favour of sending the text selection in the editor as a block and not line by line. It would have great advantages for debugging, since commands after buggy code wouldn't spoil any objects.

  7. jmcphers commented on Mar 30, 2020

    @jmcphers
    Member

    Another vote for this over in #4503.

  8. pqmatagi commented on Apr 14, 2020

    @pqmatagi

    To me, having different history behavior for "execute selection", depending on whether the selection is in a .Rmd file or a .R file, is a bug. Making them consistent is not an enhancement, it's how it should always have worked. If you feel this is different from the original issue posted, I'm happy to open a new one!

  9. renozao commented on Apr 27, 2020

    @renozao

    This issue makes writing analysis notebooks very cumbersome in practice. A fix would be greatly appreciated.
    A workaround is to write the notebook as an R script and process them with rmarkdown::render (~ spin + knitr), but then one does not get all the other features of Rmd notebooks (chunk submission, embedded graphs, etc...).

  10. pqmatagi commented on Apr 29, 2021

    @pqmatagi

    Thanks for reopening!

    Please keep in mind that despite the OP topic, there are multiple scenarios, as mentioned in the other comments:

    1. Running a whole chunk (this topic)
    2. Executing a selection via crtl-enter/cmd-enter
    3. Executing a surrounding or adjacent block via crtl-enter/cmd-enter (topic of RMarkdown function definitions execute line-by-line #4503)

    I believe 2. and 3. should behave like they do for a .R file (from the console history point of view, at least). For 1. I would prefer the @ClaytonJY suggestion of grouping expression by expression (i.e. act as though 2. was used for all the chunk code at once), but the @hadley ask of the whole chunk as a single history item would be an improvement.

    Related issue, perhaps requires a separate item, but it would be nice to think this would also fix it: it seems like we lost grouped line selection in the History pane at some point in the product history. A user almost never wants to select a single line from a multi-line expression in the History pane to copy to source or console, but to get the whole expression requires extending the selection with shift-click or some such operation. Annoying.

  11. bigO6377 commented on Jan 15, 2022

    @bigO6377

    Hey guys, is there any update/fix to these issues? Namely:

    • Scrolling upward in console will show a multiline block of code as individual lines when the code was executed from a .Rmd (though for code executed from a .R file, it does show the entire code block as desired)
    • History tab not grouping multiline commands into a single entity (regardless of whether executed from a .R, a .Rmd, or from the console directly

    Using the following version/OS

    > RStudio.Version()
    $citation
    
    To cite RStudio in publications use:
    
      RStudio Team (2021). RStudio: Integrated Development Environment for R. RStudio, PBC, Boston, MA URL
      http://www.rstudio.com/.
    
    A BibTeX entry for LaTeX users is
    
      @Manual{,
        title = {RStudio: Integrated Development Environment for R},
        author = {{RStudio Team}},
        organization = {RStudio, PBC},
        address = {Boston, MA},
        year = {2021},
        url = {http://www.rstudio.com/},
      }
    
    
    $mode
    [1] "desktop"
    
    $version
    [1] ‘2021.9.0.351’
    
    $long_version
    [1] "2021.09.0+351"
    
    $release_name
    [1] "Ghost Orchid"
    
  12. brooksambrose commented on Aug 23, 2022

    @brooksambrose

    I would consider it a feature if any solution here would also prevent subsequent lines from being fed to an empty readline command that is waiting for user input. For example, we might expect the following behavior when sourcing this block:

    x<-readline() # user enters something
    rm(x)
    x
    #> Error in eval(expr, envir, enclos): object 'x' not found

    Yet running it as a chunk yields:

    > x<-readline()
    rm(x)
    > x
    [1] "rm(x)"
  13. added this to the Backlog-3 milestone on Jul 10, 2023
  14. teecrow commented on Jul 18, 2023

    @teecrow

    I agree with @pqmatagi and others re: the issue with the console history after running a selection from a code chunk in rmarkdown RMD or quarto QMD files. Is this "selection" problem considered the same as the "Full chunk" problem and thus being tracked in this Issue? I often use Ctrl-Enter to run a multi-line operation (joined by pipes) in a chunk, and this more or less ruins the console history because each individual line (i.e., that which is captured by the history) is useless on its own. Would be nice to see consistent behavior with .R files for this, since it's highly useful for tinkering in interactive mode.

  15. modified the milestones: Backlog-3, Later on Oct 5, 2023
  16. self-assigned this
    on Oct 28, 2024
  17. ronblum commented on Oct 29, 2024

    @ronblum
    Contributor

    Verified in RStudio Desktop 2024.11.0-daily+331 on Windows 11.

    Used variations of the examples above, and guidance from the QA notes:

    Execution of a single chunk's contents (via the Run button),
    Execution of a subset of code in a chunk (via Cmd + Enter),
    Execution of all chunks in a document (via Run All or similar)

    Checked both Console Pane, which groups the chunks, and the History Pane, which treats lines separately. Checked both Rmd and Qmd. With "run all", the chunks are correctly treated as separate blocks, not all as one.

    Question: I found interesting behavior with R scripts. When using Ctrl-Shift-Enter, the commands are sent to the Console Pane and executed correctly. However, scrolling up in the commands, and in the History Pane, we don't get the code. Instead, we get

    source("~/.active-rstudio-document", echo=TRUE)
    

    @kevinushey Is this expected? Asking here before opening a bug, in case it's what should happen.

  18. ronblum commented on Oct 29, 2024

    @ronblum
    Contributor

    Moving to "Ready" because of the question; otherwise, this issue is fixed.

  19. kevinushey commented on Oct 29, 2024

    @kevinushey
    Collaborator

    That is expected; it's an artifact of how Ctrl + Shift + Enter works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions