R Shiny中的动态AND滤镜

卡西姆

我正在努力在多个字段上创建一组动态数据过滤器,并在字段之间进行可选的AND / OR连接,如下所示:

在此处输入图片说明

这是我的示例代码。我只是不确定如何使筛选器联接(AND / OR)正常工作。

library(shiny)
library(dplyr)
library(DT)

data("baseball")

Year = unique(baseball$year)
Team = unique(baseball$team)
Stint = unique(baseball$stint)

runApp(list(ui = fluidPage(
  titlePanel("Summary"),

  sidebarLayout(
    sidebarPanel(

      selectInput("year", label = "Year", choices = Year, selected = NULL, multiple = TRUE),
      selectInput("filter_join1", label = "", choices = c("OR","AND")),
      selectInput("team", label = "Team", choices = Team, selected = NULL, multiple = TRUE),
      selectInput("filter_join2", label = "", choices = c("OR","AND")),
      selectInput("stint", label = "Stint", choices = Stint, selected = NULL, multiple = TRUE)
    ),

    mainPanel(
      DT::dataTableOutput("table")
    )
  )
),

server = function(input, output, session) {
  WorkingDataset   <- reactive({ 
    df_temp <- baseball %>%
      filter(
        is.null(input$year)  | year %in% input$year,
        is.null(input$team)  | team %in% input$team,
        is.null(input$stint) | stint %in% input$stint
      )
    }) 

  output$table <- DT::renderDataTable({ WorkingDataset() })

})

)
米哈尔·马伊卡(Michal Majka)

,我们有iris数据集,并且我们想对其进行一些子设置。

iris$Species

# We can also use `with` for that
with(iris, Species) 

# We are interested in more complicated subsetting though. Want to have all rows
# with 'setosa'
with(iris, Species %in% 'setosa')
iris[with(iris, Species %in% 'setosa'), ]

# Now 'setosa' with some more condition
iris[with(iris, Species %in% 'setosa' & Sepal.Length > 5.3), ]


# That works perfectly. There is, however, an another way doing the exact thing in r.
# We can input the subsetting condition as a character string, then change it to 
# the `expression` and `eval`uate it.

cond_str <- paste0("with(iris, Species %in% 'setosa' & Sepal.Length > 5.3)")
cond_str
# which is the same as
cond_str <- paste0("with(iris, ", "Species %in% ", "'", "setosa", "'", " & ", 
                   "Sepal.Length > ", "5.3", ")")
cond_str

# This second approach will prove very powerful since we will replace "setosa"
# with, say, `input$species` later on.


cond <- parse(text = cond_str)
cond
eval(cond)
iris[eval(cond), ] # √

由于它input$species可能是向量,因此它将变得稍微复杂一些,因此我们可以获取多个字符串作为输出。例如:

Spec <- c("setosa", "virginica") # ~ input$species
paste0("with(iris, Species %in% ", Spec, ")")

# We want only one character string! So, we'll have to collapse the vector Spec
paste0("with(iris, Species %in% ", 
       paste0(Spec, collapse = " "), ")")

# This is still not what we wanted. We have to wrap the entries into "c()"
# and add quote marks. So, it's going to be pretty technical:
paste0("with(iris, Species %in% ", 
       "c(", paste0("'", Spec, collapse = "',"), "'))")

# Now, this is what we wanted :) Let's check it 
check <- eval(parse(text = paste0("with(iris, Species %in% ", 
       "c(", paste0("'", Spec, collapse = "',"), "'))")))
iris[check, ] # √

现在,让我们转到闪亮的应用程序。由于我不知道在哪里可以找到baseball与您的变量匹配的数据集,因此我将使用diamonds软件包中数据集,ggplot2而不会使用dplyr

我稍微修改了您的应用-更改了变量名,然后使用了我上面介绍的技巧进行子设置。您可以轻松地将我的示例适合您的问题。

library(shiny)
library(DT)

# data("diamonds") don't know where I can find this dataset, hence I'll use
                # diamond dataset 

library(ggplot2) # for diamonds dataset


