I'm trying to bulid my very first shiny app based on dataset of Olympic medalists. I want to create a reactive barchart which shows a number of medals per chosen country and for chosen type of olympic game(summer/winter) with years on x axis and amount of medals on y axis. So user first choose a specified country (from column Country_full) and then type.
Below I reproduce data for problem solving:
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)
So my data looks like below:
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
My code for producing a desired graph in ggplot2 below:
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))
And my code for shiny app
UI part
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 part
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)
Running this code lead to an error like this one:
Error in if (multiple) selectTag$attribs$multiple <- "multiple" :
argument is not interpretable as logical
So I assume that I have more than one error, probably in server part but I cannot identify what's wrong. I'll really appreciate any kind of help. I already spent on this approx 5 hours.