0

I am using Rmarkdown and want to display tables using knitr::kable().

I have two chunks of code which should output a table. The thing is one works and one does not. Both are trying to output a dataframe.

This one works:

### Payment method comparison
How much invoice and account purchases grew for the switching merchants.

```{r, include = TRUE, echo = FALSE}
leg_growth <- skco.growth(groups = c('Purchase_Country', 'Paymethod'), unit = 'Volume') %>%
  select(Purchase_Country, Paymethod, relative_growth) %>%
  filter(!is.na(relative_growth),
         Paymethod != 'Credit card') %>%
  mutate(relative_growth = to.percent(relative_growth - 1)) 

leg_growth%>%knitr::kable()

```

This one does not:

###Number of Brands
This is a similar scatter plot to the previous one but shows merchants grouped by the Industry Segment.
Number of switching merchants included in the sample.

```{r, include = TRUE, echo = FALSE}
a<-switching_data %>%
  group_by(Purchase_Country) %>%
  summarize(nr_brands = n_distinct(Brand_id))

a%>%knitr::kable()
```

I am not sure what is the difference between the two but running the code in the Rmarkdown it does not show me anything.

The structure of the data:

> str(a)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   4 obs. of  2 variables:
 $ Purchase_Country: chr  "d" "f" "n" "s"
 $ nr       : int  24 94 140 440
> str(leg_growth)
'data.frame':   8 obs. of  3 variables:
 $ Purchase_Country: chr  "d" "d" "f" "f" ...
 $ method       : Factor w/ 9 levels "A","C",..: 1 7 1 7 1 7 1 7
 $ growth : chr  "55%" "-3%" "18%" "91%" ...
Peter Cassar
  • 23
  • 1
  • 7
  • How should we know without a reproducible example. – Axeman Jun 15 '17 at 14:50
  • As Axeman says, we can't help you without a reproducible example. Even making a reproducible example can help you find the problem by yourself. See [https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – RobertMyles Jun 15 '17 at 15:07
  • If you're happy to share your original data, you can just do `dput(leg_growth)` and paste the structure of your dataframe into your question, then repeat with `dput(a)`. This will allow us to recreate your dataframes and see where the problem might be. – meenaparam Jun 15 '17 at 15:16
  • 1
    The difference is likely due to the difference between `leg_growth` and `a`. You could print `str(leg_growth)` and `str(a)` to see what the differences are. – user2554330 Jun 15 '17 at 21:40
  • Hi all thanks for your responses, I was hoping you would all point to some simple oversight I made and hence did not include the data. I will include it in the question now however. – Peter Cassar Jun 16 '17 at 08:53
  • No idea what's going on. When I run `kable()` on an object like `a`, it just works. You might want to try `class(a) <- "data.frame"` to remove the `dplyr` classes, but I don't see why that would help. – user2554330 Jun 16 '17 at 17:08

0 Answers0