Quarto在条件语句内部未正确呈现图表。

huangapple go评论179阅读模式
英文:

Quarto not rendering figures properly inside a conditional statement

问题

我正在使用 quarto 创建一个 HTML 文档。我想要有一些并排的图表。

当我尝试在条件语句内部呈现并排的图表时,quarto 只呈现第二个图表。下面是一个可重现的示例。


title: "RenderTest"
format: html

  1. library(tidyverse)

直接的图表 - 无条件

  1. #| layout-ncol: 2
  2. #| out-width: "50%"
  3. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  4. ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()

现在加入条件:

  1. #| layout-ncol: 2
  2. #| out-width: "50%"
  3. test <- F
  4. if(test){
  5. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  6. } else {
  7. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  8. ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
  9. }

这会产生以下输出:

Quarto在条件语句内部未正确呈现图表。

你可以看到,在条件语句内部,只有第二个图表被显示出来。我很困惑。这是一个 bug 吗?还是有一些基本的东西我不理解?

英文:

I am using quarto to create an html document. I want to have some side-by-side plots.

When I try to render side-by-side plots from inside of a conditional statement, quarto only renders the second plot. Below is a reproducible example.

  1. ---
  2. title: &quot;RenderTest&quot;
  3. format: html
  4. ---
  1. library(tidyverse)

Straight-up plot - No Conditional

  1. #| layout-ncol: 2
  2. #| out-width: &quot;50%&quot;
  3. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  4. ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()

Now with conditional:

  1. #| layout-ncol: 2
  2. #| out-width: &quot;50%&quot;
  3. test &lt;- F
  4. if(test){
  5. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  6. } else {
  7. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  8. ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
  9. }

This produces the following output:

Quarto在条件语句内部未正确呈现图表。

You can see that inside the conditional, only the second plot gets displayed. I'm very confused. Is this a bug? Is there something fundamental I don't understand?

答案1

得分: 1

  1. 你应该像这样“打印”ggplot调用:
  2. ---
  3. title: "RenderTest"
  4. format: html
  5. ---
  6. ```{r}
  7. library(tidyverse)
  8. ```
  9. ```{r}
  10. #| layout-ncol: 2
  11. #| out-width: "50%"
  12. test <- FALSE
  13. if(test){
  14. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  15. } else {
  16. print(ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point())
  17. print(ggplot(mtcars, aes(x = hp, y = disp)) + geom_point())
  18. }
  19. ```
  20. 输出:
  21. [![enter image description here][1]][1]
  22. [1]: https://i.stack.imgur.com/LbzvS.png
英文:

You should print the ggplot calls like this:

  1. ---
  2. title: &quot;RenderTest&quot;
  3. format: html
  4. ---
  5. ```{r}
  6. library(tidyverse)
  7. ```
  8. ```{r}
  9. #| layout-ncol: 2
  10. #| out-width: &quot;50%&quot;
  11. test &lt;- FALSE
  12. if(test){
  13. ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  14. } else {
  15. print(ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point())
  16. print(ggplot(mtcars, aes(x = hp, y = disp)) + geom_point())
  17. }
  18. ```

Output:

Quarto在条件语句内部未正确呈现图表。

huangapple
  • 本文由 发表于 2023年3月9日 22:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定