简体   繁体   English

Shiny中的条件语句不显示值

[英]Conditional Statement in Shiny not displaying values

I have coded a shiny app. 我已经编写了一个闪亮的应用程序。 It have user input dataset and user input dependent variable .It predicts the dependent variable. 它具有用户输入数据集和用户输入因变量。它预测因变量。 Dependent variable is stored in input$text 因变量存储在input $ text中

In UI.R : 在UI.R中:

textOutput('contents2')

In server.RI have mentioned a conditional statement where if the dependent variable is factor , it will predict class levels, otherwise continuous values: 在server.RI中提到了一个条件语句,其中如果因变量是factor,它将预测类级别,否则将预测连续值:

      output$contents2 <- renderText({
        if(class(input$text)=="factor"){
            predict(modelc(), newdata=testdata(),type="class")}

        if(class(input$text)=="numeric"){
                predict(model(), newdata=testdata())  

        }
   })

But its not displaying predicted values. 但是它没有显示预测值。 I was wondering what might be missing. 我想知道可能会丢失什么。 Thanks 谢谢

Nothing gets printed because no value is returned by renderText , you can easily fix it by wrapping predict(...) into return function. 由于renderText不返回任何值,因此不会打印任何renderText ,您可以通过将renderText predict(...)包装到return函数中来轻松修复它。

However, there is another bug. 但是,还有另一个错误。 Since input$text is a character string its class is character and your logical comparison doesn't do what you want. 由于input$text是一个字符串,因此它的类是character而您的逻辑比较不会执行您想要的操作。 You can fix it, first by subsetting testdata() with [[ operator which gives you a vector and then checking its class. 您可以通过使用[[运算符为您提供向量的子运算符]子集testdata()来进行修复,然后检查其类。

You also have to make sure that the name of inputed variable is indeed a valid variable - as usual with req function (or validate and need ) 您还必须确保输入变量的名称确实是一个有效变量-与req函数一样(或validateneed


Full example: 完整示例:

output$contents2 <- renderText({

  req(input$text %in% names(testdata() ))
  test <- class(testdata()[[input$text]])

  if (test == "factor") {
    return(predict(modelc(), newdata = testdata(), type = "class") )
  }
  if (test == "numeric") {
    return(predict(model(), newdata = testdata()) )
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM