Use shiny text input and dplyr to filter rows in a dataframe

爱⌒轻易说出口 提交于 2019-12-03 21:30:17

As aosmith pointed out, you need to remove the quotes for filtering. Second, you should use == instead of %in% inside of filter(). Third, you would use switch() in other cases (read up on ?switch), but here you don't need it.

Your server.R should look like this:

library(shiny)
library(dplyr)
df1 <- data_frame(Name = c("Carlos","Pete","Carlos","Carlos","Carlos","Pete",
                         "Pete","Pete","Pete","Homer"),
                  Sales = c(3, 4, 7, 6, 4, 9, 1, 2, 1, 9))

shinyServer(function(input, output) {
  datasetInput <- reactive({
    df1 %>% filter(Name == input$text) %>% arrange(desc(Sales))
  })
  output$volume <- renderPrint({
    dataset <- datasetInput()
    dataset$Sales
  })
})

when you filter with your own search words,
1. make update function with: reactive
2. input the function within renderDT

ui = fluidPage(
        textInput('qry', 'Species', value='setosa'),
        DT::DTOutput('tbl'))
server = function(input, output){
        a = reactive({ filter(iris, Species == input$qry })
        output$tbl = renderDT(a())
shinyApp(ui,server)

good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!