【问题标题】:Using dplyr for filtering data table output in shiny使用 dplyr 过滤闪亮的数据表输出
【发布时间】:2019-03-12 06:05:45
【问题描述】:

我有一个来自 MySQL 的表输出。我有某些过滤器,表输出依赖于这些过滤器。我将数据表作为输出,但它不是反应性的。 我在这里给出了 UI 和服务器的详细信息。你能告诉我为什么它没有反应吗?

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

用户界面

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()
 })
}

非常感谢您的帮助! 提前致谢!

【问题讨论】:

  • 问题可能是变量名冲突。您可能应该针对您的条件尝试类似mresult %in% input$mresult 的方法以避免歧义。现在filter 函数可能使用运算符的 LHS 和 RHS 表列。
  • 道歉。我可能应该更清楚 - 我的数据表对我的 input$ 变量没有反应。数据看起来像这样 - ibb.co/1zDBLXN .
  • 我尝试按照您的建议更改变量名称,Rohit。但即使是数据表(没有对过滤器的反应)现在也没有打印出来,而不是以前。
  • 您能否发布您的输入数据框以及您尝试使用它的结果的代码?这将有助于我进行实验。
  • 我已将您的新代码添加为对您帖子的编辑。不建议在 cmets 中发布多个代码表达式。请查看dput 函数,了解如何以首选格式发布数据

标签: r datatable dplyr


【解决方案1】:

您尝试的编辑代码确实有效:

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)

【讨论】:

  • 对于数据框中的特定行,它仍然对我不起作用。另外,在我选择过滤器值之前,不会显示整个表格吗?
  • 检查我的编辑。看看有没有帮助。此外,输入已经选择了默认值。它将使用这些选项。如果你不想要这个,你需要用必要的检查对其进行编码
  • 这似乎可以解决问题!有什么方法可以将整个数据框 df 显示为默认值,并且当我随后选择过滤器时,会显示特定数据?现在,只有选择特定的过滤器值,我才能看到数据。否则显示的表格为空。
  • 如何在不将所有过滤器更改为 checkboxGroupInput 并默认选择所有选项的情况下做到这一点?
  • 不要在ui.R 中提出selected 争论。然后根据输入是否为NULL 更改过滤器参数
猜你喜欢
  • 2015-12-26
  • 2015-07-14
  • 2019-07-04
  • 2017-05-05
  • 2021-04-25
  • 2015-11-15
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
相关资源
最近更新 更多