简体   繁体   中英

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

In 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:

      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.

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. You can fix it, first by subsetting testdata() with [[ operator which gives you a vector and then checking its class.

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 )


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()) )
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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