How to make object created within function usable outside

前端 未结 4 697
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 23:29

I created a function which produces a matrix as a result, but I can\'t figure out how to make the output of this function usable outside of the function environment, so that

4条回答
  •  醉酒成梦
    2020-12-13 00:05

    Can't you just use <<- to assign it the object to the workspace? The following code works for me and saves the amort_value object.

    amortization <- function(cost, downpayment, interest, term) {
      amort_value <<- (cost)*(1-downpayment/100)*(interest/1200)*((1+interest/1200)^(term*12))/((1+interest/1200)^(term*12)-1)
      sprintf("$%.2f", amort_value)         
    
    }
    amortization(445000,20,3,15)
    amort_value
    

提交回复
热议问题