官术网_书友最值得收藏!

The efficient write-execute loop

One of the most productive ways to work with R, especially when learning it, is to use the write-execute loop, which makes use of the send to console functionality mentioned in the previous section. This will allow you to do two very important things: develop your code through small and quick iterations, which allow you to see step-by-step progress until you converge to the behavior you seek, and save the code you converged to as your final result, which can be easily reproduced using the source code file you used for your iterations. R source code files use the .R extension.

Assuming you have a source code file ready to send expressions to the console, the basic steps through the write-execute loop are as follows:

  1. Define what behavior you're looking to implement with code.
  2. Write the minimal amount of code necessary to achieve one piece of the behavior you seek in your implementation.
  3. Use the send to console functionality to verify that the result in the console is what you expected, and if it's not, to identify possible causes.
  4. If it's not what you expected, go back to the second step with the purpose of fixing the code until it has the intended piece of behavior.
  5. If it's what you expected, go back to the second step with the purpose of extending the code with another piece of the behavior, until convergence.

This write-execute loop will become second nature to you as you start using it, and when it does, you'll be a more productive R programmer. It will allow you to diagnose issues faster, to quickly experiment with a few ways to accomplishing the same behavior to find which one seems best for your context, and once you have working code, it will also allow you to clean your implementation to keep the same behavior but have better or more readable code.

For experienced programmers, this should be a familiar process, and it's very similar to Test-Driven Development (TDD), but instead of using unit-tests to automatically test the code, you verify the output in the console in each iteration, and you don't have a set of tests to re-test each iteration. Even though TDD will not be used in this book, you can definitely use it in R.

I encourage you to use this write-execute loop to work through the examples presented in this book. At times, we will show step-by-step progress so that you understand the code better, but it's practically impossible to show all of the write-execute loop iterations I went through to develop it, and much of the knowledge you can acquire comes from iterating this way.

主站蜘蛛池模板: 青河县| 老河口市| 抚顺县| 澄迈县| 凤冈县| 仁化县| 营山县| 宜章县| 宣城市| 偏关县| 安乡县| 醴陵市| 韶关市| 陇西县| 柘城县| 扎赉特旗| 当涂县| 丹东市| 奉节县| 花莲市| 永嘉县| 聂荣县| 通化县| 定结县| 鄂伦春自治旗| 古田县| 拉萨市| 乐平市| 商水县| 平南县| 贡山| 青龙| 荔波县| 保亭| 嘉善县| 瑞金市| 富锦市| 信丰县| 缙云县| 潼南县| 即墨市|