Grosvenor
Grosvenor

Reputation: 135

Why is my kable table producing this "no applicable method" error?

I have searched, but prior discussions (e.g. this one) unfortunately have not helped. I cannot diagnose the error in this problem. Fundamentally, I'm trying to exclude certain rows or columns from my table. However, my code:

library(kableExtra)
library(knitr)
library(dplyr) 
library(tidyr)      

knitr::kable(mtcars, digits=3) %>%
  dplyr::filter(cyl == 6) %>% 
    kableExtra::kable_styling()

produces this error:

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "knitr_kable"

I know from prior discussions that dplyr masks filter from stats, which is why I used the double colon ("::") to specify the library I think I want. The fact that the error cites filter_ (with the underscore) tells me that the problem is arising within dplyr or my understanding of it. dplyr is at version 0..8.0.1 and knitris at version 1.22.

Any help would be very much appreciated!

Upvotes: 2

Views: 974

Answers (1)

Andrew
Andrew

Reputation: 5138

You are doing the right things in the wrong order--but you are on the right track. You need to manipulate your dataset before piping it into kable. Let me know if this helps and if you have any other questions!

library(kableExtra)
library(knitr)
library(dplyr) 
library(tidyr)      

mtcars %>%
  filter(cyl == 6) %>%
  kable(digits=3) %>%
  kable_styling()

enter image description here

Upvotes: 2

Related Questions