简体   繁体   English

在 Shiny R 中的 selectInput() 中动态分组值

[英]Grouping Values Dynamically in selectInput() in Shiny R

I am trying to create a Simple Shiny dashboard which displays the values in the form of a table.我正在尝试创建一个简单的闪亮仪表板,它以表格的形式显示值。 For displaying the data in the form of a table I am using select input statements and renderDT().为了以表格的形式显示数据,我使用了选择输入语句和 renderDT()。 Kindly find the code below.请在下面找到代码。 The Code below is just a sample of what i want to do.下面的代码只是我想要做的一个例子。

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    library(dplyr)
    
    d <-
      data.frame(
        Product_Name = c(
          "Table",
          "Chair",
          "Bed",
          "Table",
          "Chair",
          "Bed",
          "Table",
          "Chair",
          "Bed",
          "Table",
          "Chair",
          "Bed",
          "Table",
          "Chair",
          "Bed"
        ),
        Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z", "Y", "Y", "Y", "Z", "Z", "Z"),
        Product_cat = c(1, 2, 3, 4, 2, 3, 4, 5, 6,9,7,6,3,5,6)
      )
    
    ui <- shinyUI(fluidPage(
      useShinydashboard(),
      tabPanel(
        "Plot",
        sidebarLayout(
          sidebarPanel(
            selectInput(
              "product_name",
              "Product Name",
              choices = NULL,
              selected = FALSE,
              multiple = FALSE
            ),
            selectInput(
              "Category",
              "Product Category",
              choices = NULL,
              selected = FALSE,
              multiple = TRUE
            ),
            #width = 2,
            position = "bottom"),
          mainPanel(DT::DTOutput("table1"))
          
        )
      )
    ))
    
    server <- function(input, output, session) {
      
      updateSelectInput(
        session,
        "product_name",
        "Product Name",
        choices = unique(d$Product_Name)
      )
      
      observeEvent(input$product_name,{
        
        y <- input$product_name
        x <- d %>% select(Product_Name,Product_cat) %>% filter(Product_Name %in% y) %>% 
          select(Product_cat)
        
        updateSelectInput(
          session,
          "Category",
          "Product Category",
          choices = (x)
        )
w <- input$Category
    z <- d %>% filter(Product_Name %in% y, Product_cat %in% w)
    
    output$table1 <-
      DT::renderDT(z)
        
      })
    }
    
    shinyApp(ui, server)

A particular product will have multiple categories it belong to.一个特定的产品将有它所属的多个类别。 The user selects those set of categories which they need to view.用户选择他们需要查看的那些类别集。

Now the issue which I am facing is with the Product Category.现在我面临的问题是产品类别。 I want to group certain categories dynamically.我想动态地对某些类别进行分组。 For example if the user instead of seeing the categories 1,4,9,3 for the Product Table separately, if they want to see grouped categories for example 1-4 for the product table.例如,如果用户不想单独查看产品表的类别 1、4、9、3,而是希望查看产品表的分组类别,例如 1-4。 How to achieve this in shiny Application dynamically ?如何在闪亮的应用程序中动态实现这一点?

I dont want to make any changes to the source data.我不想对源数据进行任何更改。 Also the grouping might be varying with each users who is going to use it.此外,分组可能因要使用它的每个用户而异。 There would be no point in doing the predefined grouping in the dataset.在数据集中进行预定义分组是没有意义的。

So the User flow will be as follows.所以用户流程如下。

  1. The User selects the Product Name用户选择产品名称
  2. Depending upon the Product Name the Product Categories will be present in the drop down box.根据产品名称,产品类别将出现在下拉框中。 Multiple category values can be selected.可以选择多个类别值。
  3. Once the User selects the value in the drop down box then a table will get displayed in the mainPanel().一旦用户在下拉框中选择了值,就会在 mainPanel() 中显示一个表格。

For example.例如。

  1. Lets take the user selects the Product Name as "Table"让用户选择产品名称作为“表”
  2. Next the Product category options in the drop down list would be 1, 4, 9, 3.接下来下拉列表中的产品类别选项将是 1、4、9、3。
  3. Lets the the user selects the values 1 and 9. Now the values which have Product name as Table and which belong to the categories 1 and 9 would be getting displayed.让用户选择值 1 和 9。现在将显示产品名称为表且属于类别 1 和 9 的值。

Whats my expectation is "is there a way in which the user has the ability group the categories as 1-9, 1-4 etc. dynamically in a Shiny Application ?我的期望是“有没有一种方法可以让用户在 Shiny 应用程序中动态地将类别分组为 1-9、1-4 等?

Kindly let me know your suggestions.请让我知道您的建议。

Thanks in advance.提前致谢。

Perhaps you are looking for this.也许你正在寻找这个。

d <-
  data.frame(
    Product_Name = c(
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed"
    ),
    Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z", "Y", "Y", "Y", "Z", "Z", "Z"),
    Product_cat = c(1, 2, 3, 4, 2, 3, 4, 5, 6,9,7,6,3,5,6)
  )

ui <- shinyUI(fluidPage(
  useShinydashboard(),
  tabPanel(
    "Plot",
    sidebarLayout(
      sidebarPanel(
        selectInput(
          "product_name",
          "Product Name",
          choices = NULL,
          selected = FALSE,
          multiple = FALSE
        ),
        selectInput(
          "Category",
          "Product Category",
          choices = NULL,
          selected = FALSE,
          multiple = TRUE
        ),
        #width = 2,
        position = "bottom"),
      mainPanel(DTOutput("table1"), DTOutput("table2"))
      
    )
  )
))

server <- function(input, output, session) {
  
  updateSelectInput(
    session,
    "product_name",
    "Product Name",
    choices = unique(d$Product_Name)
  )
  
  observeEvent(input$product_name,{
    
    y <- input$product_name
    x <- d %>% select(Product_Name,Product_cat) %>% filter(Product_Name %in% y) %>% 
      select(Product_cat)
    
    updateSelectInput(
      session,
      "Category",
      "Product Category",
      choices = (x)
    )
    
  })
  
  observeEvent(input$Category, {
    #w <- input$Category
    z <- d %>% filter(Product_Name %in% input$product_name & Product_cat %in% input$Category)
    
    output$table1 <- renderDT({
      if (is.null(input$Category)) return(NULL)
      else z
    })
    
    val <- c()
    newpc <- unique(z$Product_cat)
    val <- newpc[1]
    n <- length(newpc)
    
    if (n>1) {
      lapply(2:n, function(i){val<<- paste0(val,'-',newpc[i]) })
      
      z2 <- z %>% mutate(New_Product_cat = val)
     
    }
    
    output$table2 <- renderDT({
      if (n>1) z2
      else return(NULL)
    })
    
  })
  
}

shinyApp(ui, server)

输出

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

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