一个R Shiny问题 - 在一个图上绘制多条线

cia*_*ius 4 r shiny

目前,我的代码最初让用户选择他们想要的数据集(选择两个).然后根据他们选择的内容,出现数据集各自子集的其他绘图变量.这样做很好,除了我希望这些图在一个图上覆盖所有图,而不是默认情况下单独出现.

我有一个默认的plot plot,plot_sotal,数据集中的其他选项正在查看它的特定子集.因此,只有一个分散是有意义的.

 output$plot_Total <- reactivePlot(function() { 
  plot.new()
   plot.window(xlim=c(1850,2020), ylim = c(0,5000000))
   axis(1)
   axis(2)
   title(main="Numbers over the years")
   title(xlab="Year")
   title(ylab="Number of people")
   box()
   points(dat$Year, dat$Total, col="red")
   lines(dat$Year, dat$Total, col="red")
   })

 output$plot_subset1 <- reactivePlot(function() { lines(dat$Year, dat$subset1) })
 output$plot_subset2 <- reactivePlot(function() { lines(dat$Year, dat$subset2) })
Run Code Online (Sandbox Code Playgroud)

为什么这段代码不起作用?它只为每个(不需要的)图形创建空白区域,在其下面显示"错误:plot.new尚未被调用".如何指定将这些行添加到默认(plot_Total)图中?

cia*_*ius 7

更新:通过在闪亮的主页上播放图表的代码,我意识到我必须这样做:

 output$plot_Total <- reactivePlot(function() { 
  plot.new()
   plot.window(xlim=c(1850,2020), ylim = c(0,5000000))
   axis(1)
   axis(2)
   title(main="Numbers over the years")
   title(xlab="Year")
   title(ylab="Number of people")
   box()
   points(dat$Year, dat$Total, col="red")
   lines(dat$Year, dat$Total, col="red")
  if (input$RC) {   lines(dat$Year, dat$dat)}
  })
Run Code Online (Sandbox Code Playgroud)

这与我原来的代码有两点不同.首先,条件在同一反应图函数中仅添加一行.其次,我创建了一个新的data.frame,它只包含子集RC.这最初不是作为输入$ dat $ RC工作,但是当RC是它自己的数据帧时,它作为输入$ RC工作.

追逐指向我正确的方向!