闪亮的应用程序不渲染Dimple.js人口金字塔

时间:2015-03-22 21:46:16

标签: r d3.js shiny dimple.js

我试图创建一个Shiny应用程序,通过选择/下拉输入选择呈现给定年份的人口金字塔。我有我的ui.R代码:

    library(shiny)

# Define UI for application that draws a outcome pyramid
shinyUI(fluidPage(

  # Application title
  titlePanel("Outcome Pyramid"),

  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "startyr",
                  label = "Select Start Year",
                  c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014)) 
    ),

    # Show a plot of the generated pyramid
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

和我的server.R看起来像:

library(shiny)
library(rCharts)

df <- read.csv("path_to_csv\data2.csv")

# Define server logic required to draw a population pyramid
shinyServer(function(input, output) {

  # Expression that generates a pypramid plot. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot

    output$distPlot <- renderPlot({

      startyear <- as.numeric(input$startyr)
      # Both arguments currently for the same thing, startyear, but eventually will want to 
      # process a range of years
      dPyramid(startyear, startyear) 

    })
})

getData <- function(startyr,endyear) {
  df <- subset(df,(year >= startyr & year <= endyear))
  return(df)
}

# DimpleJS pyramid

dPyramid <- function(startyear, endyear, colors=NULL) {
  dat <- getData(startyear, endyear)
  dat$n <- ifelse(dat$sex == 'MAL', -1 * dat$n, 1 * dat$n)
  dat$gencode <- ifelse(dat$sex == 'MAL', 1, 2)

  d1 <- dPlot(
    x = "n", 
    y = "agegrp", 
    groups = "sex", 
    data = dat, 
    type = 'bar')


  d1$yAxis(type = "addCategoryAxis", orderRule = "ord")
  d1$xAxis(type = "addMeasureAxis")
  d1$legend( x = 60, y = 10, width = 700, height = 20, horizontalAlign = "right")

  if (!is.null(colors)){
    d1$colorAxis(
      type = "addColorAxis", 
      colorSeries = "gencode", 
      palette = colors
    )
  }
  if (endyear - startyear >= 1) {
    d1$set(storyboard = "year")
    max_x <- round_any(max(dat$n), 1000, f = ceiling)
    min_x <- round_any(min(dat$n), 1000, f = floor)
    d1$xAxis(overrideMax = max_x, overrideMin = min_x)
  }

  if (max(dat$n >= 1000000)) {
    d1$setTemplate( afterScript = 
                      "
                    <script>
                    x._getFormat = function () {
                    return function(d) {
                    return d3.format(',.1f')(Math.abs(d) / 1000000) + 'm';
                    };
                    };
                    myChart.draw()
                    </script>
                    ")
  } else {
    d1$setTemplate( afterScript = 
                      "
                    <script>
                    x._getFormat = function () {
                    return function(d) {
                    return d3.format(',.0f')(Math.abs(d) / 1000) + 'k';
                    };
                    };
                    myChart.draw()
                    </script>
                    ")
  }

  d1
}

然而,每当我运行此应用程序时,Shiny应用程序页面上都没有呈现任何内容。这里使用的csv可以在https://github.com/kilimba/data找到。

感谢任何关于此事的光明, Tumaini

1 个答案:

答案 0 :(得分:0)

经过大量的修补和研究后,我发现我需要使用showOutput("distPlot", "dimple")而不是plotOutput("distPlot")renderChart2()而不是renderPlot()