09:09, 3rd June 2021
Writing to a File with Python's print() Function
Python's print() function can be configured to write output to files instead of the console by redirecting the standard output stream, a process achievable both through command-line execution and within scripts using the sys module. By temporarily assigning the sys.stdout object to a file, text can be directed to a file during script execution, with the original output restored afterward to avoid unintended side effects. Similarly, the standard error stream can be redirected for error messages and the file parameter in the print() function offers a more direct method to specify output destinations without altering the global stdout setting, allowing for flexible and targeted file writing within Python programs.
11:27, 28th May 2021
Programiz is a programming education platform used by around 4.9 million people each month, with a mission to make learning to code more accessible and straightforward. Operated by a small team of dedicated developers, it offers a broad range of free tutorials covering languages and technologies such as Python, Java, C, C++, JavaScript, SQL, HTML, CSS, TypeScript, Kotlin, Swift, Rust, Ruby and Go, alongside paid courses for those seeking more structured learning. The platform also provides online compilers and editors for a wide variety of programming languages, allowing users to write and run code directly in their browser. Recognising that written content alone is not sufficient for an effective learning experience, Programiz has extended its offering to include mobile applications for iOS and Android, covering languages such as Python, C, Java and C++.
11:25, 28th May 2021
fpdf2 is a Python library designed for simple and fast PDF document generation, forked from the earlier PyFPDF project. It supports Python 3.10 and above, offering a broad range of features including Unicode font embedding for a wide variety of languages, image embedding, SVG import, barcode and chart generation, table creation, HTML to PDF conversion and document encryption and signing. The library integrates with popular frameworks such as Django, Flask and FastAPI, and has been adopted by several open-source projects. It is available via PyPI and maintained by a community of contributors, with more than 1,300 unit tests and validation through multiple PDF checkers ensuring reliability across releases.
11:21, 28th May 2021
SAS Workshops and Notes
The Social Science Computing Cooperative at UW-Madison provides a comprehensive set of SAS learning materials authored by Doug Hemken, covering everything from fundamental concepts to advanced programming techniques. The materials guide users through working with SAS interfaces on both Windows and Linux systems, managing SAS files and submitting commands, and understanding the core SAS language and its grammar.
Statistical procedures such as frequencies, crosstabs, means, correlations and regression are covered alongside three distinct graphics systems and their associated commands. More advanced topics include building and reading data sets, subsetting and merging data, working with arrays and macros and producing output data sets. The materials also address how to document SAS work using Markdown and RMarkdown for producing web pages, PDF handouts and Word documents that incorporate SAS code and output, and include guidance on running R from within SAS.
11:18, 28th May 2021
SASweave: Literate programming using SAS
SASweave is a tool designed to integrate SAS code, output and graphics into documents, enabling the creation of reports that combine executable code with its results. It processes a LaTeX-based source file containing SAS code, executes the code and generates a LaTeX file that includes the code, output and any generated graphics, which can then be compiled into a formatted document.
The tool supports processing of both SAS and R code within the same source file, with actions determined by file extensions. It also provides a tangling feature to extract SAS code for separate use. By ensuring that output directly reflects the executed code, SASweave facilitates literate programming, a method that intertwines documentation, code and results to enhance transparency and reproducibility in analytical workflows.
11:17, 28th May 2021
How to track the performance of your blog in R?
Antoine Soetewey demonstrates how to use the googleAnalyticsR package in R to analyse blog performance data drawn from Google Analytics. Using a year's worth of data spanning December 2019 to December 2020, the blog had attracted nearly 322,000 users, generating over 428,000 sessions and 560,000 page views, with a notable traffic spike in late April 2020 caused by a viral post about downloading free Springer books during the COVID-19 lockdown.
He then works through a series of visualisations built with ggplot2, covering daily session trends, traffic by channel, sessions broken down by day of the week and hour of the day, monthly comparisons and top-performing pages. A particularly useful section addresses time-normalised page views, which adjusts for the fact that older posts have had more time to accumulate traffic, allowing for fairer comparisons between articles published at different times. Organic search accounts for the majority of traffic, that desktop usage peaks later in the day compared to mobile and that weekday traffic is consistently higher than weekend traffic, suggesting readers engage with the content primarily for professional or educational purposes.
11:16, 28th May 2021
RPubs serves as a straightforward online platform for publishing documents created using R Markdown, enabling users to share analyses, reports, tutorials and reproducible research with minimal setup. The process involves writing an R Markdown file, converting it to HTML and publishing it directly through RStudio, which generates a public URL without requiring users to manage hosting or deployment. Published content typically includes statistical analyses, visualisations, code and narrative explanations, all consolidated into a single document. While RPubs offers free hosting, one-click publishing and permanent URLs, its minimalist design limits features such as custom domains, advanced styling and multipage site creation, often prompting users to transition to more comprehensive tools like Quarto or Posit Connect for complex publishing needs.
11:15, 28th May 2021
The R Graph Gallery is a comprehensive online collection of over 400 charts created using the R programming language, organised into nearly 50 chart categories that span distribution, correlation, ranking, mapping, flow and evolution chart types. Each example includes reproducible code and detailed explanations, with foundational tutorials covering core structures before progressing to step-by-step customisation guides.
The gallery places particular emphasis on the tidyverse and ggplot2 packages, and is connected to the Data to Viz project, which provides a decision tree to help users select the most appropriate chart type for their data. Beyond beginner-level content, the gallery also curates a selection of exceptional R-based visualisations sourced from across the internet and community submissions, offering templates and code snippets for those looking to advance their data visualisation skills.
11:13, 28th May 2021
Laying out multiple plots on a page in R
Creating multiple plots in R and arranging them on a page involves using packages such as ggplot2, gtable and grid, which define the structure of individual plots through graphical objects. Functions like grid.arrange from gridExtra allow simple layouts by specifying rows, columns, or custom matrices, while more complex arrangements, such as aligning plot panels or embedding one plot within another, may require converting plots to grobs and using tools like gtable or egg for precise control.
Techniques include using annotation_custom for insets, adjusting dimensions to ensure alignment and combining plots with tables or other graphical elements. Alternatives like cowplot and patchwork offer additional flexibility, while the grid package provides foundational low-level functions for layout management. These methods enable the organisation of multiple plots, whether for alignment, shared legends, or multipage outputs, ensuring consistent visual presentation and adaptability to different display requirements.
11:12, 28th May 2021
Vignette: Write & Read Multiple Excel files with purrr
Martin Chan demonstrates how to use the functional programming package purrr, alongside readxl and writexl, to write and read multiple Excel and CSV files in R. The approach involves splitting a dataset into a list of data frames, in this case using the iris dataset divided by species, and then iterating over those data frames to export them as either a multi-sheet Excel file or individual CSV files.
For reading files back into R, the post outlines two broad options: loading each dataset separately into the global environment using assign(), or reading everything into a single list, which keeps the workspace tidier and makes it easier to apply operations across all data frames simultaneously. Both approaches are demonstrated for Excel and CSV formats, with the purrr method presented as cleaner and more consistent with tidyverse conventions than traditional alternatives such as lapply() or for loops.