1
votes

I need help with the below shiny app server function. My problem is values$npv always comes out null, not even with a 0. and I think the fun function is not doing the right thing and i'm out of ideas. If I hard-code the renderText with paste("Net Present Value:", isolate(input$val_inv)) i always have a result but not what i want and this makes me guess the fun function is not working as it should.

     inline_numericInput=function(ni){
  tags$div( class="form-inline",ni)
}

ui <- shinyUI(fluidPage(
  tags$head(tags$style("#side_panel{
                       padding-left:10px;
                       }
                       .form-group {
                       margin-bottom: 15px !important;
                       }
                       .form-inline .form-control {
                       width:80%;
                       }
                label{ width:30px;}

                       ")),

  titlePanel("Example"),

  sidebarLayout(

    sidebarPanel(width = 4,id="side_panel",
                 fluidRow(
                   column(6, inline_numericInput(numericInput("val_inv", label = "Inv:", value = 0))),
                 ),
                 fluidRow(
                   column(6, inline_numericInput(numericInput("val_r", label = "R:", value = 0))),
                 ),
                 fluidRow(
                   column(6, inline_numericInput(numericInput("val_n", label = "N:", min = 50,value = 50))),
                   column(6, inline_numericInput(actionButton("btn_calcnpv", label = "Compute NPV")))
                 )

    ),

    mainPanel(
      p('Results:'),
    textOutput("val_npv")
    )
  )
))

server <- function(input, output) {

    values <- reactiveValues()
    values$npv <- 0

    observe({
        input$btn_calcnpv
        fun <- function(n){
            cf <- 0
            for (i in 1:n){
               cf <- cf + isolate(input$val_inv)/(1+input$var_r)**i
            }
            cf
        }
        values$npv <- fun(isolate(input$val_n))- isolate(input$val_inv)
        #values$npv <- values$npv - isolate(input$val_inv)
    })

    output$val_npv <- renderText({
        if(input$btn_calcnpv)
            paste("Net Present Value:", values$npv)
        else ""
    })

}

shinyApp(ui, server) 
1
Please, provide a working example. - R learner
In addition to the previous comment, see here to know how to make a reproducible example - bretauv
I have give the entire code. - oak
Please, check the names of your input variables: val_r versus var_r. Furthermore, I suppose that input variables are characters. Maybe, you have to coerce them to numbers. - R learner

1 Answers

0
votes

Here is an answer using eventReactive and not so many isolations. Furthermore, the inputs are coerced to numbers before the calculation takes place. Using eventReactive, the calculation is started by pressing the compute-button.

server <- function(input, output) {


  npv <- eventReactive(input$btn_calcnpv, {

    val_inv <- as.numeric(input$val_inv)
    val_r <- as.numeric(input$val_r)
    val_n <- as.numeric(input$val_n)
    fun <- function(n){
      cf <- 0
      for (i in 1:n){
        cf <- cf + val_inv/(1+val_r)**i
      }
      cf
    }
    temp <- fun(val_n)- val_inv
    temp
  })

  output$val_npv <- renderText({
      req(npv())
      paste("Net Present Value:", npv())

  })

}