Subplotting animation with missing values

Hello everyone! This is my first post here, my name is Uriah and I’m a statistician from Israel.

I’m trying to make an animation subplot of two plots with lines and a dots that move along the line. Problem is that for some values, the y values does not exist for some x values - for these cases I would like the dot to disapear while the line stays as it is. Instead, the all plot disappears.

How can I do that?

Here is an example:

library(plotly)

eps <- rnorm(100)

x <- c(1:100)

y1 <- x*0.5 + eps

y2 <- c(x[1:60]*(-0.25) + eps[1:60], rep(NA,40))

dat <- data.frame(x=x, y1=y1, y2=y2)

p1 <- plot_ly(x=~x, y=~y1, type=“scatter”, mode=“lines”) %>%

add_markers(x=~x, y=~y1, frame =~x, marker=list(size=20))

p2 <- plot_ly(x=~x, y=~y2, type=“scatter”, mode=“lines”) %>%

add_markers(x=~x, y=~y2, frame =~x, marker=list(size=20))

subplot(p1,p2)