-1

I have a table where only some of the numbers should show digits after the decimal. I saw some examples in which formatC was used to convert the format in the dataframe before passing the data to knitr, but that aproach converts the values to character, and I need them to be numeric so I can specify the comma as the decimal separator.

Here is a minimal reproducible example:

---
title: "kble"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}

library(flexdashboard)
library(tidyverse)
library(shiny)
library(kableExtra)
library(janitor)

```

Title...
===================================== 
Inputs {.sidebar data-width=400}
-------------------------------------
```{r cars}

x1 <- c("rojo","verde","azul","amarillo")
x2 <- c(30000,300,30.3,3.22)
df1<- data.frame(x1,x2)

df1%>%
    knitr::kable(format.args = list(decimal.mark = ',', big.mark = "."),
                caption = "Title",
                align = "ccc"
                )%>%
   kable_styling("striped","hover",full_width = F)

```

I would like the "rojo" and "verde" rows to not have any decimal digits, and the "azul" and "amarillo" to keep them. Is there a way to achieve that? Thank you!

Natalias
  • 179
  • 6

1 Answers1

-1

Ok, after a lot of trial an error I found a solution that works with my current data, but I am not completely satisfied with:

---
title: "kble"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}

library(flexdashboard)
library(tidyverse)
library(shiny)
library(kableExtra)
library(janitor)

```

Title...
===================================== 
Inputs {.sidebar data-width=400}
-------------------------------------
```{r cars}

x1 <- c("rojo","verde","azul","amarillo")
x2 <- c(30000,300,30.3,3.22)
df1<- data.frame(x1,x2)

df1 <- df1 %>% 
    mutate(x3= formatC(x2, digits=3, format="fg",big.mark=".",decimal.mark = ','))

df1%>%
    knitr::kable(caption = "Title",
                align = "ccc"
                )%>%
   kable_styling("striped","hover",full_width = F)

```

I guess in the end i didn't need the data to be numeric in the kable table if I could modify the whole format beforehand.

Natalias
  • 179
  • 6