我正在尝试根据奥运奖牌获得者的数据建立自己的第一个闪亮应用程序。 我想创建一个反应式条形图,其中显示每个选定国家/地区和特定类型的奥林匹克运动会(夏季/冬季)的奖牌数量,x轴为年份,y轴为数量。 因此,用户首先选择一个指定的国家(从Country_full列中选择),然后输入。
下面我复制数据来解决问题:
Year <- c(2012,2012,2012,2012,2012,2012,2014,2014,2014,2014,2014,2014)
type <- c('summer','summer','summer','summer','summer','summer','winter','winter','winter','winter','winter','winter')
Country <- c('JPN','JPN','JPN','POL','POL','POL','JPN','JPN','JPN','POL','POL','POL')
Medal <- c('Bronze','Gold','Silver','Bronze','Gold','Silver','Bronze','Gold','Silver','Bronze','Gold','Silver')
medals <- c(33,7,44,8,3,1,6,1,4,3,4,4)
Country_full <- c('Japan','Japan','Japan','Poland','Poland','Poland','Japan','Japan','Japan','Poland','Poland','Poland')
tab_2 <- data.frame(Year,type,Country,Medal,medals,Country_full)
所以我的数据如下:
Year type Country Medal medals Country_full
2012 summer JPN Bronze 33 Japan
2012 summer JPN Gold 7 Japan
2012 summer JPN Silver 44 Japan
2012 summer POL Bronze 8 Poland
2012 summer POL Gold 3 Poland
2012 summer POL Silver 1 Poland
2014 winter JPN Bronze 6 Japan
2014 winter JPN Gold 1 Japan
2014 winter JPN Silver 4 Japan
2014 winter POL Bronze 3 Poland
2014 winter POL Gold 4 Poland
2014 winter POL Silver 4 Poland
我的代码用于在下面的ggplot2中生成所需的图形:
ggplot(data=selectData(),mapping=aes(x=selectData()$Year,y=selectData()$medals, fill=selectData()$Medal))
+ geom_bar(stat="identity")
+ geom_text(aes(label=medals), size = 3, position = position_stack(vjust = 0.5))
+ scale_fill_brewer(palette = "Paired")
+ scale_x_continuous(minor_breaks = seq(min(selectData$Year),max(selectData$Year),4),breaks = seq(min(selectData$Year), max(selectData$Year), 4))
+ ylab(expression("vol"))
+ xlab(expression("Year"))+ theme_minimal()
+ ggtitle("Structure of medals of chosen country Olympics (years 1896 - 2012)")
+ theme(axis.text.x = element_text(angle = 0, hjust = 1))
还有我的闪亮应用代码
UI部分
library(shiny)
library("ggplot2")
library("dplyr")
library("RColorBrewer")
setwd("/Users/rafalpietrak/Documents/Programowanie w R/Shiny/medalists")
# Define UI for application that draws a plot with two variables as a filters
ui <- fluidPage(
titlePanel("Medals per Country"),
sidebarPanel(
selectInput(inputId="country","Country:",label = "Choose a country",
choices = unique(tab_2$Country_full),selected = 'Poland',multiple=FALSE),
selectInput(inputId="type","Winter or Summer:",label = "Choose type",
choices = unique(tab_2$type),selected = 'summer',multiple=FALSE)
),
mainPanel(
plotOutput("plot1")
))
服务器部分
server <- function(input, output) {
selectData <- reactive({
tab_2 %>% filter(Country_full==input$country,type==input$type)
})
output$plot1 <- renderPlot({
ggplot(data = selectData,mapping=aes_string(x=input$country,y=selectData$medals,fill=selectData$Medal)) + geom_bar(stat="identity")+geom_text(aes(label=medals), size = 3, position = position_stack(vjust = 0.5))
+scale_fill_brewer(palette = "Paired")
+scale_x_continuous(minor_breaks = seq(min(selectData$Year),max(selectData$Year),4),breaks = seq(min(selectData$Year), max(selectData$Year), 4))
+ylab(expression("vol"))+xlab(expression("Year"))+theme_minimal()+
ggtitle("Structure of medals of chosen country Olympics (years 1896 - 2012)")
+theme(axis.text.x = element_text(angle = 0, hjust = 1))
})
}
shinyApp(ui = ui, server = server)
运行此代码会导致如下错误:
Error in if (multiple) selectTag$attribs$multiple <- "multiple" :
argument is not interpretable as logical
因此,我假设我有多个错误,可能是在服务器部分,但我无法确定出什么问题。 我将非常感谢您提供的任何帮助。我已经花了大约5个小时。