0

I have scraped a table from the web using the rvest package. Some of the numbers in my table have values of ".5" and ".4". My goal is to convert the numbers to instead have values like ".500" and ".400". I have used the round function wrapped in a transmute function, ex: transmute(x =round(x,3)) and the signif function, ex: transmute(x= signif(x,3)).

These functions haven't seemed to do the trick. My ultimate goal is to use the DT package to put my tables into a shiny app. When I run the shiny app, the numbers aren't rounded to the correct decimal place in my tables.

Any ideas?

Thanks

natguy8
  • 41
  • 6
  • 1
    The `DT` package has the `formatRound` function for this. See `?formatRound` for examples and details. – Gregor Thomas Jun 18 '22 at 20:42
  • 2
    Does this: https://stackoverflow.com/questions/2287616/controlling-number-of-decimal-digits-in-print-output-in-r or this https://stackoverflow.com/questions/3443687/formatting-decimal-places-in-r/12135122#12135122 answer your question? – Dave2e Jun 18 '22 at 20:45
  • @Dave2e The format(round()) doesn't seem to do the trick. I have come across this idea before, I don't receive any errors while running the code, it just doesn't change the decimals – natguy8 Jun 18 '22 at 23:41
  • Look at the answers in @Dave2e's first and second links using `sprintf()`. – dcarlson Jun 18 '22 at 23:52
  • 1
    `format(round(.5, 3), nsmall = 3)` doesn't do the trick? I just tested it with 5 different table outputs, including DT and they all printed how ever many decimals I set. Perhaps I'm misunderstanding what you need? – guasi Jun 19 '22 at 01:18
  • @guasi formatRound works when I run the code. However, when I try to show my table in a shiny app, it doesn't seem to carry over. It is showing ".500" in my global environment tables, but when I run my shiny app it goes back to ".5". Any suggestions? – natguy8 Jun 21 '22 at 00:36
  • When you use `format(round(x, 3), nsmall = 3)` you will notice that the vector is now of type `chr` and not `int` or `dbl`. Make sure it stays as character before printing. The only thing I can think of, is that in your process they are reverting back to numbers and loose their formatting. – guasi Jun 21 '22 at 05:53

1 Answers1

1

The following uses format(round()), renderTable(digits = 5) and datatable() %>% formatRound(5) to set decimals. They all work.

library(shiny)
library(DT)

df <- data.frame(int = c(1L,2L,10L,20L),
                 num = c(10,1,.1,.01))
df$int_frmt <- format(round(df$int, 3), nsmall = 3)
df$num_frmt <- format(round(df$num, 3), nsmall = 3)

ui <- basicPage(
  p("renderTable plain"), tableOutput("tbl1"),
  p("renderTable w/ 'digits = 5'"), tableOutput("tbl1.2"),
  p("renderDT plain"), DTOutput("tbl2"),
  p("renderDT w/ 'formatRound(5)'"), DTOutput("tbl2.2")
)

server <- function(input, output) {

  output$tbl1 <- renderTable({
    df
  })
  
  output$tbl1.2 <- renderTable({
    df
  }, digits = 5)
  
  output$tbl2 <- renderDT({
    datatable(df)
  })
  
  output$tbl2.2 <- renderDT({
    datatable(df) %>% 
      formatRound(1:4, 5)
  })
  
}

shinyApp(ui, server)
guasi
  • 1,461
  • 3
  • 12