rCharts与Highcharts一起作为闪亮的应用程序

时间:2013-10-01 16:47:22

标签: r highcharts shiny rcharts

我有一个由三个文件组成的闪亮应用程序。 server.R,ui.R和用

启动应用程序的文件
require(shiny)
require(rCharts)
runApp("shinyApp")

应用程序启动,但图表不可见。它适用于正常的r-plot和polycharts,但经过多次尝试后,我仍然没有成功使用rCharts(包括rHighcharts)。

这是上次尝试的文件:

server.R:

library(rCharts)
shinyServer(function(input, output) {
  output$myChart <- renderChart({
    h1 <- Highcharts$new()
    h1$chart(type = "spline")
    h1$series(data = c(1, 3, 2, 4, 5), dashStyle = "longdash")
    h1$series(data = c(NA, 4, 1, 3, 4), dashStyle = "shortdot")
    h1$legend(symbolWidth = 80)
    return(h1)
  })
})

ui.R:

require(rCharts)
shinyUI(pageWithSidebar(
  headerPanel("rCharts: Highcharts"),
  sidebarPanel(
    selectInput(inputId = "x",
      label = "Choose X",
      choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
      selected = "SepalLength")
    ),
  mainPanel(showOutput("myChart", "Highcharts")
  )
))

我的假设是“showOutput”的第二个参数可能是错误的,但我没有找到任何内容。

2 个答案:

答案 0 :(得分:17)

有两种方法可以实现这一目标。第一种方法是在h1$set(dom = "myChart")中添加server.R。这是必需的,以便server.Rui.R正在就正确的图表进行沟通。另一种方法是使用位于renderChart2的{​​{1}}分支中的dev,它是rCharts的升级版本,最终将替换它。为了每个人的利益,我附上了整个代码。

renderChart

答案 1 :(得分:2)

这很可能是由于rchartsjavascript这一事实,因为R变量名和javascript允许的变量名之间存在强烈的不兼容性。特别是关于名称中的点.

尝试在data.frame名称和任意character列上运行以下函数:

noDot <- function(x)
   gsub("\\.", "_", gsub("^\\.", "", x) )

即:

names(df) <- noDot(names(df))
# and
char.cols <- sapply(df, function(x) any(is(x)=="character"))
df[, char.cols] <- sapply(df[, char.cols], noDot)
# note that this has not been tested