rCharts与Highcharts在shiny应用中的使用

11

我有一个由三个文件组成的闪亮应用程序。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个回答

17

有两种方法可以使它工作。第一种方法是在你的server.R中添加h1$set(dom = "myChart")。这是必需的,以便server.Rui.R正确地交流关于正确的图表。另一种方法是使用rChartsdev分支中的renderChart2,这是renderChart的升级版本,并最终将取代它。我附上整个代码,以方便大家查看。

require(rCharts)
require(shiny)
runApp(list(
  ui =  pageWithSidebar(
    headerPanel("rCharts: Highcharts"),
    sidebarPanel(selectInput(
      inputId = "x",
      label = "Choose X",
      choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
      selected = "SepalLength"
    )),
    mainPanel(showOutput("myChart", "Highcharts"))
  ),
  server = function(input, output){
    output$myChart <- renderChart2({
      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)
    })
  }
))

2
这很可能是因为rcharts基于javascript,而R变量名与javascript允许的变量名之间存在强烈的不兼容性。特别是在名称中使用点号.
尝试对data.frame名称和任何character列运行以下函数:
noDot <- function(x)
   gsub("\\.", "_", gsub("^\\.", "", x) )

ie:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接