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

Add number_extra() for numeric inputs in reactable #26

Closed
wants to merge 1 commit into from
Closed

Add number_extra() for numeric inputs in reactable #26

wants to merge 1 commit into from

Conversation

Johan-rosa
Copy link

Have you read the Contributing Guidelines?

Issue #25

Changes description

  1. number_extra function and its js wrapper numberExtras were added

How to test

Run this example and play with the numbers

library(shiny)
library(reactable)
library(reactable.extras)

string_list <- function(values) {
  paste0(
    "{", paste0(names(values), " : ", unlist(values), collapse = ", "), "}"
  )}

ui <- fluidPage(
  reactable_extras_dependency(),
  reactableOutput("react"),
  textOutput("number_text"),
  textOutput("text")
)

server <- function(input, output, session) {
  output$react <- renderReactable({
    tibble::tibble(
      name = c("Portfolio 1", "Portfolio 2", "Portfolio 3"),
      expected_return = c(0.0687, 0.060, 0.050) * 100,
      expected_volatility = c(0.1575, 0.140, 0.120) * 100
    ) |>
      reactable(
        columns = list(
          name = colDef(cell = text_extra("text")),
          expected_return = colDef(cell = number_extra("number", class = "number-extra"))
        )
      )
  })
  output$text <- renderText({
    req(input$text)
    values <- input$text
    paste0(
      "Text: ",
      string_list(values)
    )
  })

  output$number_text <- renderText({
    req(input$number)
    values <- input$number
    paste0(
      "Text: ",
      string_list(values)
    )
  })
}

shinyApp(ui, server)

number_extra function and its js wrapper numberExtras were added
@Johan-rosa Johan-rosa closed this by deleting the head repository Nov 4, 2023
@prasan2421
Copy link

I cannot see number_extra() working yet. Is this function still yet to be released?

@Andryas
Copy link

Andryas commented Dec 11, 2024

When this feature will be released?

@Johan-rosa
Copy link
Author

I see now that the PR is close since I deleted the fork from my profile. @jakubnowicki It looks like a needed feature.

cc: @vibalre

@Johan-rosa
Copy link
Author

@Andryas this can be a workaround, I added a event listener to format the input values :

library(shiny)
library(reactable)
library(reactable.extras)

ui <- fluidPage(
  reactable_extras_dependency(),
  reactableOutput("table", width = 400)
)

server <- function(input, output, session) {
  output$table <- renderReactable({
    print(input$salary)
    
    data.frame(
      name = c("Johan Rosa", "Oscar Pascual"),
      salary = c(NA, NA)
    ) |>
      reactable(
        columns = list(
          salary = colDef(cell = text_extra("salary", class = "salary-input"))
        )
      ) |>
      htmlwidgets::onRender(
        "
          $('.salary-input').each(function() {
            $(this).on('input', function() {
                // Remove letters and commas, then format with commas
                const newValue = $(this).val().replace(/[^\\d]/g, '');
                $(this).val(formatNumberWithComma(newValue));
            });
          });

          const formatNumberWithComma = (number) => {
              return number.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');
          };
        "
      )
  })
}

shinyApp(ui, server)

@Andryas
Copy link

Andryas commented Dec 16, 2024

Thank you @Johan-rosa, nice workaround.

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

Successfully merging this pull request may close these issues.

3 participants