【问题标题】:Shiny/R: Hide error message when no value in selectizeInputShiny/R:当 selectizeInput 中没有值时隐藏错误消息
【发布时间】:2017-07-01 20:03:36
【问题描述】:

我有一个关于闪亮应用的问题。当数字输入和 selectizeInput 中没有值选择时,我的闪亮应用程序将由于空数据框而显示错误。如果用户尚未选择他们的输入,我想隐藏错误消息。我知道if return 会有所帮助,但它似乎在这个应用程序中不起作用。

server.r:

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

result<-reactive({
  if(is.null(input$wt)||is.null(input$hdcount)||is.null(input$season)||is.null(inp ut$gender) )return(NULL)

 mod1<-lm(deathLog ~ InHdCnt+ log(InHdCnt) + season+ SexCode+ AvgArrivWt,  data=mydata)
 newdata = data.frame(AvgArrivWt=input$wt,InHdCnt=input$hdcount,SexCode=input$gender,season=input$season)
data<-predict(mod1, newdata, interval="predict",level=(input$slider1)*0.01 ) 
data

})

output$distPlot <- renderPrint({

result()
})

})

ui.r:

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

# Application title
titlePanel("Death Loss Estimator with On Arrival Factors"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
   numericInput("wt", label = h4("Average Arrival Weight  input"),value="NULL"),
   numericInput("hdcount", label = h4("Arrival Head Count input"),value="NULL"),
   selectizeInput(
     'season', h4('Arrival Season'), choices = c("spring", "summer","fall", "winter"),
     options = list(
       placeholder = 'Please select a season below',
       onInitialize = I('function() { this.setValue(""); }')
     )
   ),
   selectizeInput(
     'gender', h4('Arrival Sex'), choices = c("HOL", "FEM","MAL", "MIX"),
     options = list(
       placeholder = 'Please select a season below',
       onInitialize = I('function() { this.setValue(""); }')
     )
   ),
   sliderInput("slider1", label = h4("Confidence Interval Level"), min = 50, 
               max = 100, value = 80)
   ),

  # Show a plot of the generated distribution
  mainPanel(
   textOutput("distPlot")
  )
  )
 ))

谢谢!

【问题讨论】:

  • data=mydata 没有帮助。您能否编辑帖子并使代码可重现
  • if(is.null(input$) | is.null(input$) | ... ){ return() }
  • ?req 可能会有所帮助。
  • 谢谢大家! validate() 函数将解决这个问题。仍然非常感谢所有回复!

标签: r validation input shiny


【解决方案1】:

我建议使用验证和需要。您可以将其放在反应式表达式的顶部:

validate(
   need(input$wt, "Please select a weight"),
   need(input$hdcount, "Please select a head count")
)

您也可以使用 req:

req(input$wt)
req(input$hdcount) 

【讨论】:

  • 这很棒。谢谢拉德。
猜你喜欢
  • 2016-03-30
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
相关资源
最近更新 更多