Skip to content

Implement R Notebook execution and output in visual mode - #7666

Merged
jmcphers merged 44 commits into
masterfrom
feature/visual-mode-notebook-output
Sep 4, 2020
Merged

jmcphers merged 44 commits into
masterfrom
feature/visual-mode-notebook-output

Conversation

@jmcphers

Copy link
Copy Markdown
Member

Intent

The goal of this change to achieve feature parity between R Notebooks in code view and visual mode.

image

Towards this end, it adds support for many notebook features, such as:

  • Inline output (view output of R expressions, plots, HTML widgets, etc. beneath code)
  • Chunk execution tools and semantics (run chunks in any supported language, queue chunks to run later, auto run setup chunk, etc.)
  • Chunk execution visualization (see and/or interrupt execution progress and error sources)
  • UI for editing chunk options
  • Fluid transition between editing modes (notebook state is consistent across visual and source mode)

etc.

Approach

Code Sharing

In order to make the behavior as similar as possible to traditional R notebooks, I've reused as much of the existing code as possible, refactoring where necessary. Though the UI is in the visual editor, most of the operations that involve code execution invisibly run an AST -> Markdown conversion if necessary and then run against the "parent" Ace instance. In addition to allowing us to re-use a great deal of existing code, this makes it possible for the editors to share a common execution state.

Widgets like the chunk toolbar and output UI wrappers now have two different versions that share most code but have separate implementations for code view and visual mode.

Matching and Tracking Chunks

In order to figure out which chunk in visual mode corresponds to the same chunk in source mode, I've added a couple of new tools. The first is a position field in the R Markdown chunk capsule, which indicates the index in the original Markdown where the chunk was discovered; this is used when initially loading the NodeView hosting the chunk. The second is used once both editors (visual and code) are both warm; it compares the chunk lists from both editors and synchronizes positions from code view into visual mode.

This is of particular interest since all of the R Notebook storage is row-based. In code view, a line widget conveniently tracks which row a chunk is on (and Ace updates it for us as the row moves around); in visual mode we can't rely on line widgets and instead rely on a synchronized scope tree to tell us where chunks begin and end so we can write the correct definitions to disk.

Mode Switches

For the most part, visual mode and code view each have their own UI widgets. The exception is the ChunkOutputWidget; these often have a lot of content and it would be wasteful (and complicated) to try to maintain two copies of it. When switching modes, we do a bunch of work to detach all of the notebook's output from the old view and reattach it to the new view. This makes it possible for us to show the output identically across modes.

QA Notes

Anything that works in an R Notebook in code view should generally work the same way in visual mode, so a good start for testing is to enumerate R Notebook features (see the R Notebooks section in the R Markdown Definitive Guide, for example) and sanity test them in visual mode if it makes sense to do so (happy to consult on gray areas of applicability or bits we don't implement).

The largest risks here are around the alignment of code view with visual mode; especially around synchronizing output between modes, saving the right outputs with the right code for the R Notebook preview file (.nb.html), and executing the right code (as the code being executed comes from a different source than what's actually visible to the user).

Draft Note

This PR is ready for feedback, but not for merge as there are a couple of quirks and known bugs that need to be addressed first. I'm going to be OOO from now until mid next week, and will address feedback when I return.

jmcphers added 30 commits August 7, 2020 21:35
private maxContentWidth = 0;
private minContentPadding = 0;

// keep track of whether the last transaction was selection-only

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't yet seen the GWT side of this, but the original purpose of this was to leave the selection entirely alone if the user had been editing. This is b/c in this case the selection will already be in the perfect location (b/c we are replaying diffs unrolled to inserts/deletes/etc. into source mode as the user edits). It's possible that we've lost this behavior as a result of this change. Let's discuss before you do anything here though both to explore alternative ways to preserve the behavior as well as to evaluate the priority level of preserving it.

public static final Pattern RE_SWEAVE_CHUNK_END =
Pattern.create("^\\s*@\\s*$", "");

public static final Pattern RE_EMBEDDED_R_CHUNK_BEGIN =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to capture all valid knitr chunks in a file? If so it may not get indented chunks or chunks inside a blockquote (see knitr regex patterns and/or patterns we use in source capsule detection).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't; it is only used inside the embedded Ace instances (EMBEDDED) where we thankfully don't have to worry about that.

return;
}

// we are about to mutate the document, so create a single

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may have some pretty funky merge conflicts here as I took part of this function and wrapped it in a handler that does some prompting the first time visual mode is activated and/or when we may plow over column or sentence based line wrapping. Here's the gist of my change: 0c4edf4#diff-06b7d8ba9d7ace26762c2194678a2fd8

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will attempt to resolve and get your review when complete!

@jmcphers
jmcphers marked this pull request as ready for review September 4, 2020 23:01
@jmcphers
jmcphers merged commit fbea831 into master Sep 4, 2020
@valerie-rstudio
valerie-rstudio deleted the feature/visual-mode-notebook-output branch January 21, 2022 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants