Using R code to automatically generate new Wikipedia redirect pages
Published
January 2, 2026
Using Quarto and R to generate text for Wikipedia
This page is an experiment with using Quarto and R to generate text for Wikipedia. The goal is to create a reproducible workflow that can help in generating content for Wikipedia articles, such as redirects, summaries, or infoboxes. The basic design is human-in-the-loop editing and the first experiment here is the simplest possible configuration, just linking two names together. It’s a proof of concept to enable much more complex content addition, supervised at each step by human verification.
Example: Generating Redirects
Redirects allow multiple names to link to the same encyclopedic content on Wikipedia.
We create two functions to create redirects on Wikipedia:
create_redirect(from_title, to_title): Generates a link to create a redirect from from_title to to_title if the from_title page does not already exist.
create_multiple_redirects_to_one(redirects_from, redirect_to): Uses create_redirect to generate multiple redirects from a list of titles to a single target title.
Code
create_redirect <-function(from_title, to_title) {# 1. Manually define the Wikipedia redirect syntax with Hex Encoding# # = %23 | [ = %5B | ] = %5D | Space = %20 prefix <-"%23REDIRECT%20%5B%5B" suffix <-"%5D%5D"# 2. Encode the destination title for use inside the URL# We use URLencode with reserved = TRUE to catch spaces and special chars encoded_destination <- utils::URLencode(to_title, reserved =TRUE)# 3. Assemble the preload text and the edit summary preload_content <-paste0(prefix, encoded_destination, suffix) edit_summary <-paste0("Redirecting%20to%20%5B%5B", encoded_destination, "%5D%5D")# 4. Check if the page exists using your existing logic wikipedia_pages <-page_info("en", "wikipedia", page = from_title)$query$pagesif (names(wikipedia_pages) =="-1") {# Construct the raw URL targeting index.php# We use from_title for the 'title' param so we are creating the new page base_url <-"https://en.wikipedia.org/w/index.php" auto_fill_url <-paste0( base_url, "?title=", utils::URLencode(from_title, reserved =TRUE),"&action=edit","&preloadtext=", preload_content,"&summary=", edit_summary )# 5. Return as a raw HTML link to protect the %23 from Quarto's renderer link_html <-paste0('<a href="', auto_fill_url, '" target="_blank" ','style="color: #0056b3; text-decoration: underline; font-weight: bold;">','🚀 Create redirect: ', from_title, ' → ', to_title, '</a> ','<br>Add this text:' )# We also keep the copy-paste box as a fallback redirect_text_plain <-paste0("#REDIRECT [[", to_title, "]]") copy_box <-paste0('<div style="display: flex; align-items: center; width: 80%; border: 1px solid #dee2e6; border-radius: 4px; padding: 5px; background-color: #f8f9fa; margin: 10px 0;">','<code style="flex-grow: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">', redirect_text_plain, '</code>','<button onclick="navigator.clipboard.writeText(\'', redirect_text_plain, '\'); this.innerText=\'✅\'; setTimeout(()=>this.innerText=\'📋\', 1000)" ','style="border:none; background:none; cursor:pointer; padding-left: 10px;">📋</button>','</div>' )return(paste0(link_html, "<br>", copy_box)) } else { wikipedia_url <- wikipedia_pages[[1]]$fullurlreturn(paste0("Page [", from_title, "](", wikipedia_url, ") already exists.")) }}create_multiple_redirects_to_one <-function(redirects_from, redirect_to) {if (length(redirect_to) !=1) {stop("This function needs one redirect destination.") }if (assertthat::is.string(redirects_from)) {return(create_redirect(redirects_from, redirect_to)) } else { redirects_md <-sapply(redirects_from, function(from) {create_redirect(from, redirect_to)}) }return(paste(glue("## Redirects to '{redirect_to}'\n\n"), paste(redirects_md, collapse ="\n\n---\n\n")))}
Here are three sample uses of the create_multiple_redirects_to_one function. The last two are practical uses, coming from the corresponding pages: Hickory Ground and Occaneechi people. The latter ultimately comes from the name lists (or synonymy) generated by James Mooney as shown in the margin.
Code
create_multiple_redirects_to_one(c("Alpha", "Beta", "Gamma", "Delta"),"Greek Letters")# A Native American settlement known by multiple namescreate_multiple_redirects_to_one( c("Otciapofa", "Odshiapofa", "Ocheopofau","Ocheubofau", "Oce Vpofa"), "Hickory Ground")# A Native American people referenced by different names in the historical recordcreate_multiple_redirects_to_one( c("Achonechy", "Aconechos", "Akenatzy","Hockinechy", "Occaneches", "Occaanechy", "Occhonechee","Occonacheans", "Occoneechee", "Ockanechees", "Ockanigee","Okenechee", "Acconeechy", "Occaneeches", "Ochineeches", "Ockinagee"),"Occaneechi")