Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3475
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 2, 20222022-06-02T16:27:57+00:00 2022-06-02T16:27:57+00:00

r – How to add warnings to UI outputs generated dynamically in Shiny

  • 0

[ad_1]

I am working on a shiny app that can generate a determined number of UI outputs in form of inputs based on a value defined by the user. Thanks to the help of @YBS I was able to get the app working. But now I face a new issue. Although I could define min and max value for the inputs generated, I would like to add a warning in the inputs when a value is greater than 100, I found shinyfeedback package can do this but I do not where to put properly the code or what to do in the case of dinamyc inputs like the ones generated here. This is the working app:

library(shiny)
library(shinydashboard)
library(DT)
library(shinyFeedback)
#Function

compute <- function(firstitem,seconditem)
{
  Sum <- firstitem+seconditem
  Difference <- firstitem+seconditem
  Product <- firstitem*seconditem
  Ratio <- firstitem/seconditem
  Res <- data.frame(C1=Sum,C2=Difference,C3=Product,C4=Ratio)
  return(Res)
}

#App

ui = shinyUI(fluidPage(
  
  titlePanel("Compare"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("numitems", label = "Number of items to compare?",
                   min = 1, max = 100, value = 1),
      uiOutput("period_cutpoints"),
      uiOutput("period_cutpoints2"),
      actionButton("submit", "Submit")
    ),
    mainPanel(
      uiOutput("t1")
    )
  )
))

server = shinyServer(function(input, output, session) {
  
  output$period_cutpoints<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("firstitem",i), 
                   label=paste0("Enter the value of first item ", i, ":"),value = i)
    })
    
  })
  
  output$period_cutpoints2<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("seconditem",i), 
                   label=paste0("Enter the value of second item ", i, ":"),value = i+i)
    })
  })
  
  seldates <- reactiveValues(x=NULL)
  observeEvent(input$submit, {
    seldates$x <- list()
    lapply(1:(input$numitems), function(i) { 
      seldates$x[[i]] <- compute(firstitem = input[[paste0("firstitem", i)]],seconditem = input[[paste0("seconditem", i)]])
    })
  })
  
  observeEvent(input$submit, {
    
    lapply(1:(input$numitems), function(i) { 
      output[[paste0("table",i)]] <- renderDT(seldates$x[[i]])
    })
    
    output$t1 <- renderUI({
      tagList(
        lapply(1:(input$numitems), function(i) { 
          DTOutput(paste0("table",i))
        })
      )
    })
    
  })
  
})

shinyApp(ui = ui , server = server)

I tried to add some code inside the dynamic inputs in this way:

#Code demo
  output$period_cutpoints<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("firstitem",i), 
                   label=paste0("Enter the value of first item ", i, ":"),value = i)
    })
    
    lapply(1:(input$numitems), function(i) {
      observeEvent(input[[paste0('firstitem',i)]], {
        shinyFeedback::feedbackWarning(
          inputId = paste0('firstitem',i),
          show = input[[paste0('firstitem',i)]] > 100,
          text = "Number less than 100 required.",
          color="red"
        )
      })
    })
    
  })

Unfortunately, this action broke down the app:

enter image description here

And the first input was not generated as you can see.

How can I solve this issue so that I can have warnings when the value is greater than 100? Moreover, this leads to an additional fact, in the action button if working with multiple inputs generated dynamically, how could I do something like this:

#How to extend the if condition so that it can consider the number of inputs defined by the user
observeEvent(input$submit,
             {
               if(input$firstitem1 < 0 && input$seconditem1 < 0 && input$firstitem2<0 && input$seconditem1<0)
               {
                 showModal(modalDialog(title ="Warning!!!", "Check fields!!!",easyClose = T))
               }
               else
               {
                 showModal(modalDialog(title ="Congratulations!!!", "Computing Done!!!",easyClose = T))
                 
               }
             })

How could I change the if so that it considers all the inputs that can be generated.

Many thanks!

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8038 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1840 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1920 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.