简体   繁体   English

Quarto 中 output 块的滚动条

[英]Scrollbar for output chunk in Quarto

I would like to plot multiple plots from a chunk and add a scrollbar to the output of that chunk.我想从一个块中绘制 plot 多个图,并向该块的 output 添加一个scrollbar I see here that this could be done for Code Overflow, but I am not sure how to scroll the output instead of adding all the plots below each other like in the example below:在这里看到这可以针对代码溢出来完成,但我不确定如何滚动 output 而不是像下面的示例那样将所有图表添加到彼此下面:

---
title: "Scrollbar in output chunk"
format:
  html:
    code-overflow: wrap
---

Here is some example code:

```{r}
#| code-overflow: wrap
library(ggplot2)
for(i in unique(iris$Species)) {
  print(
    ggplot(iris[iris$Species == i, ], aes(x = Sepal.Length, Sepal.Width)) +
      geom_point()
  )
}
```

Output: Output:

在此处输入图像描述

As we can see from the output, all the plots are shown below each other, but I would like to have a scrollbar chunk so it doesn't show all plot at once.正如我们从 output 中看到的那样,所有的图都显示在彼此下方,但我想要一个滚动条块,这样它就不会一次显示所有 plot。 So I was wondering if anyone knows how to add a scroll option to the output of a chunk in Quarto ?所以我想知道是否有人知道如何向Quarto中的块的 output 添加滚动选项?

You can create a css file with a defined max-height and overflow-y and add it to your chunk with class .您可以创建一个 css 文件,其中定义了max-heightoverflow-y并使用class将其添加到您的块中。 Note that this will also include the code in the scrolling section.请注意,这还将包括滚动部分中的代码。 Also note that if you want a text output to be scrollable, you should use class-output instead of class in your chunk options.另请注意,如果您希望文本output 可滚动,则应在块选项中使用class-output而不是class

---
title: "scrollable-output"
format: html
---

```{css, echo = FALSE}
.output {
max-height: 500px;
overflow-y: scroll;
}
```

Here is some example code

```{r}
#| class: output
library(ggplot2)
for(i in unique(iris$Species)) {
  print(
    ggplot(iris[iris$Species == i, ], aes(x = Sepal.Length, Sepal.Width)) +
      geom_point()
  )
}
```

在此处输入图像描述

You can add a div before the chunk, eg您可以在块之前添加一个 div,例如

---
title: "Scrollbar in output chunk"
format: html
    
css: styles.css
---

Here is some example code:

:::{.scrolling}

```{r}

library(ggplot2)
for(i in unique(iris$Species)) {
  print(
    ggplot(iris[iris$Species == i, ], aes(x = Sepal.Length, Sepal.Width)) +
      geom_point()
  )
}
```

:::

styles.css styles.css

.scrolling {
  max-height: 500px;
  overflow-y: auto;
}

在此处输入图像描述

If you do not want the scrolling for the code, than you could do this:如果您不想滚动代码,则可以这样做:

---
title: "Scrollbar in output chunk"
format: html
    
css: styles.css
---

Here is some example code:

```{r}
#| eval: false

library(ggplot2)
for(i in unique(iris$Species)) {
  print(
    ggplot(iris[iris$Species == i, ], aes(x = Sepal.Length, Sepal.Width)) +
      geom_point()
  )
}
```

:::{.scrolling}

```{r}
#| echo: false

library(ggplot2)
for(i in unique(iris$Species)) {
  print(
    ggplot(iris[iris$Species == i, ], aes(x = Sepal.Length, Sepal.Width)) +
      geom_point()
  )
}
```

:::

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM