Frage

Currently, my code initially lets the user select what dataset they want (A choice of two). Then based on what they choose, other plotting variables for the datasets' respective subsets appear. This works fine, apart from the fact that I would like the plots to be overlain all on one plot, instead of separately as they appear by default.

I have a default plot, plot_Total, and the other options in the datasets are looking at particular subsets of this. So it would make sense to have just one scatter.

 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) })

why doesnt this code snippet work? It just creates blank spaces for each (unwanted) graph, underneath which it says "Error: plot.new has not been called yet". How do I specify to add these lines to the default (plot_Total) plot?

War es hilfreich?

Lösung

Update: By playing around with the code for the graph on the shiny homepage, I realised that I had to do this:

 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)}
  })

This differs from my original code in two ways. Firstly, the conditional is added as just one line within the same reactiveplot function. Secondly, I created a new data.frame which only contains the subset RC. This was initially not working as input$dat$RC, but when RC is a dataframe of its own it works as input$RC.

Points to Chase for steering me in the right direction!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top