您好我有一个简单的闪亮应用程序,根据列#34; OriginId"创建一个饼图。我的数据框架。问题是,当我使用text = ~paste( table(testdata$OriginId), ' clients')
时,我希望鼠标悬停为我提供确切数量的客户端。但不是这个我犯了一个错误。 Column
{文本{1}}。这是我的代码:
must be length 1 or 4, not 2
答案 0 :(得分:1)
我不确定是否可能,但与此同时,这是另一种选择。
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)