Currently in RStudio keybinding CTRL/CMD + SHIFT + B perform the following job
devtools::install().
How can I change it to have this behaviour:
devtools::install(build_vignettes = TRUE)
If not, is it possible to create the entirely new keybinding for it?
You can execute anything by using an add-in. See https://rstudio.github.io/rstudioaddins/ for details. In summary, you create a small R package containing a function like
installWithVignettes <- function() devtools::install(build_vignettes = TRUE)
then set up a special file in the package inst/rstudio/addins.dcf that names this as an add-in. After that you can assign a shortcut to it.
Related
I use it a lot on Rmarkdow for referring to code, so I created an Addin, but wanted to know if there's a shortcut. If it isn't the case, how could I do to configure the addin so when calling it, the position of the caret or cursor stands between both symbols, exactly as it happens when using "" or () in RStudio.
insertInAddin <- function() { rstudioapi::insertText("``") } is the code I used for the Add-in
I'm looking help understanding how to setup
rstudioapi::setCursorPosition()
and document_position() inside the location argument of insertText.
You can use the shrtcts package for this task. It lets you assign Keyboard Shortcuts to arbitrary R Code.
Create a new R Markdown Snippet which can also be used on its own by typing e.g. in (for inline code font) and press Shift+Tab:
snippet in
`${1}`$0
Use the command shrtcts::edit_shortcuts() in the RStudio Console to open the file where you define your custom shortcuts.
Paste the following code inside that file (set your preferred keybinding in the #shortcut line). Note that the inserted text in the second line of the function must match the new Snippet from Step 1:
#' Code Font
#'
#' #description
#' If Editor has selection, transform current selection to code font.
#' If Editor has no selection, write between backticks.
#' #interactive
#' #shortcut Cmd+E
function() {
if (rstudioapi::selectionGet()$value == "") {
rstudioapi::insertText("in")
rstudioapi::executeCommand("insertSnippet") |>
capture.output() |>
invisible()
} else {
# Gets The Active Document
ctx <- rstudioapi::getActiveDocumentContext()
# Checks that a document is active
if (!is.null(ctx)) {
# Extracts selection as a string
selected_text <- ctx$selection[[1]]$text
# modify string
selected_text <- stringr::str_glue("`{selected_text}`")
# replaces selection with string
rstudioapi::modifyRange(ctx$selection[[1]]$range, selected_text)
}
}
}
This solution uses the native pipe |> and thus requires R 4.1.
You can of course just define separate variables in each line or use the magrittr pipe if you use earlier versions of R.
Further the stringr::str_glue() command can be easily replaced with a base R solution to avoid dependencies.
Use the command shrtcts::add_rstudio_shortcuts(set_keyboard_shortcuts = TRUE) in the RStudio Console to add the new shortcut with its assigned keybinding. Then restart RStudio.
Now you can use e.g. cmd+e without selection to place your cursor within backticks and press Tab to continue writing after the second backtick.
Or you can select text and then press cmd+e to surround the selected text by backticks.
The solution above can be easily generalized for bold and italic text in RMarkdown documents or to write within Dollar signs to add Inline Latex Math.
You can check RMarkdown Code Chunks and RStudio Shortcuts.
The shortcut you're looking for is: Ctrl+Alt+I.
Or you can create your own Code Snippet. To do this, follow the instructions on this page: Code Snippets in the RStudio IDE.
I am running the package 'pophelper' in RStudio on a MacBookAir where I need to choose multiple files using file.choose(). However, when the interactive window pops up I can only highlight one file and not choose multiple files with the 'Shift' key. I've tried the 'control', 'option', 'command' key and any combination thereof but only managed to highlight one file at a time. I have to choose dozens of files so doing it one by one is not an option. Has anybody else encountered this issue and is there a solution to it?
Thanks for your help
From this question file.choose for multiple files R, you can use tk_choose.files() in tcltk package to choose a list of files interactively.
In addition, you can also make a custom function with select.list() and set multiple = T and graphics = T to call a graphical widget and select more than one item.
file.choose2 <- function(path = "."){
file <- dir(path)
select <- select.list(file, multiple = T, graphics = T)
return(file.path(path, select))
}
file.choose2("path/to/your/files")
The drawback of this function is that you can only select those files in the given path.
I am looking to create a custom function in R that will allow the user to call the function and then it will produce an auto complete pipeline ready for them to edit, this way they can jump into quickly customizing the standard pipeline instead of copy pasting from an old script or retyping. How can I go about setting up this sort of autocomplete:
#pseudo code what I type---
seq.Date(1,2,by = "days") %>%
pblapply(function(x){
read.fst(list.files(as.character(x), as.data.table = T) %>%
group_by(x) %>%
count()
}) %>% rbindlist()
#how can I write a function so that when I call that function, it ouputs an autocomplete
#of the above so that I can go ahead and start just customizing the code? Something like this
my_autocomplete_function = function(x) {
print(
"
seq.Date(as.Date(Sys.Date()),as.Date(Sys.Date()+1),by = 'days') %>%
pbapply::pblapply(function(x){
fst::read.fst(list.files(as.character(x), as.data.table = T)) %>%
#begin filtering and grouping by below custom
group_by()
}) %>% rbindlist()
")
}
#I can just print the function and copy paste the text from the output in my console to my script
my_autocomplete_function()
#but I rather it just autocomplete and appear in the script if possible?
Putting text into the command line will probably be a function of the interface you are using to run R - is it plain R, Rstudio, etc?
One possibility might be to use the clipr package and put the code into the clipboard, then prompt the user to hit their "paste" button to get it on the command line. For example this function which creates a little code string:
> writecode = function(x){
code = paste0("print(",x,")")
clipr::write_clip(code)
message("Code ready to paste")}
Use it like this:
> writecode("foo")
Code ready to paste
Then when I hit Ctrl-V to paste, I see this:
> print(foo)
I can then edit that line. Any string will do:
> writecode("bar")
Code ready to paste
[ctrl-V]
> print(bar)
Its one extra key for your user to press, but having a chunk of code appear on the command line with no prompting might be quite surprising for a user.
I spend my day reading the source code for utils auto completion. The linebuffer only contains the code for ... one line, so that can't do fully what your looking for here, but it is quite customizable. Maybe the rstudio source code for autocompletion has more answers. Here is an example to write and activate your own custom auto completer. It is not well suited for packages as any new pacakge could overwrite the settings.
Below a
#load this function into custom.completer setting to activate
rc.options("custom.completer" = function(x) {
#perhaps debug it, it will kill your rsession sometimes
#browser()
###default completion###
##activating custom deactivates anything else
#however you can run utils auto completer also like this
#rstudio auto completation is not entirely the same as utils
f = rc.getOption("custom.completer")
rc.options("custom.completer" = NULL)
#function running base auto complete.
#It will dump suggestion into mutable .CompletionEnv$comps
utils:::.completeToken() #inspect this function to learn more about completion
rc.options("custom.completer" = f)
###your custom part###
##pull this environment holding all input/output needed
.CompletionEnv = utils:::.CompletionEnv
#perhaps read the 'token'. Also 'linebuffer', 'start' & 'end' are also useful
token = .CompletionEnv$token
#generate a new completion or multiple...
your_comps = paste0(token,c("$with_tomato_sauce","$with_apple_sauce"))
#append your suggestions to the vanilla suggestions/completions
.CompletionEnv$comps = c(your_comps,.CompletionEnv$comps)
print(.CompletionEnv$comps)
#no return used
NULL
})
xxx<tab>
NB! currently rstudio IDE will backtick quote any suggestion which is not generated by them. I want to raise an issue on that someday.
Bonus info: A .DollarNames.mys3class-method can be very useful and works with both rstudio and utils out of the box e.g.
#' #export
.DollarNames.mys3class = function(x, pattern = "") {
#x is the .CompletionEnv
c("apple","tomato")
}
In RStudio, [Ctrl] + [Enter] runs the currently highlighted code part, but with echo. [Ctrl] [Shift] + [S] sources the whole file without echo.
Is it possible to run the highlighted/selected code part without having the input clutter the console? Or is this an implicit requirement when running code instead of sourcing? (There seem to be subtle differences mentioned in other SO posts)
In conclusion: Is there a Hotkey to press to get exactly what [Ctrl]+[Enter] does, but without cluttering the console with my script code?
I don't believe there is a built-in way to do that. However, you can create an Addin that will source() the code and sink() results to a text file, which can be openend automatically.
In RStudio, create a New Project > R Package.
Create a new R script called runSelected:
#' runSelected
#'
#' RStudio addin to run selected code and show results in
#' default text editor.
#'
#' #export
runSelection <- function() {
context <- rstudioapi::getActiveDocumentContext()
selection <- rstudioapi::primary_selection(context)
tmp_code <- tempfile()
f <- file(tmp_code)
writeChar(object = selection$text, con = f)
close(f)
tmp_results <- tempfile()
sink(tmp_results)
source(tmp_code)
sink()
shell.exec(tmp_results)
}
In the package's directory, create file inst/rstudio/addins.dcf:
Name: Run Selection
Description: Run selected text and see results in text editor
Binding: runSelection
Interactive: false
Generate documentation with roxygen2, build and voilĂ !
Is it possible to open (not run using source) a R script for editing from the console of R studio or R command line
This is a nice command to edit an r script:
file.edit('foo.R')
You can open a file for editing with edit, e.g.,
edit(file = "test.R")
See help("edit") for more information, in particular regarding different editors.
You could just read it like a text file and do anything you want with it. This can be done using the following syntax
SourceF <- file("Source.R", open = "r")
SourceF_lines <- readLines(SourceF)
To follow with someting like:
cat(SourceF_lines, sep = "\n")
## or
writeClipboard(SourceF_lines)
Or replace a specific part:
SourceF_lines[2] <- '## Do not run!!'