rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
599 stars 179 forks source link

Using reactiveValues with formatRound and formatStyle #917

Closed tanyanchongDA closed 3 years ago

tanyanchongDA commented 3 years ago

Hi everyone,

I am facing issues with using reactiveValues() and datatable.

I have a minimally reproducible example below.

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)

ui <- fluidPage(
  actionButton(
    "button", "Generate"
  ),
  DTOutput("test")
)

server <- function(input, output){

  rResult <- reactiveValues()

  observeEvent(input$button, {
    rResult$iris <- iris
  })

  output$test <- renderDT({
    datatable(rResult$iris)
  })

}

shinyApp(ui, server)

There are no issues with the above. The code runs.

image

However, when I try to format the number of decimal places using formatRound, it cannot make reference to the columns.

library(shinydashboard)
library(tidyverse)
library(DT)

ui <- fluidPage(
  actionButton(
    "button", "Generate"
  ),
  DTOutput("test")
)

server <- function(input, output){

  rResult <- reactiveValues()

  observeEvent(input$button, {
    rResult$iris <- iris
  })

  output$test <- renderDT({
    datatable(rResult$iris) %>%
      formatRound(c("Sepal.Length", digits = 0))
  })

}

shinyApp(ui, server)

image

I have been trying to search online for results, but to no avail. Would be glad if you could help.

image

shrektan commented 3 years ago

I think changing the following line should solve your problem:

formatRound(c("Sepal.Length", digits = 0))

to

formatRound("Sepal.Length", digits = 0)