0

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.

  • And nothing changed. Still the same error with "argument not interpretable as logical" – Rafał Pietrak Aug 21 '18 at 05:35
  • Do I need to have this variables which I use as a reactive filters to be in a specific type ? Now both are characters (type and Country_full) – Rafał Pietrak Aug 21 '18 at 05:49
  • Try adding the argument `multiple = FALSE` to both of your `selectInput` functions. It shouldn't be necessary, since that's the default, but if the problem's what I think it is, that might help – divibisan Aug 21 '18 at 15:52
  • Probably helps, however now I have different error. `Error in !selectize : invalid argument type` – Rafał Pietrak Aug 21 '18 at 21:22
  • So assume that I should change the type of variables "type" and "country" ? – Rafał Pietrak Aug 21 '18 at 21:28
  • Could you update your question with a reproducible example? There's no way for us to investigate your problem since, without access to your data, we can't reproduce it. Take a look at this question to see how to make your example reproducible: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – divibisan Aug 21 '18 at 21:31
  • I've made update adding reproducible code, (sorry for putting it so late). – Rafał Pietrak Aug 26 '18 at 23:07
  • When I run my app on this simplified data, code launched however plot not appeared (only filters to choose) and new error came up: 'object of type 'closure' is not subsettable'. – Rafał Pietrak Aug 26 '18 at 23:15
  • Your problem is here: `ggplot(data = selectData`. `selectData()` is a reactive expression and must be called with parentheses: `selectData()`, not `selectData`. If you ever see that error message in a shiny app, it almost always means you're lacking parentheses when calling a `reactive` expression – divibisan Aug 27 '18 at 16:00
  • @divibisan Thak You but I'm sorry, I don't get it. I added a parentheses after selectData() in my ggplot code and problem is still the same. Probably I don't understand Your advice. Should I put something into brackets ? – Rafał Pietrak Aug 27 '18 at 20:38
  • Could You please paste this corrected part of code ? – Rafał Pietrak Aug 27 '18 at 20:48
  • `selectData` is **not** a `data.frame`. Since you defined it with `reactive`, it is a `function` that, when called, returns a data frame. If you want to access the data frame contained in it, you need to use `selectData()` wherever you'd use `selectData` if it was a data frame. That includes, for example, instances like `selectData$Year`, which should be `selectData()$Year`. – divibisan Aug 27 '18 at 21:03
  • Possible duplicate of [Error in : object of type 'closure' is not subsettable](https://stackoverflow.com/questions/11308367/error-in-my-code-object-of-type-closure-is-not-subsettable) – divibisan Aug 27 '18 at 21:03
  • Oh I see, just need to specify that specific variable is in my "reactive" data frame. But what about type of variable ? After that I got completely new error - '`x` must be a string or a R connection'. I checked that, my variable 'Country_full' and 'type' are factors and integers. So I don't see what is the problem ? I cannot have this variable as a string. – Rafał Pietrak Aug 27 '18 at 22:00
  • You didn't mention where your error is from, but I wonder why you chose to use `aes_string` which, as the name implies, takes a string, not a series of arguments like `aes`. – divibisan Aug 27 '18 at 22:05
  • Everything works now ! Problem at the end was with variables passed to ggplot. Thank You very much @divibisan for Your strong striving for solving my problem ! – Rafał Pietrak Aug 30 '18 at 19:05

0 Answers0