#' Interact with Editor
#'
#' Interact with the active R script using the package \code{rstudioapi}.
#'
#' @param block Name of new block to insert. If used with addin, then a default name is used and can be changed by user.
#'
#' @import rstudioapi
#' @import stringr
#'
#' @name interact_editor
NULL
#' @describeIn interact_editor Insert a block around the primary selection.
#' @export
code_block <- function(block = "BLOCK_NAME"){
dcontext <- rstudioapi::getActiveDocumentContext()
docsel <- rstudioapi::primary_selection(dcontext)
sec_txt <- insert_section(section = block)
ind <- stringr::str_detect(sec_txt, "\\#\\[CONTENT HERE\\]")
sec_txt[ind] <- docsel$text
replacetxt <- stringr::str_c(c(sec_txt, "\n"), collapse = "\n")
rstudioapi::modifyRange(docsel$range, replacetxt)
}
insert_section <- function(section, width = 60){
secid <- paste0("#|->>@_", stringr::str_to_upper(section))
start_tag <- "_START_#"
stop_tag <- "_STOP_#"
##
## start block lines
##
tmpWidth <- width - stringr::str_length(stringr::str_c(secid, start_tag))
top <- paste0("#", stringr::str_dup("-", width-2), "#")
bot <- top
mid <- paste0(secid,
stringr::str_dup(" ", tmpWidth),
start_tag)
start_blk <- c(top, mid, bot, "#", "#[CONTENT HERE]", "#")
##
## stop block lines
##
tmpWidth <- width - stringr::str_length(stringr::str_c(secid, stop_tag))
top <- paste0("#", stringr::str_dup("-", width-2), "#")
bot <- paste0("#", stringr::str_dup("_", width-2), "#")
mid <- paste0(secid,
stringr::str_dup(" ", tmpWidth),
stop_tag)
stop_blk <- c(top, mid, bot)
return(c(start_blk, stop_blk))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.