Skip to content

Instantly share code, notes, and snippets.

@elifiner
Created July 27, 2015 11:33
Show Gist options
  • Save elifiner/4a575ca89a9014b35ba8 to your computer and use it in GitHub Desktop.
Save elifiner/4a575ca89a9014b35ba8 to your computer and use it in GitHub Desktop.
Count R functions in R code
import re
import os
import fnmatch
from collections import defaultdict
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
if __name__ == '__main__':
funcs = defaultdict(int)
for filename in find_files('.', '*.R'):
with open(filename, encoding='cp1252') as f:
for line in f:
for func in re.findall(r'(\w+)\(', line):
funcs[func] += 1
for name, count in sorted(funcs.items(), key=lambda v: v[1], reverse=True):
if count < 10:
break
print(name, count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment