0
votes

Hello I have a simple shiny application which cretaes a pie chart based on the column "OriginId" of my data frame. The problem is that I want the mouse hover to give me the exact number of clients when I use text = ~paste( table(testdata$OriginId), ' clients'). But instead of this Im taking an error. Columntextmust be length 1 or 4, not 2. This is my code:

OriginId = c("INT", "DOM", "INT","DOM") 
RequestedDtTm = c("2017-01-16 16:43:33
", "2017-01-17 16:43:33
", "2017-01-18 16:43:33
","2017-01-19 16:43:33") 
testdata = data.frame(OriginId,RequestedDtTm) 

## ui.R ##
library(shinydashboard)
library(plotly)

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),

  ## Sidebar content
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),

  ## Body content
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
              fluidRow(
                box(
                  plotlyOutput("pie",height = 250))



              )

      )

      # Second tab content


    )
  )
)
#server.r
server <- function(input, output) {


  output$pie<-renderPlotly({
    p <- plot_ly(testdata, labels = ~OriginId, values = table(testdata$OriginId), type = 'pie',
                 textposition = 'inside',
                 textinfo = 'label+percent',
                 insidetextfont = list(color = '#FFFFFF'),
                 hoverinfo = 'text',
                 text = ~paste( table(testdata$OriginId), ' clients'),
                 marker = list(
                               line = list(color = '#FFFFFF', width = 1)),
                 #The 'pull' attribute can also be used to create space between the sectors
                 showlegend = FALSE) %>%
      layout(title = 'Domestic vs International Share',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  })

  }
1
Your app works if you use paste(testdata$OriginId, ' clients')instead of this paste( table(testdata$OriginId), ' clients'). Why are you using table() if you only want text?MLavoie
because I want the number of clients: for example when I hover over "INT" I want to "2 clients" to be displayed. not "INT clients"firmo23
Is that clear enough?firmo23

1 Answers

1
votes

I am not sure if it is possible but in the meantime, here is an alternative.

testdata <- data.frame(testdata %>% group_by(OriginId) %>% mutate(Counts = n())) 

##Add a new Counts (what the table do) column and use this column for your hover text.

## ui.R ##
library(shinydashboard)
library(plotly)

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),

  ## Sidebar content
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),

  ## Body content
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
              fluidRow(
                box(
                  plotlyOutput("pie",height = 250))



              )

      )

      # Second tab content


    )
  )
)
#server.r
server <- function(input, output) {


  output$pie<-renderPlotly({
    p <- plot_ly(testdata, labels = ~OriginId, values = table(testdata$OriginId), type = 'pie',
                 textposition = 'inside',
                 textinfo = 'label+percent',
                 insidetextfont = list(color = '#FFFFFF'),
                 hoverinfo = 'text',
                 text = ~paste(testdata$Counts, ' clients'),
                 marker = list(
                   line = list(color = '#FFFFFF', width = 1)),
                 #The 'pull' attribute can also be used to create space between the sectors
                 showlegend = FALSE) %>%
      layout(title = 'Domestic vs International Share',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  })

}

shinyApp(ui, server)