使用dplyr过滤闪亮的数据表输出

时间:2019-03-12 06:05:45

标签: r datatable dplyr

我从MySQL的表输出中闪闪发亮。我有一些筛选器,这些筛选器取决于表输出。我将数据表作为输出,但它不是被动的。 我在这里给出了UI和Server的详细信息。你能告诉我为什么它没有反应性吗?

 unique_values2 <- sort(unique(opponents$opponent))

UI

ui <- navbarPage("Advanced",
             tabPanel("Page One",
                      column(4,radioButtons("firstorsecond", "First Or Second",
                                            choices = c(1:2),selected='1')),

                      column(4,radioButtons("tresult", "T Result",
                                            choices = list("Won" = "Won", "Lost" = "Lost"),selected="Won")),

                      column(4,radioButtons("mresult", "Match Result",
                                            choices = list("Won" = "Won", "Lost" = "Lost", "Tied"="Tied"),selected="Won")),

                      column(4,selectInput("opponent", "Select opponent", choices = unique_values2)),

                      column(4,radioButtons("position", "Position", 
                                            choices = c(1:11),inline = TRUE)),

                      dataTableOutput("values1")
             )
)

服务器

server <- function(input, output, session) {


 df<-dbGetQuery(mydb,"select 
 firstorsecond,position,opponent,tresult,mresult,points
 from customers where cust_id=7830")
 df  

 tablevalues1<-reactive
 ({
  firstorsecond<-as.integer(input$firstorsecond)
  position<-as.integer(input$position)
  opponent<-input$opponent  #String value
  mresult<-input$mresult   #String value
  tresult<-input$toss_result     #String value

  df %>% filter(firstorsecond %in% firstorsecond,position %in% 
  position,opponent %in% opponent,mresult %in% mresult,tresult %in% 
  tresult)
 })

 output$values1 <- renderDataTable({
 tablevalues1()
 })
}

非常感谢您的帮助! 预先感谢!

1 个答案:

答案 0 :(得分:0)

您尝试过的修改后的代码确实有效:

tablevalues1<-reactive ({ 
  fos<-as.integer(input$firstorsecond) 
  pos<-as.integer(input$position) 
  opp<-input$opponent 
  match<-input$mresult 
  toss<-input$tresult 
  df %>% filter(firstorsecond %in% fos,position %in% pos, opponent %in% opp,mresult %in% match,tresult %in%toss ) 
}) 

问题在于,对于如此小的数据集,过滤器过多。条件之间的逗号表示AND条件。它没有显示任何结果,因为没有任何匹配过滤条件的结果。尝试使用更大的数据集,或者将输入匹配到数据框中的确切行。

编辑:使用下面的代码和选项:1,1,mresult = Tied,opponent = A,tresult = Lost我在表输出中得到了一行。

library(shiny)
ui <- navbarPage("Advanced",
                 tabPanel("Page One",
                          column(4,radioButtons("firstorsecond", "First Or Second",
                                                choices = c(1:2),selected='1')),

                          column(4,radioButtons("tresult", "T Result",
                                                choices = list("Won" = "Won", "Lost" = "Lost"),selected="Won")),

                          column(4,radioButtons("mresult", "Match Result",
                                                choices = list("Won" = "Won", "Lost" = "Lost", "Tied"="Tied"),selected="Won")),

                          column(4,selectInput("opponent", "Select opponent", choices = c('A','B','C','D'))),

                          column(4,radioButtons("position", "Position", 
                                                choices = c(1:11),inline = TRUE)),

                          dataTableOutput("values1")
                 )
)
df <- data.frame(firstorsecond=c(1,1,2,1,2,1),
                 position=c(1,3,5,8,9,11),
                 opponent=unlist(strsplit('ABCADA','')),
                 mresult=unlist(strsplit('Tied Tied Lost Tied Won Lost',' ')),
                 tresult=unlist(strsplit('Lost Lost Lost Lost Won Lost',' ')),
                 points=c(2,3,4,6,1,3)
)
server <- function(input, output, session) {





  tablevalues1<-reactive ({ 
    print(df)
    fos<-as.integer(input$firstorsecond) 

    pos<-as.integer(input$position) 

    opp<-input$opponent 
    match<-input$mresult 
    toss<-input$tresult 
    print(dput(list(fos=fos,pos=pos,opp=opp,match=match,toss=toss)))
    df1 <- df %>% filter(firstorsecond %in% fos , position %in% pos , opponent %in% opp ,mresult %in% match , tresult %in%toss ) 
    print(df1)
    df1
  }) 

 output$values1 <- renderDataTable({
 tablevalues1()
 })
}

shinyApp(ui = ui,server = server)