Hoverinfo文本在应用程序的绘图图中无法正常工作
您好,我有一个简单的闪亮的应用程序,它根据我的数据框的列原始ID创建一个饼图。问题是,当我使用text=~paste tabletestdata$OriginId“clients”时,我希望鼠标悬停能给出客户机的确切数量。但我没有这样做,而是犯了一个错误。ColumnText的长度必须为1或4,而不是2。这是我的代码:Hoverinfo文本在应用程序的绘图图中无法正常工作,r,shiny,plotly,R,Shiny,Plotly,您好,我有一个简单的闪亮的应用程序,它根据我的数据框的列原始ID创建一个饼图。问题是,当我使用text=~paste tabletestdata$OriginId“clients”时,我希望鼠标悬停能给出客户机的确切数量。但我没有这样做,而是犯了一个错误。ColumnText的长度必须为1或4,而不是2。这是我的代码: OriginId = c("INT", "DOM", "INT","DOM") RequestedDtTm = c("2017-01-16 16:43:33 ", "2017-
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)
如果您使用pastetestdata$OriginId“clients”而不是此paste tabletestdata$OriginId“clients”,则您的应用程序可以运行。如果只需要文本,为什么要使用table?因为我需要客户端的数量:例如,当我将鼠标悬停在INT上时,我希望显示两个客户端。还不够清楚吗?