Data labeling is not updated properly after taking inputs in a shiny app


#1

Hello I have a simple shiny app which an "OriginId" pie chart based on "Facility Name" inputs. What I want to achieve is to have as labels the "EXT" or "INT" in every piece of the pie (if there are both), the share and the number "EXT" or "INT" but when I change the input the labels are lost.

#data
OriginId=c("INT","EXT","INT","INT","EXT","INT","EXT","INT")
FacilityName=c("t1","t1","t2","t2","t1","t3","t4","t5")
FacId=c("t1","t1","t2","t2","t1","t3","t4","t5")
Testdata2<-data.frame(OriginId,FacilityName,FacId)

#ui.r
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
server <- function(input, output,session) {

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

    data <- dplyr::tbl_df(subset(Testdata2,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)
  })

}

#2

This strikes me as more of a plot_ly questions. Even when you extract all the shiny elements and just work with the plot, I get the same issue with your text labels.

Did you check out the guidance here: https://plot.ly/r/reference/


#3

so you mean that I have to use add_trace()?