r - Hoverinfo 文本在闪亮应用程序的绘图图中无法正常工作
问题描述
您好,我有一个简单的闪亮应用程序,它根据我的数据框的“OriginId”列创建饼图。问题是我希望鼠标悬停在我使用text = ~paste( table(testdata$OriginId), ' clients')
. 但不是这个,我犯了一个错误。Column
文本must be length 1 or 4, not 2
。这是我的代码:
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))
})
}
解决方案
我不确定这是否可能,但与此同时,这里有一个替代方案。
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)
推荐阅读
- git - Azure 资源组部署与来自非公共 GIT 的 ARM 链接模板
- r - 极坐标中的重叠点
- azure - 查找 Azure 服务器的实例 ID
- ios - 嵌入在 Launch Screen.xib 中的 UIImage 无法正确显示
- firewall - Proxmox 5.2 虚拟机 SSH
- c# - 通过 C# 或 C++ 将自定义属性添加到 slx 文件中
- c# - 使用上下文 EF Core 更新模型的好方法
- javascript - 我们如何在 JavaScript 中等待结果?
- python - 有没有办法在散景中使用基于文本的 X 值?
- javascript - Dynamics365:如何将二进制图像转换为实际图像