【问题标题】:Use Shiny to choose column, equality, and value to filter by conditions使用 Shiny 选择列、等式和值以按条件过滤
【发布时间】:2020-04-09 03:32:32
【问题描述】:

我正在创建一个闪亮的应用程序,我希望用户能够选择一个列和条件,从而产生可用于过滤数据框的 input$COLUMN input$CONDITION input$VALUE

期望的输出

iris %>% filter(input$COLUMN input$CONDITION input$VALUE) == iris %>% filter(Sepal.Length > 4.7)

为此,我需要为input$COLUMN 使用rlang,我需要evalinput$CONDITION,并且我需要在适当的时候将input$VALUE 转换为数字。 (我正在我的verbatimTextOutput 中尝试这个)

实现这一目标的最佳方法是什么?我认为让整个表达式成为一个在整洁的管道中解析的字符串可能是可行的方法,但我愿意接受其他建议!!

library(shiny)
library(tidyverse)


ui <- fluidPage(

   # Sidebar with an input for column
   # boolean input
   # and value input
   sidebarLayout(
      sidebarPanel(
        fluidRow(column(4, selectInput("COLUMN", "Filter By:", choices = colnames(iris))),
                 column(4, selectInput("CONDITION", "Boolean", choices = c("==", "!=", ">", "<"))),
                 column(4, uiOutput("COL_VALUE")))
      ),

      # Show text generated by sidebar
      # use text in tidy pipeline to create subsetted dataframe
      mainPanel(
         verbatimTextOutput("as_text"),
         tableOutput("the_data")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$COL_VALUE <- renderUI({
    x <- iris %>% select(!!sym(input$COLUMN))
    selectInput("VALUE", "Value", choices = x)
  })

  filtering_string <- reactive ({
    paste0("!!sym(", input$COLUMN, ") ", input$CONDITION, " ", input$VALUE)
  })

   output$as_text <- renderText({
     filtering_string()
   })


   output$the_data <- renderTable({
     iris %>%
       eval(parse(text = filtering_string()))
   })
}

# Run the application 
shinyApp(ui = ui, server = server)


【问题讨论】:

    标签: r shiny tidyverse rlang


    【解决方案1】:

    我对@9​​87654321@ 不太熟悉,但你可以这样做:

      output$the_data <- renderTable({
    
        # To hide error when no value is selected
        if (input$VALUE == "") {
          my_data <- "" 
        } else {
          my_data <- iris %>% 
            filter(eval(parse(text = paste0(input$COLUMN, input$CONDITION, input$VALUE))))  
        }
    
        return(my_data)
    
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-18
      • 2018-10-15
      • 2021-12-19
      • 2018-02-03
      • 1970-01-01
      • 2018-12-30
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多