0

I am building a shinyapp, and I have data for markets and customers.

I want to make a valueBox, that tells the Top Markets of the company, but I want that the user chooses how many markets he wants to see (5,10,15,20, etc)

For example, to select Top 5 or Top 10 or Top 15 and so on.

and when I apply this code, I got this error:

ui.r:

  fluidRow( 
                        box(selectInput("topmar", "Select Top", c("5", "10", "15", "20"), selected = 5, width = "80px"), 
                          title = "Top Markets", width = 4, collapsible = TRUE, collapsed = TRUE, background = "light-blue", solidHeader = TRUE, 
                            tableOutput("top5market")),

server.r:

topmarket <- reactive({
    comcon() %>% 
      group_by(Market) %>% 
      summarize(CollectiveTurnover = sum(`Net turnover`)) %>% 
      arrange(desc(CollectiveTurnover)) %>% 
      # top_n(5)
      input$topmar <- as.numeric(top_n(input$topmar)) 
      top_n(input$topmar)
  })

screenshot of the code and error

This is the error I'm getting:

no applicable method for 'tbl_vars' applied to an object of class "character"

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
  • Hi, welcome to SO! You'ght add a [reproducible example](https://stackoverflow.com/help/mcve) to help to detect your error. – s__ Jun 12 '18 at 11:59

1 Answers1

1

I guess you were almost there, but you provided your selectInput choices as characters. Here is a working example:

library(shiny)
library(shinydashboard)
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody( fluidRow( 
    box(selectInput("topmar", "Select Top", c(5, 10, 15, 20), selected = 5, width = "80px"), 
        title = "Top Markets", width = 4, collapsible = TRUE, collapsed = TRUE, background = "light-blue", solidHeader = TRUE, 
        tableOutput("topMarketTable")))
  ))

server <- function(input, output) {
  comcon <- reactive({tibble(Market = paste0("Market_", rep(LETTERS[1:20], 100)), `Net turnover` = round(runif(2000, 10000, 20000)))})

  topMarket <- reactive({
    comcon() %>% 
      group_by(Market) %>% 
      summarize(CollectiveTurnover = sum(`Net turnover`)) %>% 
      arrange(desc(CollectiveTurnover)) %>% 
      top_n(n = as.integer(input$topmar), wt = CollectiveTurnover)
  })

  output$topMarketTable <- renderTable(topMarket())

}

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78