Home > Blockchain >  R Markdown not able to display two plots within the same if-statement
R Markdown not able to display two plots within the same if-statement

Time:06-10

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.

CodePudding user response:

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
    ```
  • Related