现在,我正在尝试根据世界卫生组织的酒精消耗数据集构建ShinyApp。我已经从这些数据中构建了地图,并使这些输出生效,但是现在,我试图显示饮酒量排名前十的国家,而其中两个地块未显示(啤酒和葡萄酒)。
通过在这里查看我的闪亮应用程序,您可以明白我的意思:
https://mdsil11.shinyapps.io/worldMap/
奇怪的是,当我使用更简单的绘图代替hist(rnorm(100))之类的ggplotone时,我的代码有效。另一个奇怪的事情是,当我在常规的R脚本中运行这些绘图时,这些绘图会起作用。最后,显示我的最后一个情节(用于烈酒或混合饮料的情节),但不显示葡萄酒或啤酒。
代码如下:
library(shiny)
[library(rworldmap)
library(ggplot2)
library(tidyverse)
## defining datasets
wineDrinks = drinks\[ , c(2,5)\]
spiritDrinks = drinks\[ , c(2,4)\]
beerDrinks = drinks\[ , c(2,3)\]
topWine <- wineDrinks %>% arrange(desc(wine_servings))
topBeer <- beerDrinks %>% arrange(desc(beer_servings))
topSpirits <- spiritDrinks %>% arrange(desc(spirit_servings))
top10beer <- topBeer[0:10, ]
top10wine <- topWine[0:10, ]
top10spirits <- topSpirits[0:10, ]
## defining map objects
w <- joinCountryData2Map(drinksISO, joinCode = 'ISO3', nameJoinColumn = 'ISO', verbose = TRUE)
s <- joinCountryData2Map(drinksISO, joinCode = 'ISO3', nameJoinColumn = 'ISO', verbose = TRUE)
b <- joinCountryData2Map(drinksISO, joinCode = 'ISO3', nameJoinColumn = 'ISO', verbose = TRUE)
##### Shiny Application #####
ui <- fluidPage(
titlePanel('Global Alcohol Consumption'),
#each input should have a unique input ID
sidebarPanel(
radioButtons(inputId = 'Alc', label = 'Alcohol Type',
choices = list('Beer','Wine','Spirits'))
)
, mainPanel(
plotOutput('map'), plotOutput('TopTen')
)
)
server <- function(input, output) {
output$map <- renderPlot({
if(input$Alc == 'Beer'){
mapCountryData(mapToPlot = b,nameColumnToPlot = 'beer_servings', mapTitle = 'World Beer Consumption', missingCountryCol = "grey", oceanCol = "lightblue1")
}
if(input$Alc == 'Wine'){
mapCountryData(mapToPlot = w,nameColumnToPlot = 'wine_servings', mapTitle = 'World Wine Consumption', missingCountryCol = "grey", oceanCol = "lightblue1")
}
if(input$Alc == 'Spirits'){
mapCountryData(mapToPlot = s, nameColumnToPlot = 'spirit_servings', mapTitle = 'World Spirits Consumption', missingCountryCol = "grey", oceanCol = "lightblue1")
}
})
output$TopTen <- renderPlot({
#### PROBLEM LIES HERE #####
if(input$Alc == 'Beer'){
ggplot(top10beer,aes(x = reorder(country, -beer_servings), y = beer_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = beer_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 beer drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
}
if(input$Alc == 'Wine'){
ggplot(top10wine, aes(x = reorder(country, -wine_servings), y = wine_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = wine_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 wine drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
}
#### BUT THIS WORKS ####
if(input$Alc == 'Spirits'){
ggplot(top10spirits,aes(x = reorder(country, -spirit_servings), y = spirit_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = spirit_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 spirit drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
}
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
对于帮助显示其他两个图的任何帮助/指针,将不胜感激!
答案 0 :(得分:1)
将那些单独的if
语句更改为if/else/else
。您的renderPlot
代码块需要返回ggplot对象。如果您的代码块的最后一行是if
,并且计算结果为FALSE,则代码块不会返回任何内容。以前的if
语句中的值基本上被忽略。调用ggplot()
实际上并没有像hist
这样的基本图形功能那样绘制绘图。实际上,您需要打印ggplot
返回的值才能触发绘图绘制。
这是重写代码块的一种方法。
output$TopTen <- renderPlot({
if(input$Alc == 'Beer'){
ggplot(top10beer,aes(x = reorder(country, -beer_servings), y = beer_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = beer_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 beer drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
} else if(input$Alc == 'Wine'){
ggplot(top10wine, aes(x = reorder(country, -wine_servings), y = wine_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = wine_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 wine drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
} else if (input$Alc == 'Spirits'){
ggplot(top10spirits,aes(x = reorder(country, -spirit_servings), y = spirit_servings)) + geom_point(size = 3) + geom_segment(aes(x = country, xend = country, y = 0, yend = spirit_servings)) + labs(title="Lollipop Chart", subtitle = 'Top 10 spirit drinking countries') + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + xlab('Countries')
} else {
stop("unknown plot type")
}
})