【问题标题】:Chart not rendering with Shiny R and NVD3图表未使用 Shiny R 和 NVD3 渲染
【发布时间】:2015-03-28 12:04:02
【问题描述】:

我一直在尝试使用 NVD3 库创建一个闪亮的时间序列图。我对 R、Shiny 和 NVD3 比较陌生。问题是当我运行 ShinyApp 时,浏览器上没有呈现图表。使用 chromes 开发人员工具,我可以看到 myChart 的 div 已创建并填充了数据,但不明白为什么我看不到图表本身。

非常感谢您对此事的任何帮助... 我的代码是这样的:

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

服务器.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)    

  })
})

谢谢, 图马尼

【问题讨论】:

    标签: shiny nvd3.js rcharts


    【解决方案1】:

    使用renderChart2 代替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))
    

    【讨论】:

    • 非常感谢,Ramnath 不久前提醒我注意这一点,但感谢您的帮助 :-)
    猜你喜欢
    • 2021-03-20
    • 2020-11-08
    • 1970-01-01
    • 2021-03-12
    • 2019-03-07
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多