Skip to content

montybot/r-command-extractor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

R Command Extractor from Org Mode Files

This Python script extracts all R commands from an Org mode file and saves them to a specified output file. It is designed to help users quickly extract and reuse R code embedded in Org mode documents.


Context: Why R Code in Org Mode Files?

Org mode is a powerful and flexible document format used primarily in Emacs. It is often used for literate programming, where code and documentation are combined in a single file. Researchers and data scientists frequently use Org mode to write reproducible reports, combining R code with explanatory text, results, and visualizations.

For example, an Org mode file might contain:

  • R code blocks for data analysis.
  • Text explaining the analysis.
  • Results (tables, plots, etc.) generated by the R code.

This script helps extract the R code from such files, making it easier to reuse or debug the code independently of the Org mode document.


Features

  • Extracts R commands from Org mode files.
  • Saves the extracted commands to a text file.
  • Supports command-line arguments for input and output file paths.
  • Handles multiple R code blocks within a single Org mode file.
  • Includes robust error handling with user-friendly error messages.

Requirements

  • Python 3.x
  • No external libraries are required.

Installation

  1. Clone this repository to your local machine:

    git clone https://github.com/montybot/r-command-extractor.git
  2. Navigate to the project directory:

    cd r-command-extractor

Usage

Command-Line Syntax

Run the script from the command line as follows:

python extract_r_commands.py <source_file.org> <destination_file.txt>
  • <source_file.org>: Path to the Org mode file containing R code blocks.
  • <destination_file.txt>: Path to the output file where the extracted R commands will be saved.

Example

Suppose you have an Org mode file named example.org with the following content:

#+BEGIN_SRC R
x <- 1:10
y <- x^2
plot(x, y)
#+END_SRC

#+BEGIN_SRC R
summary(lm(y ~ x))
#+END_SRC

Run the script to extract the R commands:

python extract_r_commands.py example.org r_commands.R

The output file r_commands.R will contain:

x <- 1:10
y <- x^2
plot(x, y)
summary(lm(y ~ x))

Error Handling

The script includes robust error handling to ensure a smooth user experience. Here are some examples of error scenarios:

1. Incorrect Number of Arguments:

python extract_r_commands.py

Output:

Usage: python extract_r_commands.py <source_file.org> <destination_file.txt>
Example: python extract_r_commands.py example.org r_commands.txt

2. Input File Not Found:

python extract_r_commands.py missing_file.org output.txt

Output:

Error: The file 'missing_file.org' does not exist.

3. Permission Denied (Writing to Output File):

python extract_r_commands.py example.org /root/output.txt

Output:

An error occurred while writing to '/root/output.txt': [Errno 13] Permission denied: '/root/output.txt'

How It Works

  1. Input File Parsing:

    • The script reads the Org mode file and uses a regular expression to identify R code blocks. These blocks are enclosed between #+BEGIN_SRC R and #+END_SRC.
  2. Command Extraction:

    • It extracts all non-empty lines from the R code blocks and removes unnecessary whitespace.
  3. Output File Creation:

    • The extracted R commands are saved to the specified output file, with each command on a new line.
  4. Error Handling:

    • The script checks for common errors (e.g., missing files, permission issues) and provides clear error messages.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Commit your changes.
  4. Push your branch and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

[Michaël Gilbert]
GitHub: montybot

Acknowledgments

  • Thanks to the Org mode community for creating such a versatile document format.
  • Inspired by the need to automate the extraction of R code for reproducibility.

About

R Command Extractor from Org Mode Files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages