闪亮的应用程序未呈现 Dimple.js 人口金字塔
Shiny app not Rendering a Dimple.js Population Pyramid
我正在尝试创建一个 Shiny 应用程序,它呈现通过 select/dropdown 输入选择的给定年份的人口金字塔。我的 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 .
感谢对此事的任何了解,
图迈尼
经过大量修改和研究,我发现我需要使用 showOutput("distPlot", "dimple")
而不是 plotOutput("distPlot")
,renderChart2()
而不是 renderPlot()
。
我正在尝试创建一个 Shiny 应用程序,它呈现通过 select/dropdown 输入选择的给定年份的人口金字塔。我的 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 .
感谢对此事的任何了解, 图迈尼
经过大量修改和研究,我发现我需要使用 showOutput("distPlot", "dimple")
而不是 plotOutput("distPlot")
,renderChart2()
而不是 renderPlot()
。