[英]Conditional Statement in Shiny not displaying values
我已經編寫了一個閃亮的應用程序。 它具有用戶輸入數據集和用戶輸入因變量。它預測因變量。 因變量存儲在input $ text中
在UI.R中:
textOutput('contents2')
在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())
}
})
但是它沒有顯示預測值。 我想知道可能會丟失什么。 謝謝
由於renderText
不返回任何值,因此不會打印任何renderText
,您可以通過將renderText
predict(...)
包裝到return
函數中來輕松修復它。
但是,還有另一個錯誤。 由於input$text
是一個字符串,因此它的類是character
而您的邏輯比較不會執行您想要的操作。 您可以通過使用[[
運算符為您提供向量的子運算符]子集testdata()
來進行修復,然后檢查其類。
您還必須確保輸入變量的名稱確實是一個有效變量-與req
函數一樣(或validate
並need
)
完整示例:
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.