├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── R └── pasteLastVal.R ├── README.md ├── inst └── rstudio │ └── addins.dcf ├── lm_example.gif ├── man └── pasteLastVal.Rd ├── pasteLastVal.Rproj └── simple_example.gif /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: pasteLastVal 2 | Type: Package 3 | Title: Paste the Last Value as Comment in RStudio 4 | Version: 0.2.0 5 | Author: Kyaw Sint (Joe) 6 | Maintainer: Kyaw Sint (Joe) 7 | Description: RStudio addin to paste the .Last.value as a comment in the next line of code as a comment in the active Rstudio editor. 8 | Encoding: UTF-8 9 | LazyData: true 10 | License: MIT + file LICENSE 11 | RoxygenNote: 7.1.1 12 | Imports: 13 | stringr, 14 | rstudioapi 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Kyaw Sint (Joe) -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(pasteLastVal) 4 | importFrom(rstudioapi,document_position) 5 | importFrom(rstudioapi,getSourceEditorContext) 6 | importFrom(rstudioapi,insertText) 7 | importFrom(rstudioapi,setCursorPosition) 8 | importFrom(stringr,regex) 9 | importFrom(stringr,str_replace_all) 10 | importFrom(utils,capture.output) 11 | -------------------------------------------------------------------------------- /R/pasteLastVal.R: -------------------------------------------------------------------------------- 1 | # Paste the .Last.value as a comment in the next line of code 2 | # as a comment in the active Rstudio editor 3 | # by Kyaw Sint (Joe), Thanks to the RStudio team for addins and rstudioapi 4 | # 5 | # modified by AndrewLawrence based on: 6 | # https://github.com/fraupflaume 7 | # https://github.com/rstudio/addinexamples 8 | 9 | 10 | #' pasteLastVal 11 | #' 12 | #' Pastes the last value as a comment. 13 | #' 14 | #' @importFrom rstudioapi insertText getSourceEditorContext setCursorPosition 15 | #' document_position 16 | #' @importFrom stringr str_replace_all regex 17 | #' @importFrom utils capture.output 18 | #' @export 19 | pasteLastVal <- function() { 20 | outputstr <- utils::capture.output( 21 | tryCatch( 22 | print(.Last.value), 23 | warning = "", 24 | error = "" 25 | )) 26 | 27 | outputstr <- paste(outputstr, collapse = "\n") 28 | outputstr <- paste("#", outputstr) 29 | outputstr <- gsub("\n", "\n# ", outputstr) 30 | outputstr <- paste(outputstr, "\n") 31 | 32 | outputstr <- stringr::str_replace_all(outputstr, 33 | stringr::regex("(\\033.*?m)"), 34 | "") 35 | 36 | se_context <- rstudioapi::getSourceEditorContext() 37 | se_id <- se_context$id 38 | 39 | rstudioapi::insertText(text = outputstr, 40 | id = se_id) 41 | rstudioapi::setCursorPosition( 42 | rstudioapi::document_position( 43 | se_context$selection[[1]][["range"]][["end"]][1], 44 | 1 45 | ), 46 | id = se_id 47 | ) 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pasteLastVal 2 | An addin for RStudio to paste last value as comment in code by Kyaw Sint (Joe) 3 | 4 | ## Installation 5 | ``` 6 | devtools::install_github("ksint/pasteLastVal") 7 | ``` 8 | Add keyboard shortcut 9 | 1) Go to `Tools` -> `Modify Keyboard Shortcuts...` 10 | 2) Type `addin` in the search box 11 | 3) Assign a shortcut for `Paste Last Value`. I use `Ctrl + Shift + V` 12 | 13 | ## Usage 14 | 1) Access the pasteLastVal function by `Addins` -> `Paste Last Value` or 15 | 2) Use the user-assigned keyboard shortcut (see above). 16 | 17 | ## Screenshots 18 | ![Simple Example](simple_example.gif) 19 | 20 | ![Example from lm documentation](lm_example.gif) 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /inst/rstudio/addins.dcf: -------------------------------------------------------------------------------- 1 | Name: Paste Last Value 2 | Description: Pastes the last value as a comment. 3 | Binding: pasteLastVal 4 | Interactive: false 5 | -------------------------------------------------------------------------------- /lm_example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksint/pasteLastVal/1dfdba890e1ba80835cd1b336fbd5b106a6392e9/lm_example.gif -------------------------------------------------------------------------------- /man/pasteLastVal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pasteLastVal.R 3 | \name{pasteLastVal} 4 | \alias{pasteLastVal} 5 | \title{pasteLastVal} 6 | \usage{ 7 | pasteLastVal() 8 | } 9 | \description{ 10 | Pastes the last value as a comment. 11 | } 12 | -------------------------------------------------------------------------------- /pasteLastVal.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /simple_example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksint/pasteLastVal/1dfdba890e1ba80835cd1b336fbd5b106a6392e9/simple_example.gif --------------------------------------------------------------------------------