MenuItem呈现为subItem

时间:2018-07-24 07:46:28

标签: r shiny

我在MenuItem中遇到sidebarMenu的问题。当我添加第三个MenuItem(RFM)时,似乎在ui中将其呈现为一个子项,并且当我单击该项目时,即使在server.R中也没有任何显示。R有相应的功能。这是sidebarMenu的屏幕截图 enter image description here

ui.R

dashboardPage(
  dashboardHeader(title= "Acquisti Clienti CC"),
  dashboardSidebar(

      h4("Explorer"),
      textInput("cluster","Digita un Codice cliente CC:","H01621"),
      selectizeInput('categ',label="Seleziona una Categoria Merceologica",
                     choices=unique(user_clustering$DESC_CAT_MERC),
                     selected=c("NOTEBOOK","PC","TABLET/PDA"),
                     options = NULL,
                     multiple=TRUE),
      #uiOutput("checkcluster"),
    sidebarMenu(id="menu",
      tags$style(".fa-stats {color:#f2f4f4}"),
      tags$style(".fa-th-list {color:#f2f4f4}"),
      menuItem("Dashboard", tabName = "dashboard",icon = icon("stats",lib = "glyphicon")),
      menuItem("Data", tabName = "Data",icon = icon("th-list",lib = "glyphicon")),
      menuItem("RFM",tabname="RFM",icon = icon("dashboard",lib = "glyphicon"))  ## That's the item I ve just added
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("dashboard",
              fluidRow(
                #valueBoxOutput("Spesa_Grafico",width=3),
                valueBoxOutput("Spesa_Totale"),
                #valueBoxOutput("Spesa_Cluster",width=3),
                valueBoxOutput("Clienti_Totali")
              ),
              fluidRow(
                box(title="Cluster 1",plotlyOutput('plot1'),
                    fluidRow(column(4,offset=3,DT::dataTableOutput("plot1_data",width = 10)))),
                #DT::dataTableOutput("plot1_data",width = 8),
                box(title="Cluster 2",plotlyOutput('plot2'),
                    fluidRow(column(4,offset=3,DT::dataTableOutput("plot2_data",width = 10)))),
                #DT::dataTableOutput("plot2_data",width = 8),
                box(title="Cluster 3",plotlyOutput('plot3'),
                    fluidRow(column(4,offset=3,DT::dataTableOutput("plot3_data",width = 10)))),
                #DT::dataTableOutput("plot3_data",width = 8),
                box(title="Cluster 4",plotlyOutput('plot4'),
                    fluidRow(column(4,offset=3,DT::dataTableOutput("plot4_data",width = 10))))
                #DT::dataTableOutput("plot4_data",width = 8)

              )

              )
      ,
      tabItem("Data",
              DT::dataTableOutput("Data"),
              downloadButton("downloadCsv", "Download as CSV")
      ),
      tabItem("RFM",
              fluidRow(
                box(title="RFM",plotOutput('plot_rfm')))
              )
      )
          )
)

server.R

