2015-10-16 73 views
0

我能夠成功地繪製我的圖表在renderPlotggplot,但使用nPlotrCharts,我收到一個錯誤代碼:rChart + nvd3沒有顯示出來 - 錯誤:需要有限ylim值

Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) : 
    need finite 'ylim' values 

我只包括我所知道的是麻煩的代碼: 我ui.R:

mainPanel(
    showOutput("plot3", "nvd3") 
    )) 

我server.R

output$plot3 <- renderChart({ 
    candySelect<- input$candy 
    d <- candyData[candyData$candy== candySelect, ] 
    p3 <- nPlot(freq~purchase_month, data = d ,group = "candy", type = "lineChart") 
    p3$addParams(dom = 'plot3') 
    p3 
    }) 

注意:freqpurchase_month是我數據集中的列。例如,「」的格式爲「2014/04」。

輸出看起來是這樣的: enter image description here

+0

檢查空你的'輸入$'值請 –

+0

我可以這樣做:'d [is.na(d)!]'? – Gary

+1

你應該檢查'is.null(input $ candy)',默認情況下它們被設置爲'NULL' –

回答

1

我想你還沒有格式化你的日期,你可以使用類似dates <- as.Date(dates, "%Y-%M")格式化你的日期。

rm(list = ls()) 
library(shiny) 
library(rCharts) 

# sample Data 
dat <- data.frame(
    purchase_month = seq(as.Date("2014/1/1"), by = "month", length.out = 12), 
    candy = sample(c(rep("Mars",4),rep("Bounty",4),rep("Twix",4))), 
    freq =round(runif(12)*500,0)) 

#format dates if needs be (they have to be a datetime object) 
#dates <- as.Date(dates, "%Y-%M") 

ui <- fluidPage(titlePanel("Candy Select"), 
        sidebarPanel(
        selectInput("candy", "Choose a candy:", choices = c("Mars", "Bounty","Twix"))), 
       mainPanel(showOutput("plot3","Nvd3")) 
) 

server <- function(input, output) { 

    output$plot3 <- renderChart2({ 
    dat <- dat[dat$candy %in% input$candy,] 
    n <- nPlot(freq~purchase_month , group = 'candy', data = dat, type = 'lineChart') 

    n$xAxis(tickFormat = "#! 
     function(d) {return d3.time.format('%Y/%m')(new Date(d*1000*3600*24));} !#",rotateLabels = -90) 
    n 
    }) 
} 

shinyApp(ui, server) 

圖中出現頻率

enter image description here