简体   繁体   English

R闪亮帮助:带有2个以上滤镜的条形图

[英]R Shiny Help: Bar plot with 2+ filters

I've created a basic Shiny app that plots a numeric output to a bar plot using different filters; 我创建了一个基本的Shiny应用程序,该应用程序使用不同的过滤器将数字输出绘制为条形图。 a filter that allows the user to select a "name" and a "type" associated with that name. 一个允许用户选择与该名称关联的“名称”和“类型”的过滤器。 The "name" filter is multi-select enabled, the "type" filter is not. “名称”过滤器已启用多选,“类型”过滤器未启用。 Everything is working great, except I realize that the way I've structured my code does not allow me to plot all of the different combinations I'd like to. 一切工作都很好,除了我意识到我构造代码的方式不允许我绘制我想要的所有不同组合。

#----df creation
name<-c("a","a","b","b","c","c")
type<-c("red","blue","blue","green","green","orange")
number<-c(30,20,42,16,23,62)
cbind(name,type,number)->df
as.data.frame(df)->df
unique(df$name)->name
unique(df$type)->type

#----shiny app
library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "name",
        label = "Name Selection",
        choices = name,
        selected = "a",
        multiple = TRUE
      ),
      radioButtons(
        inputId = "type",
        label = "Type Select",
        choices = type,
        selected = "red"
      )
    ),
    mainPanel(
      plotOutput(
        outputId = "graph"
      )
    )
  )

)

server <- function(input, output) {

  output$graph <- renderPlot({

    filtered <- 
      df %>%
      filter(name == input$name) %>%
      filter(type == input$type)

    ggplot(filtered,aes(x=name,y=number)) + geom_bar(stat = "identity")

  })


 }

 shinyApp(ui = ui, server = server)

The above code allows me to select multiple names, and different types, however it appears as it it's order dependant. 上面的代码使我可以选择多个名称和不同的类型,但是它的出现取决于顺序。 For example, try selecting "a" then "b", and then "blue" as the type. 例如,尝试选择“ a”,然后选择“ b”,然后选择“ blue”作为类型。 No graph. 没有图。 Now try the same in reverse: "b" then "a", "blue" as the type. 现在,以相反的方式尝试相同的类型:“ b”,然后“ a”,“ blue”作为类型。 The desired output is produced. 产生所需的输出。 Any idea what's going on here? 知道这里发生了什么吗? Thanks all. 谢谢大家

Since you are using a multiple select, you need to change your filter to use the %in% operator: 由于使用的是单选,因此需要更改过滤器以使用%in%运算符:

Checking for equality between two vectors results in pair-wise comparisons, recycling the shorter vector, if necessary. 检查两个向量之间的相等性会导致成对比较,并在必要时回收较短的向量。 Notice the warning below that the shorter vector doesn't divide nicely into the larger: 请注意以下警告,较短的向量不能很好地划分为较大的向量:

a <- c(2,1)
b <- c(1,2,3)

a == b # [1] FALSE FALSE FALSE
       # Warning message:
       # In a == b : longer object length is not a multiple of shorter 
       # object length

a %in% b # [1] TRUE TRUE

Also, your assignment-to-the-right ( -> ) in lines 5-8, while not wrong, is generally frowned upon, although I'm sure people will defend its use. 同样,尽管我相信人们会捍卫它的使用,但您通常不会对第5-8行中的右分配( -> )感到不满意。

I believe this does what you're looking for: 我相信这可以满足您的需求:

#----df creation
name<-c("a","a","b","b","c","c")
type<-c("red","blue","blue","green","green","orange")
number<-c(30,20,42,16,23,62)
cbind(name,type,number)->df
as.data.frame(df)->df
unique(df$name)->name
unique(df$type)->type

#----shiny app
library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "name",
        label = "Name Selection",
        choices = name,
        selected = "a",
        multiple = TRUE
      ),
      radioButtons(
        inputId = "type",
        label = "Type Select",
        choices = type,
        selected = "red"
      )
    ),
    mainPanel(
      plotOutput(
        outputId = "graph"
      )
    )
  )

)

server <- function(input, output) {

  output$graph <- renderPlot({

    filtered <- 
      df %>%
      filter(name %in% input$name) %>%
      filter(type == input$type)

    ggplot(filtered,aes(x=name,y=number)) + geom_bar(stat = "identity")

  })


}

shinyApp(ui = ui, server = server)

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

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