43) is a good general reference of how to view the source code for.Internal and.Primitive functions. The basic steps are to first look for the function name in src/main/names.c and then search for the 'C-entry' name in the files in src/main/.
I want to look at the source code for a function to see how it works. I know I can print a function by typing its name at the prompt:
In this case, what does UseMethod('t')
mean? How do I find the source code that's actually being used by, for example: t(1:10)
?
Is there a difference between when I see UseMethod
and when I see standardGeneric
and showMethods
, as with with
?
In other cases, I can see that R functions are being called, but I can't find the source code for those functions.
How do I find functions like .cbindts
and .makeNamesTs
?
In still other cases, there's a bit of R code, but most of work seems to be done somewhere else.
How do I find out what the .Primitive
function does? Similarly, some functions call .C
, .Call
, .Fortran
, .External
, or .Internal
. How can I find the source code for those?
10 Answers
UseMethod('t')
is telling you that t()
is a (S3) generic function that has methods for different object classes.
For S3 classes, you can use the methods
function to list the methods for a particular generic function or class.
'Non-visible functions are asterisked' means the function is not exported from its package's namespace. You can still view its source code via the :::
function (i.e. stats:::t.ts
), or by using getAnywhere()
. getAnywhere()
is useful because you don't have to know which package the function came from.
The S4 system is a newer method dispatch system and is an alternative to the S3 system. Here is an example of an S4 function:
The output already offers a lot of information. standardGeneric
is an indicator of an S4 function. The method to see defined S4 methods is offered helpfully:
getMethod
can be used to see the source code of one of the methods:
There are also methods with more complex signatures for each method, for example
To see the source code for one of these methods the entire signature must be supplied, e.g.
It will not suffice to supply the partial signature
In the case of ts.union
, .cbindts
and .makeNamesTs
are unexported functions from the stats
namespace. You can view the source code of unexported functions by using the :::
operator or getAnywhere
.
Note that 'compiled' does not refer to byte-compiled R code as created by the compiler package. The <bytecode: 0x294e410>
line in the above output indicates that the function is byte-compiled, and you can still view the source from the R command line.
Functions that call .C
, .Call
, .Fortran
, .External
, .Internal
, or .Primitive
are calling entry points in compiled code, so you will have to look at sources of the compiled code if you want to fully understand the function. This GitHub mirror of the R source code is a decent place to start. The function pryr::show_c_source
can be a useful tool as it will take you directly to a GitHub page for .Internal
and .Primitive
calls. Packages may use .C
, .Call
, .Fortran
, and .External
; but not .Internal
or .Primitive
, because these are used to call functions built into the R interpreter.
Calls to some of the above functions may use an object instead of a character string to reference the compiled function. In those cases, the object is of class 'NativeSymbolInfo'
, 'RegisteredNativeSymbol'
, or 'NativeSymbol'
; and printing the object yields useful information. For example, optim
calls .External2(C_optimhess, res$par, fn1, gr1, con)
(note that's C_optimhess
, not 'C_optimhess'
). optim
is in the stats package, so you can type stats:::C_optimhess
to see information about the compiled function being called.
Compiled code in a package
If you want to view compiled code in a package, you will need to download/unpack the package source. The installed binaries are not sufficient. A package's source code is available from the same CRAN (or CRAN compatible) repository that the package was originally installed from. The download.packages()
function can get the package source for you.
This will download the source version of the Matrix package and save the corresponding .tar.gz
file in the current directory. Source code for compiled functions can be found in the src
directory of the uncompressed and untared file. The uncompressing and untaring step can be done outside of R
, or from within R
using the untar()
function. It is possible to combine the download and expansion step into a single call (note that only one package at a time can be downloaded and unpacked in this way):
Alternatively, if the package development is hosted publicly (e.g. via GitHub, R-Forge, or RForge.net), you can probably browse the source code online.
Compiled code in a base package
Certain packages are considered 'base' packages. These packages ship with R and their version is locked to the version of R. Examples include base
, compiler
, stats
, and utils
. As such, they are not available as separate downloadable packages on CRAN as described above. Rather, they are part of the R source tree in individual package directories under /src/library/
. How to access the R source is described in the next section.
Compiled code built into the R interpreter
If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror.
Uwe Ligges's R news article (PDF) (p. 43) is a good general reference of how to view the source code for .Internal
and .Primitive
functions. The basic steps are to first look for the function name in src/main/names.c
and then search for the 'C-entry' name in the files in src/main/*
.
In addition to the other answers on this question and its duplicates, here's a good way to get source code for a package function without needing to know which package it's in.e.g. if we want the source for randomForest::rfcv()
:
To view/edit it in a pop-up window:
To redirect to a separate file:
It gets revealed when you debug using the debug() function. Suppose you want to see the underlying code in t() transpose function. Just typing 't', doesn't reveal much.
But, Using the 'debug(functionName)', it reveals the underlying code, sans the internals.
EDIT:debugonce() accomplishes the same without having to use undebug()
Didn't see how this fit into the flow of the main answer but it stumped me for a while so I'm adding it here:
To see the source code of some base infix operators (e.g., %%
, %*%
, %in%
), use getAnywhere
, e.g.:
The main answer covers how to then use mirrors to dig deeper.
For non-primitive functions, R base includes a function called body()
that returns the body of function. For instance the source of the print.Date()
function can be viewed:
will produce this:
If you are working in a script and want the function code as a character vector, you can get it.
will get you:
Why would I want to do such a thing? I was creating a custom S3 object (x
, where class(x) = 'foo'
) based on a list. One of the list members (named 'fun') was a function and I wanted print.foo()
to display the function source code, indented. So I ended up with the following snippet in print.foo()
:
which indents and displays the code associated with x[['fun']]
.
There is a very handy function in R edit
It will open the source code of optim
using the editor specified in R's options
, and then you can edit it and assign the modified function to new_optim
. I like this function very much to view code or to debug the code, e.g, print some messages or variables or even assign them to a global variables for further investigation (of course you can use debug
).
If you just want to view the source code and don't want the annoying long source code printed on your console, you can use
Clearly, this cannot be used to view C/C++ or Fortran source code.
BTW, edit
can open other objects like list, matrix, etc, which then shows the data structure with attributes as well. Function de
can be used to open an excel like editor (if GUI supports it) to modify matrix or data frame and return the new one. This is handy sometimes, but should be avoided in usual case, especially when you matrix is big.
As long as the function is written in pure R not C/C++/Fortran, one may use the following. Otherwise the best way is debugging and using 'jumpinto':
View([function_name])
- eg. View(mean)
Make sure to use uppercase [V]. The read-only code will open in the editor.
You can also try to use print.function()
, which is S3 generic, to get the function write in the console.
In RStudio, there are (at least) 3 ways:
- Press the F2 key while cursor is on any function.
- Click on the function name while holdingCtrl or Command
View
(function_name) (as stated above)
A new pane will open with the source code. If you reach .Primitive or .C you'll need another method, sorry.
Not the answer you're looking for? Browse other questions tagged rfunctionr-faq or ask your own question.
How do you play dota online?
You can play on Battle.net or on Garena. Garena is open to players without originaly Warcraft III: Frozen Throne, so a lot of south east Asian players can be found there. Downloading Garena and installing it is simple enough, and after you set it up it is quite easy to join a game and get started. Battle.net is reserved for people who actually paid for their games. Read More
How do open a mp3 executable file?
An MP3 file should not be executable unless it is a malicious file. Don't open it. Read More
Can you make executable files with Java?
yes ,for that purpose we need executable jar file ,using launch4j open source software used to create executable file for window. Read More
How to create and open exe file in c?
An 'exe' extension file is created by a compiler that translates source code to machine code, usually in a Windows environment. An executable program can open any file (including other executable files) by using the standard file API calls with a binary open option. Unless you are doing something very specific with executable files it doesn't make much sense to read in an executable file from another executable file or program. Read More
What do you open mugen with?
Just double click. It's an executable file. Read More
What can cause an executable program or file not to open?
There are several possible reasons, but the most common is that the computer operating system doesn't recognise the file is an executable program. Read More
How do you open PS2 files on PC?
All you have to do is get an emulator and open the executable file on the disc using the emulator. Read More
Has anyone had Installous say Unable to find an executable?
Here is how I fixed the 'Unable to find an executable' problem: (i have a jailbroken ipod touch 2g) Open Installulous, and click on Settings Change the Install Method to DIRTY. THAT'S IT! After changing to DIRTY, I haven't had a single problem, all programs have installed correctly. Hope this helps! infochia Read More
How you recognize executable files in Unix and Linux?
Open a terminal, browse to the directory where the files are, and run the command 'ls -l'. On the left-most side there will read something that looks like -rwx-. 'r' means readable, 'w' means writable, and 'x' means that the file is executable. If the 'x' is not there, then the file is not executable. Read More
Where can you find a unprotected version of the most recent DotA all-stars version?
Two sites have very good info on unprotection and both have downloadable versions of DotA Allstars that are unprotected/deprotected: DeprotectDotA http://en.wikipedia.org/wiki/User:DeprotectDotA and Open DotA http://zibada.xgm.ru/opendota/index.php Read More
Can your iPad get virus through emails?
No, since the iPad will only open email attachments that cannot contain any executable code. Read More
How do you change NAT settings?
How do you fix iPhones fingerprint settings?
Go into settings, find touch and ID, open it, and follow the directions given. Read More
How do you change your market settings on your Android?
just open market and hit your menu button on phone. then click settings. Read More
How do you host dota games?
you host a dota game by creating a game being a blue player you can be the host by creating your own room.. open dota > local area network > create . . . .. . and that's it... if you want to play with AI(artificial intelligence) you can put some.. thank you.. mosh.mosh.nicol Read More
Why can't I open Google?
You should easily be able to open Google. If you are not able, check your network settings. Read More
How you can get dota games?
To play DotA one must simply own the game Warcraft 3 and the Frozen throne expansion. The user must open a battlenet account (ingame) and download the DotA map. Maps can be downloaded from getdota.com After downloading the map be sure to move the file into the Warcraft 3 maps folder. Do this by double-clicking 'my computer' and navigating to the proper folder and then dragging the file in. Read More
How do you open exe on mac without Intel?
Windows executable (.exe) files are programs that work with the Windows operating system and are not compatible with Macintosh computers. Read More
What is the difference between applet and jsp?
Applet is an executable file that is automatically downloaded when we open a browser. Swing applications can operate on local data when the network is not there. Read More
How do you export or publish c plus plus code into something more permanent like an application?
Compile and link it to produce a binary executable. You don't distribute your source code unless you are making the code publicly available (such as an open source application), you simply distribute the executable. Often the executable will be bundled with support libraries, help files, and so on, from which an installer can be built. You then distribute the installer and its support files. Read More
How can open mms message in idea?
You can go to your settings and click on messages. You will then be able to open any of the messages that you have received. Read More
How do you type on my laptop and keep it secret?
Open a private browser window! Internet Exlorer: Settings>In-Pricate Browsing Google Chrome: Settings> Open an Incognito window Read More
How do you open 16-bit .dll files?
You can't. DLL file ( Dynamic Link Library ) can only be opened by an executable ( EXE ) or compiler. Read More
How do you install codec?
Most codecs come as an executable file. All you have to do is double click, open it and install it. Its features are usually automatically applied to your movie players. Read More
How do you change your nat type on your xbox to open?
Test your xbox live connection in network settings and your nat type is open Read More
What is default setting?
default setting is the custom settings when you first open the page Read More
How do you change your Nat settings to open for halo?
You cant you have to have a completely different connection Read More
How do you get all my info off of my ipad and get it to factory settings I am reselling it and need to delete all the accounts.?
Open settings, then select 'General', then 'Reset'. Finally tap 'Erase all content and settings'. This will restore your whole iPad. Read More
Steps to give ip address to the computer?
To find your IP address, click start and then control panel. Under the control panel open up the network settings. Select an open network from the network settings and the details will provide the IP address. Read More
What is the path to the about screen on iPhone 5?
First open Settings which is probably on the first app page unless you've moved it. The path is Settings > General > About. Read More
What do I do if my iMessage app won't open?
Sometimes going to settings>general>reset>and reset all settings helps. it doesn't erase media Read More
In video games what is to load?
'Loading' a game means starting the executable file and loading the game data from the hard disk into the RAM. Essentially it means to start or open the game. Read More
How do you stop dc spam?
Settings - Appearance - Windows - remove 'open private messages in...' Read More
Gmail is not saving mail in the sent mailbox?
open gmail's settings and set it up there Read More
What do you open Minecraft with?
I'm not sure what you mean, i don't think that anybody does. Minecraft is it's own program. When you download it, there will be an icon on your desktop that you double-click on. That will open the screen to log in. Minecraft's portable launcher is an executable program. Just double-click on it and it will open. Read More
How do you open the Bluetooth of laptop?
Click on the start button which is usually on the bottom left of laptops. Click on control panel and go into connecton settings and you wil find the bluetooth settings there. Read More
When you open Team Fortress 2 it goes to the home screen for a second then it goes back to your desktop why?
because your video game settings are different from the desk top resolution settings :) Read More
Whenever i open Counter Strik then that say to i do the cod. how is the cod?
How do you remove the quick menu on a lg cosmos?
go to menu, settings & tools, phone settings, set shortcuts, set slide open view, and click home screen Read More
Where can I open Explorer to Documents and Settings?
Right click on the 'Start' button, then on the popup menu left click on 'Explore All Users' or 'Open All Users'. Read More
How do you stop your ipod from syncing your photos without deleting them off your computer?
When you plug in your ipod, open it in your itunes. Then open the photos tab and change any settings from there. Read More
How do you open your ports?
Open the ports in a computer depends on the platforms and operating system being used . Open the Windows Firewall by clicking the Start button and clicking on the Control Panel. On the left pane, there is the Advanced settings which will open the ports. Read More
How do you change the settings of the Pop-up blocker in Internet explorer?
Open Internet Explorer, click tools, and point to POP-Up Blocker. Click POP-Up Blocker Settings. Read More
How do you change your settings in minecraft if you messed them up to sprint?
Press escape to open the menu. Press settings. Press controls. Click the control you want to change. Press the key you want to change it to. Read More
How do you Unlock volume control on a computer?
You go to control panel, open volume settings, than Read More
How do you access settings on your router?
Normally you open your internet browser and type in either 192.168.2.1, 192.168.1.1 or 192.168.0.1 Read More
What is the 3 or 4 characters to the right of the period in a file name?
That is the file extension. Each file has this extension to tell the system what to do with the file. For example, a program has .exe at the end. This means that it is an executable file, so the system will launch it as a standard executable. A document has .doc or .docx. The system checks file association when you run it, and tells whatever program is set to handle documents to open that fileā¦ Read More
Can Android open all application?
it can only open these files txt,apk and configuration settings it cannot open exe,msdox,ppt,html or php Read More
I can't open attachment in my hotmail. The files are gray?
The most likely cause is that your security settings are set to not allow attachments from being opened. You would need to change these settings to allow attachments. Read More
How do you get rid of power save mode on the computer?
If you're running Windows Vista, right-click on the Desktop and click on Personalize. Open the Screen Saver settings. Now click on 'Change power settings'. Now, check what plan you're using. Click on the 'Change plan settings' right below that plan. Now change the plan settings (you might need to click on 'Change Advanced Power Settings'). Read More