Why Ecologists Should Learn Python or R?

Recently, several teachers shared some resources for learning Python on WeChat. Compared to R, Python is a true programming language that can be used not only for scripting and numerical calculations but also for writing command-line programs, developing user interface applications, creating websites, plotting, and calling functions from libraries written in C, C++, and Fortran, among others. Python has a broader range of applications than R, being widely used in fields such as chemistry, physics, astronomy, and bioinformatics, in addition to its fun aspects. With such a powerful language, it is certainly worth understanding and learning. Notably, many Python projects are open-source, meaning the source code can be downloaded for detailed study by interested individuals. Another advantage of Python is its cross-platform compatibility, allowing it to be compiled into standalone applications. Python scripts can also be used to drive software like MySQL, SQLite, ArcGIS, and Adobe InDesign for secondary development, making it extremely versatile. Python resources are abundant, with tens of thousands of packages available, including many bioinformatics packages. Biopython is one such package, which provides functions for complex operations on DNA sequences, enabling access to and querying of GenBank. Python’s syntax is concise and easy to understand, making it very accessible. Python users do not need to deal with complex nested braces; they only need to pay attention to indentation. Python also features the pip package management system, making it very convenient to write packages. Mastering Python means mastering these resources. After reading the above introduction, are there any friends who have not yet learned Python feeling tempted?

However, there is another option: the R language, also known as R software, which is the command that drives R software. In fact, the essence of R is the S language, which originated at Bell Labs in the United States. However, the S language software S-Plus is prohibitively expensive. Two teachers from the University of Auckland in New Zealand created a software to implement the S language using C and Fortran during their teaching process, thus giving birth to R software. Since these two teachers opened the source code of R, it has gained attention in the academic community. The S language has a simple syntax that is close to the familiar C language for many, excelling in statistical analysis and plotting. Moreover, R adheres to UNIX traditions, with all commands, including functions in packages, having very detailed help files, which many languages and software lack. Many Python packages do not achieve this level of documentation. In recent years, R has become the most popular software for ecological research, especially in areas such as forest plot data processing, geographic information and spatial analysis, statistical plotting, phylogenetic comparative analysis, economics, and Bayesian statistics, with top scholars consistently using R. The publication of R-related books by renowned publishers such as Springer, Wiley, and O’Reilly reflects R’s popularity. According to the Tiobe programming language popularity ranking, R has risen to 8th place, which is remarkable for a scripting language with a narrow application domain. Many research papers even provide R code directly in their appendices. The journal Methods in Ecology and Evolution, published by the British Ecological Society, has a column dedicated to software packages for ecology and evolutionary analysis, with R packages making up the vast majority. In the past, researchers might have provided MATLAB code or SAS programs, but this has gradually decreased in recent years. In contrast, ecological research providing Python code is still relatively scarce.

For ecological research, both R and Python are excellent languages, but R has the edge. Mastering any programming language requires extensive practice, and even if an ordinary person spends a lot of time, they may not grasp the essence of a programming language, let alone use it proficiently. Python’s coverage is vast; it can accomplish almost anything that can be done with a scripting language.

However, the dominance of a programming language in a discipline often depends on luck, but more importantly, it relies on whether there are enough talented individuals who can make groundbreaking contributions and establish a solid foundation for development. For example, the reason R software dominates in phylogenetic comparative analysis is fundamentally due to the APE package written by French evolutionary biologist E. Paradis, which defined the ‘phylo’ data type and designed the format for storing evolutionary trees in R along with basic operations. Consequently, if you want to extract species names, you can use $tip.label; to reorder the evolutionary tree, you can use ladderize(); and to plot, you can use plot.phylo(); Paradis created APE and introduced the corresponding operations in his book Analysis of Phylogenetics and Evolution. APE simplifies operations on evolutionary trees, quickly attracting a large number of users, including many top scholars, who have developed packages like laser, phytools, picante, etc., introducing numerous new analytical methods, making R the go-to for evolutionary analysis. A similar example in community ecology is the vegan package, which is primarily used for multivariate statistics and plays a crucial role in analyzing community ecology data, such as calculating alpha and beta diversity, CCA, DCA ordination, and variance decomposition, all accomplished with a single function. The results can be directly plotted or further analyzed, making it extremely convenient. Vegan is also a collaborative effort among many top ecologists. For biodiversity analysis, vegan is indispensable, and more and more people are starting to use vegan for community data analysis, which is a significant driving force for many to learn R. Previously, community ecologists mainly used CANOCO, TWINSPAN, or other multivariate statistical analysis software for data processing, each with its own data format. At that time, even Pierre Legendre, the renowned author of Numerical Ecology, had to write programs in Fortran himself.

Currently, R is very important in ecological and evolutionary research at the Chinese Academy of Sciences and domestic universities. Dr. Lai Jiangshan’s R training courses at the Institute of Botany are in high demand, with every session fully booked. Many people learn R not only to master a skill but also to understand resources. Learning a programming language opens a window to the world; programming languages are no different in this regard, allowing one to see what can be done with the language and what resources are available. However, the key to learning is not to try to learn too many things at once but to delve deeply into one area. Even if one has started learning R or Python, if they do not master it thoroughly, they may not know how to use the available code and packages effectively. Wickham, the author of ggplot2, suggests in his book R for Data Science that readers should first master one language to a high level of proficiency, which makes it easier to integrate knowledge. I wholeheartedly agree with this statement.

For students who have learned R, what have they become proficient in? Here, I will share my experience in writing R packages.

In fact, learning R does not necessarily require writing packages. Writing packages involves some LaTeX documentation, and sometimes it requires calling C, C++, or Fortran code, dealing with dependencies on other packages, and understanding the format of the Description file and UTF-8 encoding, among many other issues that require time to learn thoroughly. Moreover, as R evolves, the requirements for writing R packages frequently change, and packages that once worked properly may be removed from CRAN for not meeting the new standards. There are also limitations on compiling across different operating platforms and data size restrictions. For example, during the R package checking process, there are dozens of criteria, and if any one of them fails, the package cannot be submitted to CRAN. The strict quality control of packages is not due to the R management team’s harshness. Recently, a teacher from the Kunming Institute of Botany complained that installing an R package required ten other dependencies, and some of those packages depended on several others. However, one of the secondary dependency packages was removed from CRAN for being non-compliant, which prevented the package from being installed correctly. When packages are not well maintained, errors can easily affect user experience and tarnish the reputation of CRAN and R. Of course, one could argue that the packages on CRAN are the responsibility of their authors, and errors are inevitable, so the R core team bears no responsibility. While this is true, no one wants an abundance of unreliable packages.

At the end of 2017, I suddenly received an email from Professor Kurt Hornik at the University of Vienna, asking me to modify phylotools because some functions in the package read paths outside the user’s working directory, which violated CRAN regulations. The phylotools package is primarily used to construct a supermatrix of DNA sequences. At the end of 2009, I supplemented the DNA barcode data from the large sample site in Gutian Mountain, Zhejiang, in the laboratory of researcher Ge Xuejun at the South China Botanical Garden. After conducting some experiments, I needed to align the rbcLa, matK, and trnH-psbA genes for over 140 species and then assemble them into a supermatrix. This is easily accomplished in a computer program, but it is very cumbersome to do manually. At that time, I was not very familiar with R, so I wrote some R scripts to create the supermatrix, and these functions later became the core of phylotools. In 2015, after rechecking this package, I felt that many functions were not well written, so I rewrote them and hosted them on GitHub. When I received the email notification to modify phylotools, I simply made some changes to the new version on GitHub and submitted it to CRAN. The new version was quickly accepted by CRAN, but there was a cost: because I had rewritten some functions, the parameters differed from the old version, and R scripts based on the old version were no longer usable. Nevertheless, as the package author, I felt that the new version’s code was more elegant and the function design was more reasonable. In contrast, although I was not satisfied with the HK80 and spaa packages, I have not modified them due to limited energy. The other two R packages, plantlist for querying plant families and herblabel for printing plant labels, have been hosted on R-Forge and GitHub, and I have not put in the effort to submit them to CRAN.

Does being able to write R packages indicate a high level of R programming skill? The answer is no. Writing packages is something that any user who can write R functions can achieve by learning a bit about package writing techniques. Being able to write packages does indicate a familiarity with R, at least to the extent of being able to write R functions. The ability to write functions is the dividing line between beginners and those who are more familiar with a language. In learning programming, beginners first become familiar with syntax, such as assignment, function calls, loops, and how to query help; then they move on to data manipulation, string operations, and regular expressions, and writing functions. After that, they learn various algorithms. Some say that algorithms are the soul of functions, and in many cases, this is indeed true. Functions are designed to achieve certain functionalities, and algorithms tell you how to implement those functionalities. Without a deep understanding of algorithms, it is challenging to solve more complex problems. For instance, writing a function for CCA ordination or variance decomposition is impossible without a solid grasp of the relevant theories. These are the true tests of foundational theory and skill, and the gap in core competitiveness between domestic ecologists and their international counterparts lies here.

Learning a programming language, whether Python or R, helps cultivate good thinking habits and a rigorous attitude. In programming, even a single punctuation error can lead to failure. At the same time, learning programming requires understanding many abstract concepts, such as S3 and S4 methods, object-oriented programming, inheritance, and polymorphism, as well as some mathematical and statistical content, including least squares, maximum likelihood, Bayesian statistics, non-parametric statistics, and linear and mixed models, etc.

Breaking down a problem into solvable programming tasks requires a step-by-step approach, thinking carefully, and proceeding cautiously, which can technically resolve the issue. However, truly solving ecological and evolutionary problems, discovering new knowledge, and proposing new viewpoints are not merely technical issues; they cannot be answered by programming alone. At this point, it is necessary to integrate various pieces of information, deeply grasp the essence of the problem, and engage in profound thinking. No matter how strong one’s programming skills are, without deep reflection, a comprehensive understanding of the problem, and a focus on specific academic issues, one is likely to remain at a technical level, lacking profound insights. This is a consideration that needs careful thought, whether learning Python or R.

January 9, 2018, in Tai Po, Hong Kong

http://blog.sciencenet.cn/blog-255662-1093799.html

Why Ecologists Should Learn Python or R?

Follow us

WeChat ID:sciencenet-cas (← Long press to copy) or long press the QR code below

Why Ecologists Should Learn Python or R?

Leave a Comment