|
| 1 | +--- |
| 2 | +title: "How to find out how much of R Core is R" |
| 3 | +date: "2024-12-25" |
| 4 | +category: R |
| 5 | +output: html_document |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +Cleaning out my computer as I get ready to switch to a new one has me running into old gems. So, when I say "Today I learned," I really mean "I learned this back in December 2021." 😅 |
| 11 | + |
| 12 | +Back then, I gave a talk at Why R? called |
| 13 | +[Packages for Using R With Python, Tableau, and Other Tools](https://www.youtube.com/watch?v=vyA2EiIz4pI&feature=youtu.be). One part of the talk was about how R itself isn't just made up of R. |
| 14 | + |
| 15 | +I adapted [this classic blog post](https://librestats.wordpress.com/2011/08/27/how-much-of-r-is-written-in-r/) by wrathematics to explore the composition of the [R 4.1.2 source package](https://cran.r-project.org/src/base/R-4/). The post features a script that scans the `.R`, `.c`, and `.f` files in the source, then records the language (R, C, or Fortran) and the number of lines of code in each language to a CSV file. Keep in mind, I have almost no knowledge of Shell (and this was pre-ChatGPT days!), so it took me a bit to adapt the original script from 2011. |
| 16 | + |
| 17 | +```{.bash filename="shell.sh"} |
| 18 | +outdir="./" |
| 19 | + |
| 20 | +rdir="./R-4.1.2" #eg, ~/R-2.13.1/ |
| 21 | +cd $rdir/src |
| 22 | + |
| 23 | +for rfile in `find . -type f -name *.R` |
| 24 | +do |
| 25 | +loc=`wc -l $rfile | sed -e 's/ ./,/' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\///g' -e 's/\///'` |
| 26 | +echo "R,$loc" >> $outdir/r_source_loc.csv |
| 27 | +done |
| 28 | + |
| 29 | +for cfile in `find . -type f -name *.c` |
| 30 | +do |
| 31 | +loc=`wc -l $cfile | sed -e 's/ ./,/' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\///g' -e 's/\///'` |
| 32 | +echo "C,$loc" >> $outdir/r_source_loc.csv |
| 33 | +done |
| 34 | + |
| 35 | +for ffile in `find . -type f -name *.f` |
| 36 | +do |
| 37 | +loc=`wc -l $ffile | sed -e 's/ ./,/' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\//\//g' -e 's/\/[^/]*\///g' -e 's/\///'` |
| 38 | +echo "Fortran,$loc" >> $outdir/r_source_loc.csv |
| 39 | +done |
| 40 | +``` |
| 41 | + |
| 42 | +The script creates a file called `r_source_loc.csv`. It shows the number of lines by programming language by script in R 4.1.2. We can read it into R: |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +::: {.cell} |
| 47 | + |
| 48 | +```{.r .cell-code} |
| 49 | +library(dplyr) |
| 50 | +library(stringr) |
| 51 | + |
| 52 | +r_loc <- |
| 53 | + readr::read_table(here::here("til-r", "r-composition", "r_source_loc.csv"), |
| 54 | + col_names = c("language", "lines", "script")) |> |
| 55 | + mutate(language = case_when(str_detect(language, "R,,") ~ "R", |
| 56 | + str_detect(language, "C,,") ~ "C", |
| 57 | + str_detect(language, "Fortran,,") ~ "Fortran"), |
| 58 | + lines = as.numeric(lines)) |> |
| 59 | + distinct() |
| 60 | + |
| 61 | +head(r_loc) |
| 62 | +``` |
| 63 | + |
| 64 | +::: {.cell-output .cell-output-stdout} |
| 65 | + |
| 66 | +``` |
| 67 | +# A tibble: 6 × 3 |
| 68 | + language lines script |
| 69 | + <chr> <dbl> <chr> |
| 70 | +1 R 20 .snow2.RR |
| 71 | +2 R 9 .multicore3.RR |
| 72 | +3 R 15 .multicore2.RR |
| 73 | +4 R 10 .multicore1.RR |
| 74 | +5 R 25 .RSeed.R |
| 75 | +6 R 36 .Master.R |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +::: |
| 80 | +::: |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +Now, we can visualize the percentage of R Core sourcecode files by language using ggplot2: |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +::: {.cell} |
| 89 | + |
| 90 | +```{.r .cell-code} |
| 91 | +library(ggplot2) |
| 92 | +library(forcats) |
| 93 | + |
| 94 | +r_loc |> |
| 95 | + filter(!is.na(language)) |> |
| 96 | + group_by(language) |> |
| 97 | + summarise (n = n()) |> |
| 98 | + mutate(rel.freq = n / sum(n), accuracy = 0.1) |> |
| 99 | + ggplot(aes(x = fct_reorder(language, desc(rel.freq)), y = rel.freq, fill = language)) + |
| 100 | + geom_bar(stat = "identity") + |
| 101 | + geom_text( |
| 102 | + aes(label = scales::percent(rel.freq)), |
| 103 | + position = position_dodge(width = 0.9), |
| 104 | + vjust = -0.25, |
| 105 | + size = 4 |
| 106 | + ) + |
| 107 | + theme_minimal() + |
| 108 | + labs(title = "Percentage of R Core Sourcecode Files by Language") + |
| 109 | + theme(plot.title = element_text(size = 14), |
| 110 | + axis.title.x = element_blank(), |
| 111 | + axis.title.y = element_blank(), |
| 112 | + axis.text.x = element_text(size = 12), |
| 113 | + axis.text.y = element_blank()) + |
| 114 | + scale_fill_manual(values = c("R" = "#332288", |
| 115 | + "C" = "#882255", |
| 116 | + "Fortran" = "#44AA99")) |
| 117 | +``` |
| 118 | + |
| 119 | +::: {.cell-output-display} |
| 120 | +{width=672} |
| 121 | +::: |
| 122 | +::: |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +Or, we can visualize the percentage of R Core lines of code by language: |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +::: {.cell} |
| 131 | + |
| 132 | +```{.r .cell-code} |
| 133 | +r_loc |> |
| 134 | + filter(!is.na(language)) |> |
| 135 | + group_by(language) %>% |
| 136 | + summarise(sum_lines = sum(lines, na.rm = TRUE)) |> |
| 137 | + ungroup() |> |
| 138 | + mutate(percent = sum_lines/sum(sum_lines)) |> |
| 139 | + ggplot(aes(x = fct_reorder(language, desc(percent)), y = percent, fill = language)) + |
| 140 | + geom_bar(stat = "identity") + |
| 141 | + geom_text( |
| 142 | + aes(label = scales::percent(percent)), |
| 143 | + position = position_dodge(width = 0.9), |
| 144 | + vjust = -0.25, |
| 145 | + size = 4 |
| 146 | + )+ |
| 147 | + theme_minimal() + |
| 148 | + labs(title = "Percentage of R Core Lines of Code by Language") + |
| 149 | + theme(plot.title = element_text(size = 14), |
| 150 | + axis.title.x = element_blank(), |
| 151 | + axis.title.y = element_blank(), |
| 152 | + axis.text.x = element_text(size = 12), |
| 153 | + axis.text.y = element_blank(), |
| 154 | + legend.position = "none") + |
| 155 | + scale_fill_manual(values = c("R" = "#332288", |
| 156 | + "C" = "#882255", |
| 157 | + "Fortran" = "#44AA99")) |
| 158 | +``` |
| 159 | + |
| 160 | +::: {.cell-output-display} |
| 161 | +{width=672} |
| 162 | +::: |
| 163 | +::: |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +It’s interesting to see how much goes into making R what it is: an ecosystem built on collaboration across languages and tools (which was the takeaway from the talk!). If you’re curious about R's source code, give the script a shot! |
0 commit comments