2012-11-10 153 views
4

目前,我的代碼最初可以讓用戶選擇他們想要的數據集(兩種選擇)。然後根據他們選擇的內容,出現數據集各個子集的其他繪圖變量。這很好,除了我希望重複所有情節的情節,而不是像默認情況下那樣分開。An R Shiny問題 - 在一張圖上繪製多條線

我有一個默認繪圖plot_Total,而數據集中的其他選項正在查看此特定的子集。所以只有一個分散是有道理的。

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

爲什麼這段代碼片段不工作?它只是爲每個(不需要的)圖形創建空格,在其下面會顯示「錯誤:plot.new尚未被調用」。如何指定將這些行添加到默認(plot_Total)圖?

+3

你看過閃亮的主頁上的例子嗎?它似乎做你想要添加一個低級繪圖命令到現有的情節:http://www.rstudio.com/shiny/ – Chase

回答

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

這不同於我原來的代碼在兩個方面。首先,在同一個反應圖函數中,條件被添加爲一行。其次,我創建了一個只包含子集RC的新數據框。這最初不是作爲輸入$ dat $ RC工作,但是當RC是它自己的數據框時,它作爲輸入$ RC工作。

點追逐轉向我在正確的方向!