Labels are not displayed appropriately in a plotly pie chart of a shiny app


#1

Hello I have a simple shiny app which takes as inputs the input$variable and then displays a pie chart with the share of each input. The problem is that I want to display somehow the OriginId (EXT,INT)inside each piece.

#dataset
OriginId=c("INT","EXT","INT","INT","EXT")
FacilityName=c("t1","t1","t2","t2","t1")
Testdata2<-data.frame(OriginId,FacilityName)

#app
## ui.R ## ##Correct Version
library(shinydashboard)
library(plotly)
library(data.table)

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)
#
ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = img(src="Logo1.jpg", height = 50, align = "left")

                    ),

                    ## Sidebar content
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))

                      )
                    ),

                    ## Body content
                    dashboardBody(
                      tabItems(

                        # Dashboard tab
                        tabItem(tabName = "dashboard",
                                fluidRow(
                                  box(title = "Verhältnis interner / externer Aufträge", status = "primary", solidHeader = TRUE,
                                      plotlyOutput("pie",height = 250)),
                                  uiOutput("var")

                                )

                        )



                      )
                    )
)
#server.r
##Correct Version
server <- function(input, output,session) {

  # Auftrag INT vs Ext
  output$pie<-renderPlotly({

    data <- dplyr::tbl_df(subset(Testdata2[,1:2],Testdata2$FacilityName %in% input$variable))
    ttestdata <- data.frame(data %>% group_by(OriginId) %>% mutate(Counts = n())) 
    p <- plot_ly(data, labels=data$OriginId, values = table(data$OriginId), type = 'pie',
                 textposition = 'inside',
                 insidetextfont = list(color = '#FFFFFF'),
                 hoverinfo = 'text',
                 text = ~paste(ttestdata$Counts, ' Kunden'),
                 marker = list(
                   line = list(color = '#FFFFFF', width = 1)),
                 showlegend = FALSE) %>%
      layout(
        title = paste(input$variable),
        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

  })



  output$var<-renderUI({
    selectInput("variable",
                h4("Abteilung wählen:"),
                choices = Testdata2 %>% distinct(FacilityName),selected = 1)
  })

}