我可以在renderPlot
和ggplot
中成功绘制图表,但使用nPlot
rCharts
,我收到错误代码:
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) :
need finite 'ylim' values
我只包含了我知道很麻烦的代码: 我的ui.R:
mainPanel(
showOutput("plot3", "nvd3")
))
我的服务器.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
})
注意:freq
和purchase_month
是我的数据集中的列。 purchase_month
的格式为,例如,“2014/04”。
答案 0 :(得分: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)
该图显示频率