R Markdown无法在相同的If-Statement中显示两个图

发布于 2025-02-05 20:53:44 字数 1700 浏览 2 评论 0原文

我正在创建R-MAR​​KDOWN文件,以帮助在学校报告期末考试结果。对于数学考试,我需要有条件的语句以显示适当的地块,因为如果学生的书面分数高于一定的阈值,则不需要参加口试(口腔= NA)。因此,我有一个if stategent,可以检查oral_exam变量的总和(1对于那些必须服用的人,否则为0)大于零,如果是的,请创建一个3D散点图口腔检查标记为红色,然后是另一种类型的情节,只有必须参加口腔考试的学生,根据口腔检查结果进行彩色。如果没有一个学生必须参加口腔检查,则在以后的案件中进行检查,只会产生一个情节。我的代码看起来像这样:

```{r warning = FALSE, message = FALSE, echo = FALSE, eval = params$subj == "Matematika"}
if(sum(fulldata$Oral_exam) > 0){

fulldata_color = fulldata %>% mutate(Oral_exam, = as.character(Oral_exam), color = recode(Oral_exam, '1'  = "red", '0' = "green"))

div(plot_ly(data = fulldata_color, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent, marker = list(color = ~color), type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
    scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
    enter code here

Oral_data = fulldata %>% filter(!is.na(Oral_percent))
div(plot_ly(data = Oral_data, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent,color = ~Oral_percent,  type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
    scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
}

此代码(编织时)仅产生第二个图,看起来就像我打算这样做的方式。但是,如果我将其分解为两个如果具有相同条件的语句,然后将一个绘图命令(以及用于创建数据框架的相应命令),则两个图都正确显示

,我可以通过拥有两个if-if-陈述而不是两个,但是很高兴知道为什么它不起作用,尤其是因为我在同一文档中使用了同一代码块(尽管不在同一案件中)中的多个图,而且它始终是按预期工作。

I am creating an R-Markdown document to help with reporting final exam results at our school. For the mathematics exam, I need a conditional statement to display appropriate plots, because the students do not need to take an oral exam (Oral = NA) if their written score is above a certain threshold. So I have an if-statement that checks whether the sum of the Oral_Exam variable (1 for those who had to take it, 0 otherwise) is larger than zero, and if so, create a 3D scatterplot where the students who had to take an oral exam are marked with red, followed by another plot of the same type, only with the students who had to go to the oral exam, colored according to oral exam result. If none of the students had to go to an oral exam, it is checked in a later if-statement, and only one plot is produced. My code looks like this:

```{r warning = FALSE, message = FALSE, echo = FALSE, eval = params$subj == "Matematika"}
if(sum(fulldata$Oral_exam) > 0){

fulldata_color = fulldata %>% mutate(Oral_exam, = as.character(Oral_exam), color = recode(Oral_exam, '1'  = "red", '0' = "green"))

div(plot_ly(data = fulldata_color, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent, marker = list(color = ~color), type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
    scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
    enter code here

Oral_data = fulldata %>% filter(!is.na(Oral_percent))
div(plot_ly(data = Oral_data, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent,color = ~Oral_percent,  type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
    scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
}

This code, when knit, results in only the second plot being created, and it looks like the way I intend it to. However, if I break it up into two if statements with the same condition, and put one plotting command (and the corresponding command for the creation of the data frame), both plots are displayed correctly

I can work around it by having two if-statements instead of two, but it would be good to know why it doesn't work, especially since I have used multiple plots in the same code chunk (although not in the same if-statement) in the same document, and it has always worked as intended.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-02-12 20:53:45

您可以将plotly objets存储在变量中,并在的情况下在之外打印它们:

```{r}
p1 <- NULL
p2 <- NULL
if(TRUE) {
  p1 <- plot_ly(x = 1, y = 1, type = "scatter", mode = "marker")
  p2 <- plot_ly(x = 1, y = 10, type = "scatter", mode = "marker")
}
p1
p2
    ```

You can store plotly objets in variables and print them outside if:

```{r}
p1 <- NULL
p2 <- NULL
if(TRUE) {
  p1 <- plot_ly(x = 1, y = 1, type = "scatter", mode = "marker")
  p2 <- plot_ly(x = 1, y = 10, type = "scatter", mode = "marker")
}
p1
p2
    ```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文