【发布时间】: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,我需要eval 和input$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)
【问题讨论】: