r shiny在numericInput为空时防止应用程序崩溃

时间:2018-02-09 15:57:59

标签: r shiny numeric-input

我有一个非常简单的Shiny应用程序正在运行。用户在框1上输入一个值,然后在框2中输入,然后点击“运行”。

但是,当用户在两个numericInput单元格之一中点击退格键,直到没有输入任何内容时,应用程序崩溃。

我怎样才能防止它崩溃?非常感谢你!

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("run", label = "Run calculations!"),
  # Input 1
  numericInput("inNumber1", label = "Input number 1", 
               value = 100, min = 10, max = 200, step = 10),
  # Message that shows up if the value of inNumber1 is outside the allowed range:
  hidden(h5("Enter a value between 10 and 200!",
            id = "message1",
            style = "font-weight:bold;color:orange;")),

  uiOutput("inNumber2"),
  # Message that shows up if the value of inNumber1 is outside the allowed range:
  hidden(h5("Enter a value between 10 and the value of Input number 1!",
            id = "message2",
            style = "font-weight:bold;color:orange;")),

  textOutput("out1"),
  textOutput("out2")
)

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

  # Input 2 - dependent on Input 1
  observeEvent(input$inNumber1, {

    # Is input$inNumber1 outside the allowed range?
    if (input$inNumber1 < 10 | input$inNumber1 > 200) {
      shinyjs::show("message1")
    } else {
      shinyjs::hide("message1")
    }
    output$inNumber2 <- renderUI({
      if (is.null(input$inNumber1)) return(NULL)
      numericInput("inNumber2", label = "Input number 2", 
                   value = 100, min = 10, max = input$inNumber1, step = 10)
    })
  })

  observeEvent(input$inNumber2, {

    # Is input$inNumber2 outside the allowed range?
    if (input$inNumber2 < 10 | input$inNumber2 > input$inNumber1) {
      shinyjs::show("message2")
    } else {
      shinyjs::hide("message2")
    }

  })

  observeEvent(input$run, {

    # Check Input 1:
    number1 <- input$inNumber1
    if (number1 < 10 | number1 > 200) {
      warning1 <- "Input 1 must be between 10 and 200"
    } else {warning1 <- renderText({NULL})}

    output$out1 <- renderText({number1})

    number2 <- input$inNumber2  # value 2
    maxnumber2 <- input$inNumber1
    if (number2 > maxnumber2) {
      # warning(paste0("number2 must be lower than ", maxnumber2))
      number2 <- maxnumber2
    }
    output$out2 <- renderText({number2})


  })
}

shinyApp(ui, server)

非常感谢!

1 个答案:

答案 0 :(得分:5)

对不起,那是一个小小的翻转,匆匆忙忙。看看下面的代码,我认为现在工作正常。

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("run", label = "Run calculations!"),
  # Input 1
  numericInput("inNumber1", label = "Input number 1", 
               value = 100, min = 10, max = 200, step = 10),
  # Message that shows up if the value of inNumber1 is outside the allowed range:
  hidden(h5("Enter a value between 10 and 200!",
            id = "message1",
            style = "font-weight:bold;color:orange;")),

  uiOutput("inNumber2"),
  # Message that shows up if the value of inNumber1 is outside the allowed range:
  hidden(h5("Enter a value between 10 and the value of Input number 1!",
            id = "message2",
            style = "font-weight:bold;color:orange;")),

  textOutput("out1"),
  textOutput("out2")
)

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

  # Input 2 - dependent on Input 1
  observeEvent(input$inNumber1, {

    req(input$inNumber1)

    # Is input$inNumber1 outside the allowed range?
    if (input$inNumber1 < 10 | input$inNumber1 > 200) {
      shinyjs::show("message1")
    } else {
      shinyjs::hide("message1")
    }
    output$inNumber2 <- renderUI({
      if (is.null(input$inNumber1)) return(NULL)
      numericInput("inNumber2", label = "Input number 2", 
                   value = 100, min = 10, max = input$inNumber1, step = 10)
    })
  })

  observeEvent(input$inNumber2, {

    req(input$inNumber1, input$inNumber2)

    # Is input$inNumber2 outside the allowed range?
    if (input$inNumber2 < 10 | input$inNumber2 > input$inNumber1) {
      shinyjs::show("message2")
    } else {
      shinyjs::hide("message2")
    }

  })

  observeEvent(input$run, {

    req(input$inNumber1, input$inNumber2)

    # Check Input 1:
    number1 <- input$inNumber1
    if (number1 < 10 | number1 > 200) {
      warning1 <- "Input 1 must be between 10 and 200"
    } else {warning1 <- renderText({NULL})}

    output$out1 <- renderText({number1})

    number2 <- input$inNumber2  # value 2
    maxnumber2 <- input$inNumber1
    if (number2 > maxnumber2) {
      # warning(paste0("number2 must be lower than ", maxnumber2))
      number2 <- maxnumber2
    }
    output$out2 <- renderText({number2})


  })
}

shinyApp(ui, server)