Chart not rendering with Shiny R and NVD3

不打扰是莪最后的温柔 提交于 2019-12-01 11:35:15

问题


I have been attempting to create a Shiny timeseries plot using NVD3 library. Am relatively new to R, Shiny and NVD3. The problem is that when I run the ShinyApp, no chart renders on the browser. Using chromes developer tools, I can see that the div for myChart is created and populated with data, but not understanding why I cannot see the chart itself.

Would appreciate any and all help on this matter... My code is like so:

#ui.R
require(rCharts)

shinyUI(pageWithSidebar(
  headerPanel("Population Trend By Age Group:"),

  sidebarPanel(
    selectInput(inputId = "agegrp",
                label = "Choose Agegroup",
                choices = c("0-4",
                            "5-9",
                            "10-14",
                            "15-19",
                            "20-24",
                            "25-29",
                            "30-34",
                            "35-39",
                            "40-44",
                            "45-49",
                            "50-54",
                            "55-59",
                            "60-64",
                            "65-69",
                            "70-74",
                            "75-79",
                            "80-84",
                            "85+"
                ),
                selected = "0-4")
  ),
  mainPanel(
    showOutput("myChart", "nvd3")
  )
))

server.R:

#server.R
require(rCharts)

data <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/data2.csv")
agegroup_mapping <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/agegroup.csv")
data <- merge(data,agegroup_mapping,by.x="agegrp",by.y="agegroup")


shinyServer(function(input, output) {

  output$myChart <- renderChart({

    selection <- subset(data,mapping == input$agegrp)

    plot <- nPlot(n ~ year,
                  data = selection,
                  type = "lineChart",
                  group = "sex")
    # Add axis labels and format the tooltip
    plot$yAxis(axisLabel = "Population", width = 62)

    plot$xAxis(axisLabel = "Year")

    plot$save("ac.html")
    return(plot)    

  })
})

Thanks, Tumaini


回答1:


Use renderChart2 instead of renderChart.

rm(list = ls())
library(shiny)
library(rCharts)

data <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/data2.csv")
agegroup_mapping <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/agegroup.csv")
data <- merge(data,agegroup_mapping,by.x="agegrp",by.y="agegroup")

ui =pageWithSidebar(
  headerPanel("Population Trend By Age Group:"),

  sidebarPanel(
    selectInput(inputId = "agegrp",
                label = "Choose Agegroup",
                choices = c("0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39",
                            "40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85+"),selected = "0-4"),width=2),
  mainPanel(
    showOutput("myChart", "nvd3")
  )
)

server = function(input, output) {

  output$myChart <- renderChart2({
    #selection <-  data[data$mapping == "0-4",]
    selection <- data[data$mapping == input$agegrp,]

    selection <- subset(data,mapping == input$agegrp)

    plot <- nPlot(n ~ year,
                  data = selection,
                  type = "lineChart",
                  group = "sex")
    # Add axis labels and format the tooltip
    plot$yAxis(axisLabel = "Population", width = 62)
    plot$xAxis(axisLabel = "Year")
    plot$set(width=1600, height=800)

    plot$save("ac.html")
    plot
  })
}


runApp(list(ui = ui, server = server))


来源:https://stackoverflow.com/questions/29316927/chart-not-rendering-with-shiny-r-and-nvd3

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