Label code chunks in regular Rstudio .r script - r

An .rmd document in Rstudio is easy to navigate to the various code chunks using the little drop down in the bottom left corner of the script window.
In a regular .r file, functions are listed with a helpful 'f' icon and the function name and commented chunks are also listed with a helpful '#' icon, but with (untitled) next to them.
What do I need to include in my comment so the "(untitled)" will be replaced with a label?
example:
#----------------------------------------------------
# FirstLabel - This is the first chunk of code I want to navigate to.
#----------------------------------------------------

Add #### at the end of every comment you want to list
# FirstLabel - This is the first chunk of code I want to navigate to. ####
The nice thing about this is that you can hide everything between these comments using the drop down arrow on the left of the comment

#### still works, but now you can also add ---- at the end or click on Code>Insert Section...
# new section ----
The keyboard shortcut on windows is CTRL+SHIFT+R and on Mac: CMD+SHIFT+R

Related

How can I insert a line of text above the occurence of a specific text in a long R Script?

I have been looking for a solution to this problem but could not find a practical one.
I have a long R script (more than 1500 lines).
I have the following text which repeats itself several times in the Script:
df00$StartDate
I need to add the following line of text just above it (everywhere where it occurs):
df00$`MealPlan` <- sub('\n.*', '', output3$`Your choices`)
Is there an efficient way to do this instead of scrolling through the Script and manually pasting that new line of code?
CTRL+F search for df00$StartDate, then click "All", then start editing, the edit will be done for all selected rows at the same time.
See screenshot, you will see a multiline cursor at the end of each line:
2015-05-06: RStudio v0.99 Preview: More Editor Enhancements

Paned plots in R notebook export

When editing an R notebook inside RStudio, if I create multiple graphical outputs in one R block, I get an icon for each plot, which I can click on to select which plot to look at:
I like that behavior, it's especially handy to click from one plot to another to compare changes between them.
However, when I render the notebook to HTML (e.g. by hitting the "Preview" button in the editor), the plots simply cascade down the page:
Is there a way I can get the former behavior in an exported document? Some option I can set, or a chunk of Javascript I can include, or something?

What cause format change when copy and paste in rstudio?

#sample select
sample_frac(mydata,n%)#random select n% sample
##############data review####
Just copy above code into rstudio script, you will find 2 more tab added to the last line.
What cause it?
Edit
As mentioned by #Jay in the comments, the n% in the command is treated as a function and since it is not complete it indents the next line.
To further confirm, try with df %in% in the script or df >%> and hit enter to see the cursor goes to the next line with an indent.
To avoid that just complete the function there.
sample_frac(mydata,n)
OR
sample_frac(mydata, n %% somenumber)
whatever you are trying to do and it should be fine.
Original Answer
It did add 2 tab spaces in the code when pasting in the RStudio script. I tried to paste the same text in my notes, Pycharm editor but it did not add any extra tabs there. So it was sure that this is a RStudio issue.
It turns out it is the indentation settings in RStudio which is responsible for this. To change that:
Go to Tools -> Global Options. Click on the Code option on the left. You'll see this :
Uncheck Auto-indent code after paste
and click on OK.
Now try to paste the same text. Should be resolved.

Click on a URL from an R string output

Suppose I have an output from the cat function of R which is a URL. For example:
cat("https://en.wikipedia.org/wiki/Statistics")
# Output: https://en.wikipedia.org/wiki/Statistics
Is there any command on the cat function or any other thing so that the output https://en.wikipedia.org/wiki/Statistics becomes a clickable URL in the R console?
As you mentioned in the comments you are using RStudio. It is not specified why it has to be the console in R, but I assume there is a good reason to display the links within RStudio and I assume the viewer pane on the right next to the console also works for you.
If that is the case you could do the following:
library(DT) # for datatable() function
library(shiny) # for tags$a() function
data <- data.frame(link = toString(tags$a(href = paste0("http://google.de"), "google")))
datatable(data, escape = FALSE)
Very close to the console ;)
It would depend on how the output is being viewed. If this output is going to an HTML file, then all you need to do is set this as the location for a hyperlink.
https://en.wikipedia.org/wiki/Statistics
But if, for example, you are just viewing the output in notepad, then I don't believe notepad has the functionality for hyperlinks.
I don't believe the R console directly supports hyperlinks or not. But it looks like from this: http://rmarkdown.rstudio.com/lesson-2.html that you can maybe use R Markdown to do what you want.
This is not currently possible in RStudio, unfortunately. However, it is an open issue which you can upvote if you think it should be prioritised.

Define what makes a code section in rstudio

Rstudio changed how a code section is defined. In version 0.99.902 code sections had to have some text behind the hash symbol. But now in version 1.0.136 if there are 5 hashes in a row it will define a new section.
Is there anyway to make it go back to the old way of defining sections? It isn't a big deal except I would mark my sections with hashes above and below the name and now it is creating 3x as many sections.
Old version:
New version:
I don't know if there's a way to recover the old behavior, but you could use + instead. In addition, you can put this in a code snippet (if you haven't already). In Preferences, go to the Code tab, scroll to the bottom and click the Edit Snippets button. Then add something like the following:
snippet hd
`r "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
### HEAD ##########
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"`
Then, when you type hd followed by a tab (actually two tabs, since the first tab will bring up a few options that start with hd, but hd will be at the top, so you can just press tab twice) in your R script file, the following will appear:
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
### HEAD ##########
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Better yet, you can create a snippet that takes the heading text as an argument:
snippet hd
`r paste("# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
"### ", "${1:HEAD}", " ##########\n",
"# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", sep="")`
Then, when you type hd followed by two tabs, the HEAD text will be highlighted and you can just type in your actual heading text.
Unfortunately, this behavior has changed between RStudio v0.98.1091, v0.99.903 and the current release v1.0.136.
In RStudio v0.98.1091, 'empty' headers such as ##### were recognized as section headers.
This behavior was briefly changed with v0.99.903, such that some initial text was required for these to be recognized as section headers. A number of users were unhappy as this effectively broke code folding for users who were explicitly using standalone ##### blocks to create sections.
Because of that, the behavior was reverted in RStudio v1.0.136, and so now standalone ##### blocks are again recognized as section headers.

Resources