Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR updates an R bond pricing interactive application by refactoring code organization and adding Shiny app deployment capabilities. The changes modernize the existing manipulate-based interface while maintaining the core bond pricing functionality and adding a web-deployable version.
Key changes:
- Refactors the main bond plotting logic into a separate modular file (
bondPlot.R) - Adds dynamic working directory detection for better script portability
- Creates a new Shiny app interface (
app.R) for web deployment - Updates documentation with new R version requirements and deployment links
Reviewed Changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| run_tests.R | Adds dynamic working directory detection function |
| interactive_bonds.R | Refactors to use extracted bondPlot function and adds working directory detection |
| bondPlot.R | New file containing extracted and improved bond plotting logic with better text positioning |
| app.R | New Shiny application providing web interface for the bond pricing tool |
| README.md | Updates documentation with new R version, deployment link, and usage instructions |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
interactive_bonds.R
Outdated
| # 19 February 2015 | ||
| library(manipulate) | ||
| source("helpers/helpers.R") | ||
|
|
||
| bondP=function(c,n,f,r,rN){ | ||
| # c = bond coupon in % | ||
| # n = time to maturity in years | ||
| # f = frequency of payments per year | ||
| # r = current interest rate | ||
| # rN = new interest rate (to see convexity effect) | ||
|
|
||
| # Plot bond price as a function of yield | ||
| yields <- seq(0.02,0.5,by=0.01) | ||
| nominal_price <- bond_price(c,n,f,yields) | ||
| op <- par(mar = c(6,4,4.5,4) + 0.5) | ||
| plot(yields,nominal_price,type="l",xlim=c(0,0.5),axes = F,xlab = NA,ylab = NA) | ||
| title(main="Bond price",line=4) | ||
|
|
||
|
|
||
| # calculate the gradient for tanget line | ||
| myenv <- new.env() | ||
| assign("r", r, envir = myenv) | ||
| drv <- numericDeriv(quote(bond_price(c,n,f,r)),c("r"),myenv) | ||
|
|
||
| # draw tangent line | ||
| abline(a=(c(attr(drv,"gradient")*(0-r))+drv[1]),b=attr(drv,"gradient"),col="green") | ||
|
|
||
| points(r,bond_price(c,n,f,r),col="red",pch=19) | ||
| lines(c(r,r),c(0,bond_price(c,n,f,r)), col = "red",lty=3,lwd=2) | ||
| lines(c(0,r),c(bond_price(c,n,f,r),bond_price(c,n,f,r)), col = | ||
| "red",lty=3,lwd=2) | ||
|
|
||
| # new interest rate to see the convexity effect (area in yellow) | ||
| polygon(x=c(seq(r,rN,length.out=10),seq(rN,r,length.out=10)), | ||
| y=c(bond_price(c,n,f,seq(r,rN,length.out=10)),rev(c(attr(drv,"gradient")*(0-r)+drv[1])+c(attr(drv,"gradient"))*seq(r,rN,length.out=10))), | ||
| col="yellow1") | ||
| points(rN,bond_price(c,n,f,r=rN),col="blue",pch=19) | ||
| lines(c(rN,rN),c(bond_price(c,n,f,r=rN),bond_price(c,n,f,r=min(yields))), col = | ||
| "grey",lty=3,lwd=2) | ||
| lines(c(rN,max(yields)),c(bond_price(c,n,f,r=rN),bond_price(c,n,f,r=rN)), col = | ||
| "grey",lty=3,lwd=2) | ||
| lines(c(rN,rN),c(0,bond_price(c,n,f,r=rN)), col = "blue",lty=3,lwd=2) | ||
| lines(c(0,rN),c(bond_price(c,n,f,r=rN),bond_price(c,n,f,r=rN)), col = | ||
| "blue",lty=3,lwd=2) | ||
|
|
||
| box() | ||
| axis(1,r) | ||
| axis(2,bond_price(c,n,f,r)) | ||
| # Last update: 1 October 2025 |
There was a problem hiding this comment.
[nitpick] The date format should use a consistent style. Consider using 'October 1, 2025' or '2025-10-01' for better readability and consistency.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 7 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| # Plot bond price as a function of yield | ||
| yields <- seq(0.02, 0.5, by = 0.01) | ||
| nominal_price <- bond_price(c, n, f, yields) | ||
| op <- par(mar = c(6, 4, 4.5, 4) + 0.5) |
There was a problem hiding this comment.
The variable op stores the previous par settings but is never used to restore them. Either remove this assignment or add par(op) at the end of the function to restore the original graphics parameters.
1b5d993 to
4f59ba1
Compare
4f59ba1 to
b84d273
Compare
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 8 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
bondPlot.R
Outdated
| mtext(side = 2, "Price in % for selected YTM", line = 2) | ||
| mtext(side = 3, "Market interest rate", line = 2) | ||
| mtext(side = 4, "Market price in %", line = 2) | ||
| par(op) |
There was a problem hiding this comment.
The par() reset should be wrapped in a tryCatch or on.exit() to ensure graphical parameters are restored even if an error occurs during plotting. Consider using on.exit(par(op)) right after line 18 where op is defined.
No description provided.