繁体   English   中英

在闪亮的应用程序中创建具有动态组数的分组条形图

[英]Create plotly grouped bar chart with dynamic number of groups in a shiny app

我在下面有一个闪亮的应用程序,用户可以在其中选择一个或两个数据帧,然后根据其他过滤器创建一个条形图,该条形图将显示一个或两个数据帧的值。 现在我还没有将它连接到小部件以显示我想要实现的目标。 它让我困惑如何将数据帧df1df2一起或一次一个地提供给plot_ly()add_trace()函数。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
library(plotly)
shinyApp(
    ui = dashboardPagePlus(
        header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                     
        ),
        sidebar = dashboardSidebar(width = 320,
                                   checkboxGroupInput("checkGroup", label = "Select Dataset", 
                                                      choices = list("df1", "df2"),
                                                      selected = "df1"),
                                   checkboxGroupInput("checkGroup2", label = "Select Social Network", 
                                                      choices = list("FACEBOOK", "INSTAGRAM"),
                                                      selected = "FACEBOOK"),
                                   radioButtons("radio", label = "Choose type of values",
                                                choices = list("Absolute", "Percentages" ), 
                                                selected = "Absolute"),
                                   uiOutput("x30"),
                                   uiOutput("x16"),
                                   uiOutput("value")
                                   
                                   
        ),
        body = dashboardBody(
            plotlyOutput("plot")
        )
        
        
    ),
    server = function(input, output) {
       
        page<-c("ONE","TWO","THREE")
        network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
        av<-c(3.5,7.2,8.7)
        growth<-c(5,7,9)
        av2<-c(3.5,7.2,8.7)
        growth2<-c(5,7,9)
        df1<-data.frame(page,network,av,growth,av2,growth2)
        
        page<-c("ONE","TWO","THREE")
        network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
        av<-c(4.5,7.9,8.7)
        growth<-c(5,6,9)
        av2<-c(3.5,9.2,8.7)
        growth2<-c(5,43,9)
        df2<-data.frame(page,network,av,growth,av2,growth2)
        
        output$x30<-renderUI({
            if("df1" %in% input$checkGroup){
                new<-subset(df1, network %in% input$checkGroup2)
                pickerInput(
                    inputId = "x3"#The colname of selected column
                    ,
                    label = "Select Profile-df1" #The colname of selected column
                    ,
                    choices = as.character(new$page)#all rows of selected column
                    ,
                    multiple = TRUE,options = list(`actions-box` = TRUE)
                    
                )
            }
            else{
                return(NULL)
            }
            
        })
        output$x16<-renderUI({
            if("df2" %in% input$checkGroup){
                new<-subset(df2, network %in% input$checkGroup2)
                pickerInput(
                    inputId = "x6"#The colname of selected column
                    ,
                    label = "Select Profile-df2" #The colname of selected column
                    ,
                    choices = as.character(new$page)#all rows of selected column
                    ,
                    multiple = TRUE,options = list(`actions-box` = TRUE)
                    
                )
            }
            else{
                return(NULL)
            }
            
        })
        output$value<-renderUI({
            if(input$radio=="Absolute"){
                pickerInput(
                    inputId = "val"
                    ,
                    label = "Select Absolut Value" 
                    ,
                    choices = c("growth","growth2")#all rows of selected column
                    ,
                    multiple = F,options = list(`actions-box` = TRUE)
                    
                )
            }
            else{
                pickerInput(
                    inputId = "val"
                    ,
                    label = "Select Percentage Value" 
                    ,
                    choices = c("av","av2")#all rows of selected column
                    ,
                    multiple = F,options = list(`actions-box` = TRUE)
                    
                )
            }
            
        })
        output$plot<-renderPlotly({
            fig <- plot_ly(df1, x = ~page, y = ~growth, type = 'bar', name = 'growth')
            fig <- fig %>% add_trace(df2,y = ~growth, name = 'growth')
            fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
            
            fig
        })
    }
)

