The problem
Lately, there has been some well-deserved buzz around addins in RStudio, datapasta being one and hrbraddins being another highly liked one.
I find datapasta helpful for creating little tibbles for teaching. I'll find some interesting data online and just copy and paste the table directly into the correct format. You can also set up keyboard shortcuts, because who doesn't love a keyboard shortcut. Thanks @MilesMcBain pic.twitter.com/deaZVVYYDu
β We are R-Ladies (@WeAreRLadies) July 22, 2019
My keyboard shortcut for this lil' function gets quite the workoutβ¦
β Mara Averick (@dataandme) July 29, 2019
πΊ "hrbraddins::bare_combine()" by @hrbrmstr https://t.co/8dwqNEso0B #rstats pic.twitter.com/gyqz2mUE0Y
All of this is done with RStudio Addins using the rstudioapi r package.
A lot of the popular addins follows the same simple formula
- extract highlighted text
- modify extracted text
- replace highlighted text with modified text.
if your problem can be solved with the above steps, then this post is for you.
The solution
Once you have found the name of your addin, go to your package directory, or create a new package. Then we use usethis to create a .R file for the function and to create the necessary infrastructure to let RStudio know it is a Addin.
use_r("name_of_your_addin")
use_addin("name_of_your_addin")
The inst/rstudio/addins.dcf
file will be populated to make a binding between your function to the addins menu. From here you will in Name
to change the text of the button in the drop-down menu and change the description
to change the hover text.
: New Addin Name
Name: New Addin Description
Description: name_of_your_addin
Binding: false Interactive
now you can go back to the .R to write your function. Below is the minimal code needed. Just replace any_function
with a function that takes a string and returns a modified string. build the package and you are done!
<- function() {
example
# Gets The active Documeent
<- rstudioapi::getActiveDocumentContext()
ctx
# Checks that a document is active
if (!is.null(ctx)) {
# Extracts selection as a string
<- ctx$selection[[1]]$text
selected_text
# modify string
<- any_function(selected_text)
selected_text
# replaces selection with string
::modifyRange(ctx$selection[[1]]$range, selected_text)
rstudioapi
} }
Examples - slugify
While I was writing this post I created an addin to turn the title of the blog post into a slug I could use. I replaced
<- any_function(selected_text) selected_text
with
<- stringr::str_to_lower(selected_text)
selected_text <- stringr::str_replace_all(selected_text, " ", "-") selected_text
Which gave me this little gem of an addin!
session information
β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
setting value 4.1.0 (2021-05-18)
version R version 10.16
os macOS Big Sur .0
system x86_64, darwin17
ui X11 language (EN)
-8
collate en_US.UTF-8
ctype en_US.UTF/Los_Angeles
tz America2021-07-15
date
β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ* version date lib source
package 1.3.2 2021-06-09 [1] Github (rstudio/blogdown@00a2090)
blogdown 0.22 2021-04-22 [1] CRAN (R 4.1.0)
bookdown 0.2.5.1 2021-05-18 [1] CRAN (R 4.1.0)
bslib 3.0.0 2021-06-30 [1] CRAN (R 4.1.0)
cli 0.7.1 2020-10-08 [1] CRAN (R 4.1.0)
clipr 0.2-18 2020-11-04 [1] CRAN (R 4.1.0)
codetools 1.4.1 2021-02-08 [1] CRAN (R 4.1.0)
crayon 1.3.0 2021-03-05 [1] CRAN (R 4.1.0)
desc * 0.2.1 2020-01-12 [1] CRAN (R 4.1.0)
details 0.6.27 2020-10-24 [1] CRAN (R 4.1.0)
digest 0.14 2019-05-28 [1] CRAN (R 4.1.0)
evaluate 0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0)
htmltools 1.4.2 2020-07-20 [1] CRAN (R 4.1.0)
httr 0.1.4 2021-04-26 [1] CRAN (R 4.1.0)
jquerylib 1.7.2 2020-12-09 [1] CRAN (R 4.1.0)
jsonlite * 1.33 2021-04-24 [1] CRAN (R 4.1.0)
knitr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)
magrittr 0.1-7 2013-12-03 [1] CRAN (R 4.1.0)
png 2.5.0 2020-10-28 [1] CRAN (R 4.1.0)
R6 0.4.11 2021-04-30 [1] CRAN (R 4.1.0)
rlang 2.9 2021-06-15 [1] CRAN (R 4.1.0)
rmarkdown 2.0.2 2020-11-15 [1] CRAN (R 4.1.0)
rprojroot 0.4.0 2021-05-12 [1] CRAN (R 4.1.0)
sass 1.1.1 2018-11-05 [1] CRAN (R 4.1.0)
sessioninfo 1.6.2 2021-05-17 [1] CRAN (R 4.1.0)
stringi 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
stringr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)
withr 0.24 2021-06-15 [1] CRAN (R 4.1.0)
xfun 1.3.2 2020-04-23 [1] CRAN (R 4.1.0)
xml2 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)
yaml
1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library [