rCharts with Highcharts as shiny application

让人想犯罪 __ 提交于 2019-11-27 14:57:35

问题


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.


回答1:


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



回答2:


This is most likely due to the fact that rcharts is javascript based on there are strong incompatibilities between R variable names and javascript allowed variable names. Specifically with regards to dots . in names.

Try running the following function on the data.frame names and any character columns:

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


来源:https://stackoverflow.com/questions/19121695/rcharts-with-highcharts-as-shiny-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!