Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code appears before plots when using figure layouts #41

Open
andrewpbray opened this issue Jun 28, 2024 · 4 comments
Open

Code appears before plots when using figure layouts #41

andrewpbray opened this issue Jun 28, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@andrewpbray
Copy link
Contributor

andrewpbray commented Jun 28, 2024

In format: html, the following document will first show the echoed code, then a two column layout with a total of three plots and a data frame. In format: closeread-html, the echoed code shows up after the two column layout.

---
title: "Orphaned code output test"
format: closeread-html
---

:::{.cr-layout}

Test test test

:::{.cr-crossfade cr-to="big-code"}
Test test test
:::

Test test test

```{r}
#| echo: true
#| cr-id: big-code
#| layout-ncol: 2

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

plot(mtcars$mpg, mtcars$dist)

plot(mtcars$hp, mtcars$dist)

plot(mtcars$hp, mtcars$mpg)

mtcars
\```

:::

@andrewpbray andrewpbray added the bug Something isn't working label Jun 28, 2024
@andrewpbray andrewpbray self-assigned this Jun 28, 2024
@andrewpbray
Copy link
Contributor Author

Initial diagnosis

When our lua filter encounters the layout div, in the AST the code cell looks like:

Div {.cell cr-id="big-code" layout-ncol="2"}
Blocks:

  1. CodeBlock {.r .cell-code}
  2. Div {.cell-output-display}
    Blocks:
    1. Image
  3. CodeBlock {.r .cell-code}
  4. Div {.cell-output-display}
    Blocks:
    1. Image
  5. CodeBlock {.r .cell-code}
  6. Div {.cell-output-display}
    Blocks:
    1. Image
  7. CodeBlock {.r .cell-code}
  8. Div {.cell-output-display}
    Blocks:
    1. CodeBlock

That is, it separates the code into different CodeBlocks based on the lines that produce output, then interleaves those CodeBlocks with output (3 images and a CodeBlock to print the dataframe).

At the end of our lua filter, the whole DivAbove is wrapped into two more Divs:

Div {.sticky-col}
Blocks:

  1. Div {.sticky-col-stack}
    Blocks:
    DivAbove

That looks about right! The fact that in the flat html format the CodeBlock is not separated and interleaved into the output suggests the filter for layout-ncol runs after our filter does to rearrange those blocks. That rearrangement must be breaking out Div wrapper structure.

Next steps: write a separate lua filter that runs late to see if we can catch the AST post-layout filter.

@andrewpbray
Copy link
Contributor Author

andrewpbray commented Jun 28, 2024

@jimjam-slam Oy, this was a deep cut:

quarto-dev/quarto-cli#10174

I think the issue is that we're looking for cr-id's which no longer are parents to the CodeBlock, so it gets treated as a separate thing.

@jimjam-slam
Copy link
Collaborator

Dang, nice work on the diagnosis!

@jimjam-slam
Copy link
Collaborator

jimjam-slam commented Sep 23, 2024

Circling back to this and updating the test case following the shift to Quarto cross-ref style trigger references:

---
title: "Orphaned code output test"
format:
  closeread-html:
    theme: cosmo
---

:::{.cr-section}

Test test test

Test test trigger big code @cr-big-code

Test test test

:::{#cr-big-code}
```{r}
#| echo: true
#| layout-ncol: 2

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

a <- rnorm(100)
b <- 1 + rexp(100)
c <- a + b

plot(mtcars$mpg, mtcars$dist)
plot(mtcars$hp, mtcars$dist)
plot(mtcars$hp, mtcars$mpg)
mtcars

```
:::

:::

All done!

On Safari:

Screenshot 2024-09-24 at 09 42 28

So there's still a layout problem even though we encourage folks to wrap the entire code cell in a fenced div (rather than adding id: cr-... directly to the code cell.

But the ID is in the right place, so it's no longer a problem with the figure layout processing — I'm assuming this is one of the many possible instances of #78 (ie. what we do with mixed content stickies).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants