Joe Crozier
Joe Crozier

Reputation: 1036

R, shiny, prevent error until field is calculated

I am building a shiny application, and I would like there to be a field like this that displays the probability someone will return (given a bunch of underlying models):

enter image description here

And it pretty much works, except its in decimal form: enter image description here

The code pasting into that box looks like this:

paste(a$result)

I can get it to look "correct" and say '83%' for instance, instead of 0.83000.....

by using this code:

paste(  c( round(a$result, digits=2))*100,"%")

But the problem is.... while this code does work, until you hit the "calculate" button, it looks like this:

enter image description here

I wish I could provide some sample data to try but given the interactiveness of the shiny app, that'd be very hard. Is there a simple solution?

Upvotes: 0

Views: 37

Answers (1)

MrFlick
MrFlick

Reputation: 206187

Use the function shiny::req to make sure required values are present before performing the calculation. For example

paste(c( round(req(a$result), digits=2))*100,"%")

Upvotes: 3

Related Questions