r - MenuItem 呈现为 subItem

标签 r shiny shinydashboard

sidebarMenu 中的 MenuItem 有问题。当我添加第三个 MenuItem (RFM) 时,它似乎在 ui 中呈现为子项,当我单击该项目时,即使在服务器中也没有显示任何内容。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')))
              )
      )
          )
)

服务器.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...")})

}

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

最佳答案

你错过了一个大写字母:

  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)

关于r - MenuItem 呈现为 subItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493181/

相关文章:

r - gvisGauge 未在 R Shiny(空白空间)中显示 - googleVis - 仪表

r - 分解 Shiny 代码的最佳实践

R Shiny 的 csv 或 excel 上传选项

r - 从 CSV 创建 xts 对象

r - 在 R 中,比较不同数据帧中的两列并删除重复项

javascript - 在 htmlWidgets 的 onRender() 中修改选定点的有效方法

R/shinyjs : Plot appears beyond the box width after using function show/hide sidebar

r - 让用户在 Shinydashboard 应用程序中创建不同的按钮操作

r - SAS 宏到 R %sysfunc

javascript - 将数据从js发送到R Shiny变量