function(input, output, session) {

  # Combine the selected variables into a new data frame


  # Radar Chart data
  selectedData <- reactive({

    categ<-input[["categ"]]
    data_plot<- user_clustering_raw %>%filter(DESC_CAT_MERC %in% categ)%>%
                group_by(CLUSTER,DESC_CAT_MERC)%>%
                dplyr::summarise(VAL_INV=sum(VAL_INV))%>%ungroup()%>%
                group_by(CLUSTER)%>%mutate(VAL_INV=VAL_INV/sum(VAL_INV))




    return (data_plot)
  })

  # RFM chart (2nd page....)
  selectedData_plot2<-reactive({
    clust<-user_clustering_raw[user_clustering_raw$CO_CUST==input$cluster,]$CLUSTER[0]

    rfm <- RFM_rec %>% 
      inner_join(user_clustering_raw%>%select(CO_CUST,CLUSTER)%>%distinct(),by="CO_CUST")%>%
      filter(CLUSTER %in% clust)

    return (rfm)

  })

  # Data for summary alongside graph
  summary_1<-reactive({
    categ<-input[["categ"]]
    summary_1<-user_clustering_raw%>%
                         filter(DESC_CAT_MERC%in% categ)
    return (summary_1)
  })


  # Value box
  output$Spesa_Totale <- renderValueBox({
    valueBox(
      value = prettyNum(round(sum(user_clustering$VAL_INV),0),big.mark=",",decimal.mark = "."),
      subtitle = "Spesa Totale",
      icon = icon("euro"),width=6
    )
  })
  output$Clienti_Totali <- renderValueBox({


    valueBox(
      length(unique(user_clustering_raw%>%pull(CO_CUST))),
      "Numero Clienti Totali",
      icon = icon("users"),width=6
    )
  })

  summary_2<-reactive({


    outlier<-data.frame(CO_CUST=attributes(big_outliers),FLAG_OUTLIER=1)
    colnames(outlier)<-c("CO_CUST","FLAG_OUTLIER")

    data_summary_2<- user_clustering_raw%>%left_join(outlier,by="CO_CUST")%>%
      replace_na(list(FLAG_OUTLIER=0))

    colnames(data_summary_2)<-c("Codice Cliente", "Categoria Merc.",
                                "Spesa (EUR)","Cluster","Outlier") 
    data_summary_2

  })

  # 1 CLUSTER
  output$plot1 <- renderPlotly({
    categ<-input[["categ"]]
    d1<-selectedData()
    d1_clust<-d1%>%filter(DESC_CAT_MERC %in% categ)

    d1_clust<-d1_clust%>%filter(CLUSTER==1)

    plot_ly(
      type = 'scatterpolar',
      r = d1_clust$VAL_INV,
      theta = d1_clust$DESC_CAT_MERC,
      fill = 'toself'
    ) %>%
      layout(
        polar = list(
          radialaxis = list(
            visible = T,
            range = c(0,1)
          )
        ),
        showlegend = F
      )


   })

  output$plot1_data <- DT::renderDataTable({
    plot1_data<-summary_1()

    plot1_data<-plot1_data%>%filter(CLUSTER==1)%>%
                group_by(DESC_CAT_MERC)%>%
                summarise(VAL_INV=sum(VAL_INV),NUMERICA_CLIENTI=n_distinct(CO_CUST))%>%
                ungroup()%>%
                mutate(VAL_INV_PERC=round(VAL_INV/sum(VAL_INV),3)*100)

    plot1_data <- plot1_data[c("DESC_CAT_MERC", "VAL_INV", "VAL_INV_PERC","NUMERICA_CLIENTI")]
    colnames(plot1_data)<-c("Cat.Merceologica","Fatturato (EUR)","Fatturato %","Numero Clienti")
    DT::datatable(plot1_data,rownames = FALSE,options = list(dom = 't',
                  columnDefs = list(list(className = 'dt-center', targets = "_all"))))%>%formatCurrency(2:2, '')

  })

  # 2 CLUSTER
  output$plot2 <- renderPlotly({
    categ<-input[["categ"]]
    d1<-selectedData()

    d2_clust<-d1%>%filter(DESC_CAT_MERC %in% categ)
    d2_clust<-d2_clust%>%filter(CLUSTER==2)

    plot_ly(
      type = 'scatterpolar',
      r = d2_clust$VAL_INV,
      theta = d2_clust$DESC_CAT_MERC,
      fill = 'toself',mode="markers"
    ) %>%
      layout(
        polar = list(
          radialaxis = list(
            visible = T,
            range = c(0,1)
          )
        ),
        showlegend = F
      )


  })

  output$plot2_data <- DT::renderDataTable({

    plot2_data<-summary_1()

    plot2_data<-plot2_data%>%filter(CLUSTER==2)%>%
      group_by(DESC_CAT_MERC)%>%
      summarise(VAL_INV=sum(VAL_INV),NUMERICA_CLIENTI=n_distinct(CO_CUST))%>%ungroup()%>%
      mutate(VAL_INV_PERC=round(VAL_INV/sum(VAL_INV),3)*100)

    plot2_data <- plot2_data[c("DESC_CAT_MERC", "VAL_INV", "VAL_INV_PERC","NUMERICA_CLIENTI")]

    colnames(plot2_data)<-c("Cat.Merceologica","Fatturato (EUR)","Fatturato %","Numero Clienti")
    DT::datatable(plot2_data,rownames = FALSE,
                  options = list(dom = 't',
  columnDefs = list(list(className = 'dt-center', targets = "_all"))))%>%formatCurrency(2:2, '')
  })

  # 3 CLUSTER
  output$plot3 <- renderPlotly({
    categ<-input[["categ"]]
    d1<-selectedData()
    d3_clust<-d1%>%filter(DESC_CAT_MERC %in% categ)
    d3_clust<-d3_clust%>%filter(CLUSTER==3)

    plot_ly(
      type = 'scatterpolar',
      r = d3_clust$VAL_INV,
      theta = d3_clust$DESC_CAT_MERC,
      fill = 'toself'
    ) %>%
      layout(
        polar = list(
          radialaxis = list(
            visible = T,
            range = c(0,1)
          )
        ),
        showlegend = F
      )


  })

  output$plot3_data <- DT::renderDataTable({

    plot3_data<-summary_1()

    plot3_data<-plot3_data%>%filter(CLUSTER==3)%>%
      group_by(DESC_CAT_MERC)%>%
      summarise(VAL_INV=sum(VAL_INV),
                NUMERICA_CLIENTI=n_distinct(CO_CUST))%>%ungroup()%>%
      mutate(VAL_INV_PERC=round(VAL_INV/sum(VAL_INV),3)*100)



    plot3_data <- plot3_data[c("DESC_CAT_MERC", "VAL_INV", "VAL_INV_PERC","NUMERICA_CLIENTI")]

    colnames(plot3_data)<-c("Cat.Merceologica","Fatturato (EUR)","Fatturato %","Numero Clienti")
    DT::datatable(plot3_data,rownames = FALSE,options = list(dom = 't',
    columnDefs = list(list(className = 'dt-center', targets = "_all"))))%>%
      formatCurrency(2:2, '')
  })

  # 4 CLUSTER
  output$plot4 <- renderPlotly({

    categ<-input[["categ"]]
    d1<-selectedData()
    d4_clust<-d1%>%filter(DESC_CAT_MERC %in% categ)
    d4_clust<-d4_clust%>%filter(CLUSTER==3)

    plot_ly(
      type = 'scatterpolar',
      r = d4_clust$VAL_INV,
      theta = d4_clust$DESC_CAT_MERC,
      fill = 'toself'
    ) %>%
      layout(
        polar = list(
          radialaxis = list(
            visible = T,
            range = c(0,1)
          )
        ),
        showlegend = F
      )


  })

  output$plot4_data <- DT::renderDataTable({

    plot4_data<-summary_1()

    plot4_data<-plot4_data%>%filter(CLUSTER==4)%>%
      group_by(DESC_CAT_MERC)%>%
      summarise(VAL_INV=sum(VAL_INV),
                NUMERICA_CLIENTI=n_distinct(CO_CUST))%>%
      ungroup()%>%mutate(VAL_INV_PERC=round(VAL_INV/sum(VAL_INV),3)*100)

    plot4_data <- plot4_data[c("DESC_CAT_MERC", "VAL_INV", "VAL_INV_PERC","NUMERICA_CLIENTI")]

    colnames(plot4_data)<-c("Cat.Merceologica","Fatturato (EUR)","Fatturato %","Numero Clienti")
    DT::datatable(plot4_data,rownames = FALSE,options = list(dom = 't',
    columnDefs = list(list(className = 'dt-center', targets = "_all"))))%>%
    formatCurrency(2:2, '')
  })

  # rfm
  output$plot_rfm <- renderPlot({

    d<-selectedData_plot2()
    adding_point<- d[d$CO_CUST==input$cluster,]
    p1 <-  ggplot(d,aes(x=FREQ))+
      geom_histogram(fill="darkblue",col="white")+
      ggtitle("Frequenza Acquisti")+labs(x="Frequenza Acquisti",y="Conteggio")+
      geom_point(x=adding_point$FREQ,y=0,col="red",size=4)+
      theme(axis.text.x = element_text(angle=45,hjust=1,size=12),
            axis.title.x = element_blank(),plot.title = element_text(size=14,face="bold"))

    breaks <- pretty(range(d$MONET), n = nclass.FD(d$MONET), min.n = 1)
    bwidth <- breaks[2]-breaks[1]
    p2 <-  ggplot(d,aes(x=round(MONET,0)))+
      geom_histogram(fill="darkblue",col="white")+
      ggtitle("Valore Monetario Acquisti (EUR)")+labs(x="Valore Monetario",y="Conteggio")+
      scale_x_continuous(labels=dollar_format(prefix="€"))+
      geom_point(x=adding_point$MONET,y=0,col="red",size=4)+
      theme(axis.text.x = element_text(angle=45,hjust=1,size=12),
            axis.title.x = element_blank(),plot.title = element_text(size=14,face="bold"))

    p3 <-  ggplot(d,aes(x=LAST_PURCHASE))+
      geom_histogram(fill="darkblue",col="white")+
      ggtitle("Ultimo Acquisto (Giorni)")+labs(x="Ultimo Acquisto",y="Conteggio")+
      geom_point(x=adding_point$LAST_PURCHASE,y=0,col="red",size=4)+
      theme(axis.text.x = element_text(angle=45,hjust=1,size=12),
            axis.title.x = element_blank(),plot.title = element_text(size=14,face="bold"))

    grid.arrange(p1, p2,p3, nrow = 1)
  })

  # Data being displayed 2 tabitem
  output$Data <- DT::renderDataTable({
    DT::datatable(summary_2(),rownames = FALSE)%>% formatStyle(
      'Outlier',
      target = 'row',
      color =  styleEqual(c(1, 0), c('red', 'black')))%>%formatCurrency(3:3, '')
  })

  # Check CO_CLIENTE per errori input utente
  output$checkcluster <- renderUI({
    if (sum(input$cluster%in% user_clustering_raw$CO_CUST)==0)
      print ("Errore! Codice Cliente non presente...")})

}

我希望它已经足够清楚了,请不要降级

1 个答案:

答案 0 :(得分:2)

您错过了一个大写字母:

  menuItem("RFM",tabname="RFM",icon = icon("dashboard",lib = "glyphicon"))  ## That's the item I ve just added

tabname应该是tabName

此外,tabItem("RFM",应该是tabItem("rfm",,因为它已链接到id参数中的tabName

因此,下面给出了一个简化的工作版本-最大限度地减少代码是我发现问题的方式。希望这会有所帮助!

library(shiny)
library(shinydashboard)
library(plotly)

ui <- dashboardPage(
  dashboardHeader(title= "Acquisti Clienti CC"),
  dashboardSidebar(

    sidebarMenu(id="menu",
                menuItem(text = "Dashboard", tabName = "dashboard",icon = icon("stats",lib = "glyphicon")),
                menuItem(text = "Data", tabName = "Data",icon = icon("th-list",lib = "glyphicon")),
                menuItem(text = "RFM", tabName="rfm",icon = icon("th-list",lib = "glyphicon"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("dashboard",
              p('dashboard')
      )
      ,
      tabItem("Data",
              p('data')
      ),
      tabItem("rfm",
              p('rfm')
      )
    )
  )
)

server <- function(input,output){}
shinyApp(ui,server)