反应式数据帧dfa()提供来自一个或两个数据帧的选定数据。 现在, plotly需要一些工作。 请注意,在dfa()使用变量会产生一些错误,因为df1df2都在服务器中定义并且在output$plot中可用,因此,您可能需要使用dfa()$id等。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
library(plotly)
shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                 
    ),
    sidebar = dashboardSidebar(width = 320,
                               checkboxGroupInput("checkGroup", label = "Select Dataset", 
                                                  choices = list("df1", "df2"),
                                                  selected = "df1"),
                               checkboxGroupInput("checkGroup2", label = "Select Social Network", 
                                                  choices = list("FACEBOOK", "INSTAGRAM"),
                                                  selected = "FACEBOOK"),
                               radioButtons("radio", label = "Choose type of values",
                                            choices = list("Absolute", "Percentages" ), 
                                            selected = "Absolute"),
                               uiOutput("x30"),
                               uiOutput("x16"),
                               uiOutput("value")
                               
    ),
    body = dashboardBody(
      plotlyOutput("plot") , DTOutput("tb1")
    )
    
    
  ),
  server = function(input, output) {
    
    page<-c("ONE","TWO","THREE")
    network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
    av<-c(3.5,4.2,8.7)
    growth<-c(4,7,9)
    av2<-c(3.5,7.2,4.7)
    growth2<-c(4,7,9)
    id <- rep("df1",3)
    df1<-data.frame(page,network,av,growth,av2,growth2,id)
    
    page<-c("ONE","TWO","THREE")
    network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
    av<-c(4.5,7.9,8.7)
    growth<-c(5,4,8)
    av2<-c(3.5,9.2,6.7)
    growth2<-c(5,4,9)
    id <- rep("df2",3)
    df2<-data.frame(page,network,av,growth,av2,growth2,id)
    
    output$x30<-renderUI({
      if (is.null(input$checkGroup)) {
        return(NULL)
      }else if("df1" %in% input$checkGroup){
        new<-subset(df1, network %in% input$checkGroup2)
        pickerInput(
          inputId = "x3" #The rownames of selected column
          ,
          label = "Select Profile-df1" #The colname of selected column
          ,
          choices = as.character(new$page) #all rows of selected column
          ,
          multiple = TRUE,options = list(`actions-box` = TRUE)
          
        )
      }
      else{
        return(NULL)
      }
      
    })
    output$x16<-renderUI({
      if (is.null(input$checkGroup)) {
        return(NULL)
      }else if( "df2" %in% input$checkGroup){
        new2<-subset(df2, network %in% input$checkGroup2)
        pickerInput(
          inputId = "x6" #The rownames of selected column
          ,
          label = "Select Profile-df2" #The colname of selected column
          ,
          choices = as.character(new2$page) #all rows of selected column
          ,
          multiple = TRUE,options = list(`actions-box` = TRUE)
          
        )
      }
      else{
        return(NULL)
      }
      
    })
    output$value<-renderUI({
      if(req(input$radio)=="Absolute"){
        pickerInput(
          inputId = "val"
          ,
          label = "Select Absolut Value" 
          ,
          choices = c("growth","growth2") #all rows of selected column
          ,
          multiple = F, options = list(`actions-box` = TRUE)
          
        )
      }else if(req(input$radio)=="Percentages"){
        pickerInput(
          inputId = "val"
          ,
          label = "Select Percentage Value" 
          ,
          choices = c("av","av2")#all rows of selected column
          ,
          multiple = F,options = list(`actions-box` = TRUE)
          
        )
      } else {return(NUL)}
      
    })
    
    dfa <- reactive({
      req(input$val)
      if (is.null(input$checkGroup)) {return(NULL)}
      if (input$checkGroup == "df1" | length(input$checkGroup) == 2){
        df11 <- df1 %>% filter(page %in% req(input$x3) & network %in% req(input$checkGroup2))
      }else df11 <- NULL
      if (input$checkGroup == "df2" | length(input$checkGroup) == 2){
        df22 <- df2 %>% filter(page %in% req(input$x6) & network %in% req(input$checkGroup2)) 
      }else df22 <- NULL
      
      if (is.null(df11) & is.null(df22)) {return(NULL)
      }else {
        if (is.null(df11)){df <- df22
        }else if (is.null(df22)){df <- df11
        }else { df <- rbind(df11,df22) }
        df <- df %>%  transform(y=df[[as.name(input$val)]], x=page)
      }
      df
    })
    
    output$tb1 <- renderDT(dfa())
    
    output$plot<-renderPlotly({
      if (is.null(dfa())) return(NULL)
      xvar <- unique(dfa()$page)
      xform <- list(categoryorder = "array",
                    categoryarray = xvar)  ## to get the bars in the order you wish to display
      fig <- plot_ly() 
     
      fig <- fig %>% add_trace( dfa() , x = ~x, y = ~y, type='bar', name='growth', fill=x, color=dfa()$id)
      fig <- fig %>% layout(xaxis = list(xform, title="Page"), 
                            yaxis = list(title = 'Count'), barmode = 'group')
      fig
    })
  }
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM