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

Conflict with shinyFeedback #711

Open
Aspirational-Human opened this issue Oct 22, 2024 · 1 comment
Open

Conflict with shinyFeedback #711

Aspirational-Human opened this issue Oct 22, 2024 · 1 comment

Comments

@Aspirational-Human
Copy link

Hello,

I greatly appreciate the autonumericInput function's ability to format numeric inputs as currency. However, no matter what condition I set, I cannot make this input compatible with shinyFeedback to alert users when they have left a particular field empty. See my example below showing how autonumericInput is uniquely incompatible with shinyFeedback.

library(shiny)
library(bslib)
library(shinyFeedback)
library(shinyWidgets)

ui <- page_fluid(
  useShinyFeedback(),
  
  card(
    card_header("Input Form"),
    textInput("text_input", "Text Input"),
    numericInput("numeric_input", "Numeric Input", value = NA),
    autonumericInput(
      "autonumeric_input",
      "Autonumeric Input",
      value = NULL,
      align = "right",
      decimalPlaces = 2
    ),
    actionButton("submit", "Submit")
  )
  )

server <- function(input, output, session) {
  observeEvent(input$submit, {
    # Check text input
    if (is.null(input$text_input) || input$text_input == "") {
      showFeedbackDanger("text_input", "Please enter some text")
    } else {
      hideFeedback("text_input")
    }
    
    # Check numeric input
    if (is.na(input$numeric_input)) {
      showFeedbackDanger("numeric_input", "Please enter a number")
    } else {
      hideFeedback("numeric_input")
    }
    
    # Check autonumeric input
    if (is.null(input$autonumeric_input) || input$autonumeric_input == "" || is.na(input$autonumeric_input)) {
      showFeedbackDanger("autonumeric_input", "Please enter a number")
    } else {
      hideFeedback("autonumeric_input")
    }
  })
}

shinyApp(ui, server)
@pvictor
Copy link
Member

pvictor commented Oct 23, 2024

Hello,

shinyFeedback's support for autonumericInput() is available in the dev version, install package from GitHub and it will work:

remotes::install_github("merlinoa/shinyFeedback")

For an immediate CRAN available solution, you can try package shinyvalidate:

library(shiny)
library(bslib)
library(shinyvalidate)
library(shinyWidgets)

ui <- page_fluid(
  useShinyFeedback(),
  
  card(
    card_header("Input Form"),
    textInput("text_input", "Text Input"),
    numericInput("numeric_input", "Numeric Input", value = NA),
    autonumericInput(
      "autonumeric_input",
      "Autonumeric Input",
      value = NULL,
      align = "right",
      decimalPlaces = 2
    )
  )
)

server <- function(input, output, session) {
  
  # 1. Create an InputValidator object
  iv <- InputValidator$new()
  
  # 2. Add validation rules
  iv$add_rule("text_input", sv_required())
  iv$add_rule("numeric_input", sv_required())
  iv$add_rule("autonumeric_input", sv_required())
  
  # 3. Start displaying errors in the UI
  iv$enable()
  
}

shinyApp(ui, server)

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