R语言 夸托无法正确呈现条件语句中的数字

7hiiyaii  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(107)

我正在使用夸托创建一个html文档。我想有一些并排的情节。
当我试图从一个条件语句内部呈现并排图时,夸托只呈现第二个图。下面是一个可重复的例子。

---
title: "RenderTest"
format: html
---
library(tidyverse)

直线图-无条件

#| layout-ncol: 2
#| out-width: "50%"

ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()

现在有条件:

#| layout-ncol: 2
#| out-width: "50%"

test <- F

if(test){
  ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
} else {
  ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
  ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
}

这将生成以下输出:

你可以看到在条件函数中,只有第二个图被显示出来,我很困惑,这是一个bug吗?是不是有什么基本的东西我不明白?

fwzugrvs

fwzugrvs1#

您应该像这样print ggplot调用:

---
title: "RenderTest"
format: html
---

```{r}
library(tidyverse)
#| layout-ncol: 2
#| out-width: "50%"

test <- FALSE

if(test){
  ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
} else {
  print(ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point())
  print(ggplot(mtcars, aes(x = hp, y = disp)) + geom_point())
}

输出:

![](https://i.stack.imgur.com/LbzvS.png)

相关问题