Most of all: always keep in mind that you need to test and debug your code. Do not just write code to satisfy requirements. Consider testing and debugging to be a requirement itself. That mind set is a good starting point to follow these rules:
- R-Studio provides quite some functionality useful for debugging: step-by-step execution of your code, a trace of function calls, inspection of variables, and the opportunity to run your own code on the console while the app is on hold.
- If breakpoints do not work (sometimes they just won't), add
browser()
to your code which creates a "forced" breakpoint.
- Sometimes
print()
helps getting additional information output to the console.
- Clearly separate the business logic from the UI. Use unit tests (testthat). If errors occur, write some sample code to test the business logic outside the shiny app.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…