Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

r - rCharts with Highcharts as shiny application

I have a shiny application consisting of three files. server.R, ui.R and the file to launch the application with

require(shiny)
require(rCharts)
runApp("shinyApp")

The application starts, but the plot is not visible. It works with a normal r-plot and with polycharts, but after trying a lot, I still have no success with rCharts (which includes rHighcharts).

This is the files of the last try:

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

My assumption is that the second argument of "showOutput" might be wrong, but I didn't find anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways to get this working. The first way is to add h1$set(dom = "myChart") in your server.R. This is required so that both server.R and ui.R are communicating about the correct chart. The alternative is to use renderChart2, which is in the dev branch of rCharts, that is a upgraded version of renderChart and will eventually replace it. I am attaching the entire code for everyone's benefit.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...