Joe
Joe

Reputation: 1768

Show instant notification if number input does not fulfill condition

I have a textInput in which the user should enter a number, for which I check if it is greater than 0 (the reason why I am using textInput instead of numInput is that I do not like the arrows coming with the latter). The number is used for further calculations. My goal is to show the user an instant notification if the input number is less than or equal to 0. By "instant" I mean once the user clicks outside the input field. I tried the following, but no notification is shown whatever the input:

library(shiny)

ui <- fluidPage(textInput("num_ipt", "enter positive number"))

server <- function(input, output, session) {

  pos_num <- reactive({
    validate(need(as.numeric(input$num_ipt) > 0), "Enter positive number")
      as.numeric(input$num_ipt)})}

shinyApp(ui, server)

EDIT:

@Imran Ali, did you mean this?

library(shiny)

ui <- fluidPage(textInput("num_ipt", "enter positive number"),
                verbatimTextOutput("numb"))

server <- function(input, output, session) {

  pos_num <- reactive({
    validate(need(as.numeric(input$num_ipt) > 0), "Enter positive number")
    as.numeric(input$num_ipt)})
  output$numb <- renderText(pos_num())

  }

shinyApp(ui, server)

This does not work. Instead of the number an error is shown in the output panel: "Error: argument 'label' is missing, with no default". Besides that, the number should not be outputted at all. In my real app it is only needed as an input for calculations.

2. EDIT: @Stephane, your first snippet goes in the right direction, but it is not quite what I need as it only works if I output pos_num. If I remove the output$numb, it does not work:

library(shiny)

ui <- fluidPage(textInput("num_ipt", "enter positive number"))

server <- function(input, output, session) {

  pos_num <- reactive({
    validate(need(as.numeric(input$num_ipt) > 0, "Enter positive number"))
    as.numeric(input$num_ipt)})}

shinyApp(ui, server)

Upvotes: 0

Views: 206

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84719

That does not work because the message must be an argument of need, while it is an argument of validate in your code.

library(shiny)

ui <- fluidPage(textInput("num_ipt", "enter positive number"),
                verbatimTextOutput("numb"))

server <- function(input, output, session) {

  pos_num <- reactive({
    validate(need(as.numeric(input$num_ipt) > 0, "Enter positive number"))
    as.numeric(input$num_ipt)
  })

  output$numb <- renderText(pos_num())

}

shinyApp(ui, server)

FYI, below is a way to print such a message with shinyFeedback:

library(shiny)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(),
  textInput("num_ipt", "enter positive number", value="1"),
  verbatimTextOutput("numb")
)

server <- function(input, output, session) {

  observeEvent(input$num_ipt, {
    feedbackWarning(
      inputId = "num_ipt",
      condition = is.na(as.numeric(input$num_ipt)) || !(as.numeric(input$num_ipt) > 0),
      text = "Enter a positive number !"
    )
  })

  pos_num <- reactive({
    validate(need(as.numeric(input$num_ipt) > 0, message=FALSE))
    as.numeric(input$num_ipt)
  })

  output$numb <- renderText(pos_num())

}

shinyApp(ui, server)

Upvotes: 1

Related Questions