0
votes

My shiny application doesn't work and I have no idea why : This tries to get an input and calculate the output and render the result in output$mortgagepayment. when I do run app it gives me the following message:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

library(shiny)

# Define UI ----
ui <- fluidPage(
    titlePanel("Basic widgets"),

     fluidRow(

         # column(3,
         #       h3("Buttons"),
         #        actionButton("action", "Action"),
         #        br(),
         #        br(),
         #        submitButton("Submit")),

         # column(3,
         #        h3("Single checkbox"),
         #        checkboxInput("checkbox", "Choice A", value = TRUE)),

         column(3,
                checkboxGroupInput("Fixed",
                                   h3("Mortgage Year"),
                                   choices = list("30 year" = 30,
                                                  "15 year" = 15,
                                                  "10 year" = 10),
                                   selected = 1)),

         # column(3,
         #       dateInput("date",
         #                 h3("Date input"),
         #                  value = "2014-01-01"))
     ),

    fluidRow(

        # column(3,
        #        dateRangeInput("dates", h3("Date range"))),
        # 
        # column(3,
        #        fileInput("file", h3("File input"))),



        column(3, 
               numericInput("housePrice", 
                            h3("Housing Price"), 
                            value = 1)),   


        column(3, 
               numericInput("percentageDown", 
                            h3("Percentage Down"), 
                            value = 1),min=0,max=1) ,  

        column(3, numericInput("mortgageYield", h3("Mortgage Yield"), value=0.0, min = 0, max = 0.1, step = NA,
                              width = NULL)),
        ),

    fluidRow(
        column(3, h3("Mortgage Payment"), verbatimTextOutput("mortgagepayment")))
)



# Define server logic ----
server <- function(input, output) {
   housePrice = input$housePrice
   downPayment = input$percentageDown    
   mortgageYield = input$mortgageYield 
   mortgageAmount = housePrice*(1-downPayment)
   years = session$fixed
   mortgagePayment = (mortgageAmount*mortgageYield)/(1-1/(1+mortgageYield)^(12*years))
   output$mortgagepayment <-renderText({mortgagePayment})
   }

# Run the app ----
shinyApp(ui = ui, server = server)
1

1 Answers

1
votes

There are several things that need to be corrected.

  1. All references to input$... variables must be within reactive components; in this case, it might go within renderText(...).

  2. session$fixed is wrong, you probably mean input$fixed.

  3. you declare checkboxGroupInput("Fixed",...) (upper-case "F") but reference ...$fixed (lower-case "f"), R is case sensitive.

  4. you try to use the value of the checkbox directly, but checkbox values are strings; you need to use years <- as.numeric(input$fixed) (assuming you continue with the trend of naming the checkbox selection meaningful numbers).

Your current defaults drive to a payment of 0 (namely, percentageDown=1 means that the full amount is a down-payment, so nothing-owed).

I think your server component should be something like this:

server <- function(input, output) {
  output$mortgagepayment <- renderText({
    # prevent this block from trying to calculate when other fields are
    # empty or invalid
    req(input$housePrice, input$percentageDown, input$mortgageYield, input$Fixed)
    # message("calculate!") # just advises when this block fires
    housePrice = input$housePrice
    downPayment = input$percentageDown    
    mortgageYield = input$mortgageYield 
    mortgageAmount = housePrice*(1-downPayment)
    years = as.numeric(input$Fixed)
    mortgagePayment = (mortgageAmount*mortgageYield)/(1-1/(1+mortgageYield)^(12*years))
    mortgagePayment
  })
}