cut <- unique(as.character(diamonds$cut)) # or just levels(diamonds$cut)
color <- unique(as.character(diamonds$color))
clarity <- unique(as.character(diamonds$clarity))

runApp(list(ui = fluidPage(
    titlePanel("Summary"),

    sidebarLayout(
      sidebarPanel(
        # changed names of inputs
        selectInput("cut", label = "Cut", choices = cut, selected = NULL, multiple = T),
        selectInput("filter_join1", label = "", choices = c("OR","AND")),
        selectInput("color", label = "Color", choices = color, selected = NULL, multiple = T),
        selectInput("filter_join2", label = "", choices = c("OR","AND")),
        selectInput("clarity", label = "Clarity", choices = clarity, selected = NULL, multiple = T)
      ),

      mainPanel(
        DT::dataTableOutput("table")
      )
    )
  ),

  server = function(input, output, session) {

    WorkingDataset <- reactive({ 
      req(input$cut, input$color, input$clarity)
      # show table only if all three inputs are available

      # depending on filter_join inputs return "OR" or "AND"
      join1 <- ifelse(test = input$filter_join1 == "OR", yes = "| ", no = "& ")
      join2 <- ifelse(test = input$filter_join2 == "OR", yes = "| ", no = "& ")

      # You could do this differently: just set choices = c("OR" = "|", "AND" = "&"))
      # in the selectInput widget.

      # Similar as in the example above with the iris dataset. 

      cond_str <- paste0(
        "with(diamonds, ", 
        paste0("cut %in% ", "c(", paste0("'", input$cut, collapse = "',"), "')", colapse = " "),
        join1, 
        paste0("color %in% ", "c(", paste0("'", input$color, collapse = "',"), "')", colapse = " "),
        join2,
        paste0("clarity %in% ", "c(", paste0("'", input$clarity, collapse = "',"), "')", colapse = " "),
        ")")

      print(cond_str) # print the result to the console
      cond <- parse(text = cond_str)
      df <- as.data.frame(diamonds)[eval(cond), ]
      df
    }) 

    output$table <- DT::renderDataTable({ WorkingDataset() })

  })
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

R Shiny中的动态AND滤镜

来自分类Dev

R Shiny中的动态selectInput

来自分类Dev

如何下载R Shiny中动态的图?

来自分类Dev

在R Shiny中动态调整ggplot2图的大小

来自分类Dev

如何在Shiny R中动态更改传单地图的大小?

来自分类Dev

更新R Shiny中动态创建的selectInput框的选择

来自分类Dev

使用动态过滤器聚合R Shiny中的数据

来自分类Dev

R Shiny动态滑块动画

来自分类Dev

R Shiny访问动态输入

来自分类Dev

如何使用R中的Shiny中的滑块动态更新直方图的x轴范围

来自分类Dev

R Shiny中的环境

来自分类Dev

R Shiny中DataTable按钮扩展中下载文件名的动态命名

来自分类Dev

使用renderUI的R Shiny中的动态选项卡和复选框组

来自分类Dev

如何在R Shiny中访问动态创建的可控值并进行计算?

来自分类Dev

根据R Shiny中的其他选择动态更新两个selectInput框

来自分类Dev

如何在R Shiny中动态链接2个小部件选项?

来自分类Dev

产生动态/多个输入框以根据Shiny R中的用户选择收集数据

来自分类Dev

R Shiny:具有动态大小的情节

来自分类Dev

R Shiny:从选定的输入创建动态UI

来自分类Dev

R-Shiny DT:动态亮点

来自分类Dev

R Shiny:具有动态大小的情节

来自分类Dev

R Shiny - 动态创建的 UI 书签

来自分类Dev

R Shiny-动态文件输入标签

来自分类Dev

清晰度设计中的动态滤镜

来自分类Dev

R 中的动态列表

来自分类Dev

在R / Shiny中缓存图

来自分类Dev

R Shiny中的多个图

来自分类Dev

在R Shiny中读取文件

来自分类Dev

在R Shiny中显示图