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. Column
textmust 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))
})
}
paste(testdata$OriginId, ' clients')
instead of thispaste( table(testdata$OriginId), ' clients')
. Why are you usingtable()
if you only want text? – MLavoie