2016-10-01 24 views
1

一个例子的代码:如何在R闪亮的UI元素附近打印错误消息?

ui.R

library(shiny) 

shinyUI(
    fluidRow(column(2, actionButton("add", "ADD details")), 
      fluidRow(uiOutput("ui")) 
) 
) 

server.R

shinyServer(function(input, output,session){ 
    observeEvent(
    input$add, 
    output$ui <- renderUI({ 
     isolate({ 
     fluidRow(column(4, textInput("birthweight", label = "birth weight:", value = '')), 
       column(3, numericInput("height",label = "Height:",value='')), 
       column(2, actionButton("addnew", "ADD details to database")) 
     ) 
     }) 
    }) 
) 
}) 

当用户输入的输入birthweight应显示附近,如果它的textInput盒的错误消息包含字符数据,类似于numericInputHeight。用户输入数据后,或者用户单击数据库操作按钮的添加详细信息时,必须立即显示该信息,但应该显示为文本框附近的错误消息,而不是弹出窗口。

这可以在R闪亮吗?

+0

您是否尝试过的http:/ /shiny.rstudio.com/articles/validation.html? –

回答

2

validate功能是一种选择。另一种选择是只有在textInput具有非数字值时才使用额外的UI来显示错误消息。在这种情况下,您可以将您自己的CCS添加到错误消息中。这里是一个基于你的代码的例子。

library(shiny)  
ui <-shinyUI( 
    fluidRow( 
    column(2, 
     actionButton("add", "ADD details")), 
    fluidRow(uiOutput("ui")) 
) 
)  

server <- shinyServer(function(input, output,session){ 
    observeEvent(input$add, 
    output$ui <- renderUI({ 
     isolate({ 
     fluidRow( 
      column(4, 
      textInput("birthweight", label = "birth weight:", value = ''), 
      uiOutput("checkBirthweight")), 
      column(3, 
      numericInput("height",label = "Height:",value='')), 
      column(2, 
      actionButton("addnew", "ADD details to database")) 
     ) 
     }) 
    }) 
) 

    output$checkBirthweight <- renderUI({ 
    if (nchar(input$birthweight) > 0 && is.na(as.numeric(input$birthweight))) 
     p("Error: birth weight must be numeric") 
    }) 

}) 

shinyApp(ui, server) 

顺便说一句,这是把你的代码在你的问题的代码好主意,它会帮助别人找出问题所在。您可以找到有关这些额外的帮助,在https://stackoverflow.com/editing-help

另外,我知道每个人都有自己的代码风格,我尊重这一点,但我发现这些准则R中非常有用的译码https://google.github.io/styleguide/Rguide.xml