首页 > 解决方案 > 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))
  })

  }

标签: rshinyplotly

解决方案


我不确定这是否可能,但与此同时,这里有一个替代方案。

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)

推荐阅读