JadaLovelace
JadaLovelace

Reputation: 204

How do I create a content index for code in R

For my job I have to write scripts that are upwards of 600 lines. It's important that my scripts are readable for my coworkers, but I noticed that even when reading my own code I need to scroll a lot to find a certain function.

The issue is not so much that the code is ugly or not commented - each line has a comment explaining what is being done and why. It's also organized - it follows a very predictable structure. What i'm strugglin with is all the vertical scrolling. It seems outdated. When i'm writing a word or latex document, I can create headers that are recognized by the software as index points. Those index points then show up in a table of contents.

Can I do something similar in R? Is there any programming language that allows for indexing points like this? Wikipedia does it too, for example the link https://en.wikipedia.org/wiki/Meteorological_history_of_Hurricane_Patricia#Peak_strength will take you to the paragraph about the peak strength of the hurricane.

Upvotes: 2

Views: 682

Answers (2)

Johannes Stötzer
Johannes Stötzer

Reputation: 506

if you use RStudio, you could do it like this. You need to use comments like this and show the document outline (next to Source)

#library----

#some code---- 


#example----

enter image description here

Upvotes: 5

Federico Giorgi
Federico Giorgi

Reputation: 10735

My very personal trick is the following. I organize chunks of code in if(TRUE) blocks, so that I can fold them, e.g. in RStudio.

# Step 1 of the pipeline
if(TRUE){
    message("Step 1 starting")
    Sys.sleep(0.1)
    i<-5
    i<-i+5
}

# Step 2 of the pipeline
if(TRUE){
    message("Step 2 of the pipeline")
    i<-i^2
    message("The results is ",i)
}

So that it will look tidier in the end:

enter image description here

Upvotes: 1

Related Questions