Skip to content

Instantly share code, notes, and snippets.

@jimhester
Created April 9, 2020 13:41
Show Gist options
  • Save jimhester/bfed4f1e9eb6d17f24241111a5fee21b to your computer and use it in GitHub Desktop.
Save jimhester/bfed4f1e9eb6d17f24241111a5fee21b to your computer and use it in GitHub Desktop.
Enable the use of Instruments from the command line with R

Enable use of Instruments from the command line with R

First, the time profiler template has a space in it, which breaks when called from R, so we need to add sym link to it that doesn't have a space.

cd /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/Resources/templates
sudo ln -s 'Time Profiler.tracetemplate' 'time-profiler.tracetemplate'

Then, we need to workaround R being a little too helpful when passing arguments with -d, so we need to comment out those lines. e.g. apply the following diff to /Library/Frameworks/R.framework/Versions/3.6/Resources/bin/R

--- 3.5/Resources/bin/R	2019-03-13 10:40:24.000000000 -0400
+++ 3.6/Resources/bin/R	2019-12-19 14:47:40.000000000 -0500
@@ -278,15 +280,15 @@
       args_ok=yes
       ;;
   esac
-  if test -n "${args}${*}" && test "${args_ok}" = no; then
-    args=`expr "${args} ${*}" : " *\(.*\)"`
-    echo "*** Further command line arguments ('${args}') disregarded"
-    echo "*** (maybe use 'run ${args}' from *inside* ${debugger})"
-    echo ""
-    exec ${debugger} ${debugger_args} "${R_binary}"
-  else
+  #if test -n "${args}${*}" && test "${args_ok}" = no; then
+    #args=`expr "${args} ${*}" : " *\(.*\)"`
+    #echo "*** Further command line arguments ('${args}') disregarded"
+    #echo "*** (maybe use 'run ${args}' from *inside* ${debugger})"
+    #echo ""
+    #exec ${debugger} ${debugger_args} "${R_binary}"
+  #else
     exec ${debugger} ${debugger_args} "${R_binary}" ${args} "${@}"
-  fi
+  #fi
 fi
 
 ### Local Variables: ***

After those two steps you can run instruments on a file 'script.R' with the following command

R -d instruments --debugger-args="-t time-profiler" --vanilla -f script.R

This will generate a new directory instrumentscli0.trace, with the number incremented as needed.

You can open the trace in Instruments with open instrumentscli0.trace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment