R DT::datatables formatting multiple columns simultaneously









up vote
0
down vote

favorite












I wish to implement formatCurrency() and formatPercentage() (both from DT package) across multiple columns simultaneously in a shiny dashboard. I am using shinymaterial for the given example.



I am currently doing the following:



# The packages to load.
required_packages <- c("shiny", "shinymaterial", "DT", "tidyverse")

# This function will load in all the packages needed.
lapply(required_packages, require, character.only = TRUE)

# A table example.
ui <- material_page(
title = "Example table",
tags$h1("Table example"),
material_card(
title = "Table",
material_row(
DT::dataTableOutput("data_table_example")
),
depth = 1
)
)

server <- function(input, output)

data_table_example_data = tibble(
Person = paste0("Person ", c(1:100)),
`Price $` = rnorm(100, 50000, 500),
`Cost $` = rnorm(100, 30000, 300),
`Probability %` = rnorm(100, 0.6, 0.1),
`Win %` = rnorm(100, 0.5, 0.2)
)

# This will create an output summary table
output$data_table_example = renderDataTable(
result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
class = 'cell-border stripe compact', rownames = FALSE) %>%
formatCurrency("Price $") %>%
formatCurrency("Cost $") %>%
formatPercentage("Probability %", digits = 1) %>%
formatPercentage("Win %", digits = 1)
)


shinyApp(ui = ui, server = server)


However, what I wish to do is, within the renderDataTable() function, to simplify the format functions into fewer lines. For example, implement formatCurrency() in any column with a "$" and formatPercentage() in any column with a "%".



I have done a fair bit of searching for an appropriate but could not find a solution, but I assume I am just missing a fairly simple solution.



Something like:



# This will create an output summary table
output$data_table_example = renderDataTable(
result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
class = 'cell-border stripe compact', rownames = FALSE) %>%
formatCurrency(grepl("$", colnames()) %>%
formatPercentage(grepl("%", colnames()), digits = 1)
)


A few additional points:



  • The tibble will actually be a reactive

  • This example is a very trivial version of a rather more complex table and set of reactives

  • I do not want to implement the formatting in the reactive part since I find this then messes with the DT sorting function, since it assumes the column is a character string

Any help will be greatly appreciated










share|improve this question

























    up vote
    0
    down vote

    favorite












    I wish to implement formatCurrency() and formatPercentage() (both from DT package) across multiple columns simultaneously in a shiny dashboard. I am using shinymaterial for the given example.



    I am currently doing the following:



    # The packages to load.
    required_packages <- c("shiny", "shinymaterial", "DT", "tidyverse")

    # This function will load in all the packages needed.
    lapply(required_packages, require, character.only = TRUE)

    # A table example.
    ui <- material_page(
    title = "Example table",
    tags$h1("Table example"),
    material_card(
    title = "Table",
    material_row(
    DT::dataTableOutput("data_table_example")
    ),
    depth = 1
    )
    )

    server <- function(input, output)

    data_table_example_data = tibble(
    Person = paste0("Person ", c(1:100)),
    `Price $` = rnorm(100, 50000, 500),
    `Cost $` = rnorm(100, 30000, 300),
    `Probability %` = rnorm(100, 0.6, 0.1),
    `Win %` = rnorm(100, 0.5, 0.2)
    )

    # This will create an output summary table
    output$data_table_example = renderDataTable(
    result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
    class = 'cell-border stripe compact', rownames = FALSE) %>%
    formatCurrency("Price $") %>%
    formatCurrency("Cost $") %>%
    formatPercentage("Probability %", digits = 1) %>%
    formatPercentage("Win %", digits = 1)
    )


    shinyApp(ui = ui, server = server)


    However, what I wish to do is, within the renderDataTable() function, to simplify the format functions into fewer lines. For example, implement formatCurrency() in any column with a "$" and formatPercentage() in any column with a "%".



    I have done a fair bit of searching for an appropriate but could not find a solution, but I assume I am just missing a fairly simple solution.



    Something like:



    # This will create an output summary table
    output$data_table_example = renderDataTable(
    result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
    class = 'cell-border stripe compact', rownames = FALSE) %>%
    formatCurrency(grepl("$", colnames()) %>%
    formatPercentage(grepl("%", colnames()), digits = 1)
    )


    A few additional points:



    • The tibble will actually be a reactive

    • This example is a very trivial version of a rather more complex table and set of reactives

    • I do not want to implement the formatting in the reactive part since I find this then messes with the DT sorting function, since it assumes the column is a character string

    Any help will be greatly appreciated










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wish to implement formatCurrency() and formatPercentage() (both from DT package) across multiple columns simultaneously in a shiny dashboard. I am using shinymaterial for the given example.



      I am currently doing the following:



      # The packages to load.
      required_packages <- c("shiny", "shinymaterial", "DT", "tidyverse")

      # This function will load in all the packages needed.
      lapply(required_packages, require, character.only = TRUE)

      # A table example.
      ui <- material_page(
      title = "Example table",
      tags$h1("Table example"),
      material_card(
      title = "Table",
      material_row(
      DT::dataTableOutput("data_table_example")
      ),
      depth = 1
      )
      )

      server <- function(input, output)

      data_table_example_data = tibble(
      Person = paste0("Person ", c(1:100)),
      `Price $` = rnorm(100, 50000, 500),
      `Cost $` = rnorm(100, 30000, 300),
      `Probability %` = rnorm(100, 0.6, 0.1),
      `Win %` = rnorm(100, 0.5, 0.2)
      )

      # This will create an output summary table
      output$data_table_example = renderDataTable(
      result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
      class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency("Price $") %>%
      formatCurrency("Cost $") %>%
      formatPercentage("Probability %", digits = 1) %>%
      formatPercentage("Win %", digits = 1)
      )


      shinyApp(ui = ui, server = server)


      However, what I wish to do is, within the renderDataTable() function, to simplify the format functions into fewer lines. For example, implement formatCurrency() in any column with a "$" and formatPercentage() in any column with a "%".



      I have done a fair bit of searching for an appropriate but could not find a solution, but I assume I am just missing a fairly simple solution.



      Something like:



      # This will create an output summary table
      output$data_table_example = renderDataTable(
      result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
      class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency(grepl("$", colnames()) %>%
      formatPercentage(grepl("%", colnames()), digits = 1)
      )


      A few additional points:



      • The tibble will actually be a reactive

      • This example is a very trivial version of a rather more complex table and set of reactives

      • I do not want to implement the formatting in the reactive part since I find this then messes with the DT sorting function, since it assumes the column is a character string

      Any help will be greatly appreciated










      share|improve this question













      I wish to implement formatCurrency() and formatPercentage() (both from DT package) across multiple columns simultaneously in a shiny dashboard. I am using shinymaterial for the given example.



      I am currently doing the following:



      # The packages to load.
      required_packages <- c("shiny", "shinymaterial", "DT", "tidyverse")

      # This function will load in all the packages needed.
      lapply(required_packages, require, character.only = TRUE)

      # A table example.
      ui <- material_page(
      title = "Example table",
      tags$h1("Table example"),
      material_card(
      title = "Table",
      material_row(
      DT::dataTableOutput("data_table_example")
      ),
      depth = 1
      )
      )

      server <- function(input, output)

      data_table_example_data = tibble(
      Person = paste0("Person ", c(1:100)),
      `Price $` = rnorm(100, 50000, 500),
      `Cost $` = rnorm(100, 30000, 300),
      `Probability %` = rnorm(100, 0.6, 0.1),
      `Win %` = rnorm(100, 0.5, 0.2)
      )

      # This will create an output summary table
      output$data_table_example = renderDataTable(
      result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
      class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency("Price $") %>%
      formatCurrency("Cost $") %>%
      formatPercentage("Probability %", digits = 1) %>%
      formatPercentage("Win %", digits = 1)
      )


      shinyApp(ui = ui, server = server)


      However, what I wish to do is, within the renderDataTable() function, to simplify the format functions into fewer lines. For example, implement formatCurrency() in any column with a "$" and formatPercentage() in any column with a "%".



      I have done a fair bit of searching for an appropriate but could not find a solution, but I assume I am just missing a fairly simple solution.



      Something like:



      # This will create an output summary table
      output$data_table_example = renderDataTable(
      result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
      class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency(grepl("$", colnames()) %>%
      formatPercentage(grepl("%", colnames()), digits = 1)
      )


      A few additional points:



      • The tibble will actually be a reactive

      • This example is a very trivial version of a rather more complex table and set of reactives

      • I do not want to implement the formatting in the reactive part since I find this then messes with the DT sorting function, since it assumes the column is a character string

      Any help will be greatly appreciated







      r shiny shinydashboard dt






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 12 hours ago









      FitzKaos

      4211




      4211






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try:



          # This will create an output summary table
          output$data_table_example = renderDataTable(
          result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
          class = 'cell-border stripe compact', rownames = FALSE) %>%
          formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
          formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
          )


          It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data).



          I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. grep seems to not have this issue.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224060%2fr-dtdatatables-formatting-multiple-columns-simultaneously%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Try:



            # This will create an output summary table
            output$data_table_example = renderDataTable(
            result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
            class = 'cell-border stripe compact', rownames = FALSE) %>%
            formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
            formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
            )


            It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data).



            I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. grep seems to not have this issue.






            share|improve this answer


























              up vote
              1
              down vote



              accepted










              Try:



              # This will create an output summary table
              output$data_table_example = renderDataTable(
              result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
              class = 'cell-border stripe compact', rownames = FALSE) %>%
              formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
              formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
              )


              It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data).



              I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. grep seems to not have this issue.






              share|improve this answer
























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Try:



                # This will create an output summary table
                output$data_table_example = renderDataTable(
                result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
                class = 'cell-border stripe compact', rownames = FALSE) %>%
                formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
                formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
                )


                It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data).



                I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. grep seems to not have this issue.






                share|improve this answer














                Try:



                # This will create an output summary table
                output$data_table_example = renderDataTable(
                result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE),
                class = 'cell-border stripe compact', rownames = FALSE) %>%
                formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
                formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
                )


                It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data).



                I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. grep seems to not have this issue.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 8 hours ago

























                answered 9 hours ago









                Eli Berkow

                40528




                40528



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224060%2fr-dtdatatables-formatting-multiple-columns-simultaneously%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo