Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A lot of digits in shiny DT #1071

Open
3 tasks done
ly0ka opened this issue Jun 22, 2023 · 8 comments
Open
3 tasks done

A lot of digits in shiny DT #1071

ly0ka opened this issue Jun 22, 2023 · 8 comments

Comments

@ly0ka
Copy link

ly0ka commented Jun 22, 2023

After upgrading to R version 4.3.1 and DT 0.28, in shiny DT tables sometimes have a lot of digits, but in the dataframe the number is rounded - round(x / 0.01) * 0.01
It is not suitable to set a common number of digits for all row. Each row has a unique rounding step 0.1, 0.01, 0.001...

image

ui <- fluidPage(
  actionButton(inputId = "update", label = "Update"),
  DTOutput("tbl")
)

server <- function(input, output) {
  dt_tbl <- eventReactive(
    eventExpr = input$update,
    ignoreNULL = FALSE,
    valueExpr = data.table(price = round(runif(n = 10, min = 0.8, max = 0.85) / 0.01) * 0.01))
  
  output$tbl <- renderDT(isolate(expr = {
    datatable(dt_tbl(),
              class = "cell-border stripe", extensions = "FixedHeader", selection = "none", rownames = FALSE,
              options = list(dom = "t", fixedHeader = TRUE, paging = FALSE, ordering = FALSE,
                             columnDefs = list(list(className = "dt-center", targets = "_all"))))
  }))
  
  observe(dataTableProxy("tbl") %>% replaceData(dt_tbl(), rownames = FALSE))
  
}

shinyApp(ui, server)

Stack Overflow


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.
@stla
Copy link
Collaborator

stla commented Jun 23, 2023

Hello,

That's normal. The way you use to round the numbers is not correct:

> x = round(runif(n = 10, min = 0.8, max = 0.85) / 0.01) * 0.01
> print(x, digits = 16)
 [1] 0.8300000000000001 0.8200000000000001 0.8400000000000000 0.8400000000000000
 [5] 0.8300000000000001 0.8000000000000000 0.8000000000000000 0.8400000000000000
 [9] 0.8200000000000001 0.8400000000000000

@ly0ka
Copy link
Author

ly0ka commented Jun 23, 2023

But in the version R 4.2.3, the table did not show these digits

@stla
Copy link
Collaborator

stla commented Jun 23, 2023

Strange. I tried other ways but the problem occurs as well:

x = round(runif(n = 10, min = 0.8, max = 0.85), 2) 
print(x, digits = 16)

x = round(runif(n = 10, min = 0.8, max = 0.85) * 100) / 100 
print(x, digits = 16)

You should use formatRound?

@ly0ka
Copy link
Author

ly0ka commented Jun 23, 2023

No, each row has a unique round value
fromatRound rounds all over the column

@stla
Copy link
Collaborator

stla commented Jun 23, 2023

Hmm that's annoying. Maybe you should use JavaScript then, the option rowCallback.

@stla
Copy link
Collaborator

stla commented Jun 28, 2023

In fact the easiest way is to get the rounded numbers as character strings and then to use the column-wise option render to transform them to numbers when you sort a column (if you don't use the sorting, no need of render).

@ly0ka
Copy link
Author

ly0ka commented Jun 28, 2023

Agree, I came to this conclusion and use as.character for all numeric columns before render DT

@stla
Copy link
Collaborator

stla commented Sep 6, 2023

The problem with as.character is that you will not get what is expected when sorting a column. Unless you use the render option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants