Reputation: 1235
I have, once again, run into some problems with Shiny. When I run my app I get the following message:
Warning: Error in tapply: arguments must have same length [No stack trace available]
Still quite new to R, I dont understand why I get this warning. I've been randomly experiement with aes_string instead of aes in the graph, but I can't find any workable solution. I would be so grateful if someone could help me.
library(shiny)
library(tidyverse)
df_bransch <- data_frame(
kommun = c("Bjuv", "Bjuv", "Bjuv", "Bromölla", "Bromölla", "Bromölla", "Båstad", "Båstad", "Båstad", "Helsingborg", "Helsingborg", "Helsingborg"),
bransch = c("Besöksnäring", "Byggnadsmaterial", "Fastigheter", "Besöksnäring", "Byggnadsmaterial", "Fastigheter",
"Besöksnäring", "Byggnadsmaterial", "Fastigheter", "Besöksnäring", "Byggnadsmaterial", "Fastigheter"),
Anställda = c(46, 369, 36, 57, 40, 36, 525, 5, 1133, 2392, 195, 1042),
Förädlingsvärde = c(20724, 579892, 91406, 26097, 136440, 51731, 252891, 3852, 1343391, 1257333, 176595, 5017640))
ui <- fluidPage(
navbarPage(title = "TEST", id = "nav",
tabPanel("Branschstruktur",
sidebarLayout(
sidebarPanel(selectInput("kom", "Kommun", choices = unique(df_bransch$kommun), selected = "Malmö"),
varSelectInput("var", "Variabel", df_bransch[c(3,4)])),
mainPanel(plotOutput("plot"))),
tabPanel("Utveckling"))))
server <- function(input, output, session) {
df <- reactive({df_bransch %>%
req(input$var, input$kom) %>%
filter(kommun == input$kom)
})
output$plot <- renderPlot({
ggplot(df(), aes(x = reorder(bransch, input$var), y = input$var)) +
geom_bar(position = "dodge", stat = "identity") +
labs(title = paste0("Branschstruktur, ", input$kom, " år 2018"),
subtitle = paste0("Variabel: ", input$var),
caption = "Källa: Bisnode") +
coord_flip()
})
}
shinyApp(ui, server)
Upvotes: 0
Views: 297
Reputation: 21287
Please make the following change:
ggplot(df(), aes(x = reorder(bransch, df()[[input$var]]), y = df()[[input$var]]))
Also, add to labs()
the code y = "Bransch", x = as.name(input$var),
to give proper labels on x and y axis. Then you will get the output as:
Upvotes: 1