Skip to content

Instantly share code, notes, and snippets.

@jamiefolson
jamiefolson / sys_top.R
Created November 9, 2014 21:46
Getting process ID and running `top` from R
#' Run the Linux top command
#'
#' @param user character value indicating a system user to filter tasks by
#' @param pid one or more process ids to retrieve
#'
#' @return
#' A data.frame value containing the information returned by top.
#'
sys.top <- function(user=Sys.getenv("USER"),pid=NULL){
# Run top in batch mode "-b" once "-n 1"
@jamiefolson
jamiefolson / package_dependencies.R
Created November 9, 2014 21:39
Get (recursive) package dependencies
library(stringr)
package_names_from_description <- function(pkg, imports=TRUE){
# include imports as well as depends
deps <- c("Depends",if(imports)"Imports")
pkg_descr <- packageDescription(pkg,fields=deps)
pkg_list <- unlist(pkg_descr)
pkg_list <- pkg_list[!is.na(pkg_list)]
if (length(pkg_list)==0)return(character(0))
pkg_full_names <- unlist(str_split(as.character(pkg_list),","))
# Strip the version info from the name
@jamiefolson
jamiefolson / peaks.R
Created June 21, 2013 14:55
Finding local maxima and minima in R
#' Find local maxima and minima in R
#' @param x numeric vector to search for peaks
#' @param partial whether or not to include partial peaks
#' at the beginning or end of the vector
#' @param decreasing whether to find peaks (the default)
#' or valleys
which.peaks <- function(x,partial=TRUE,decreasing=FALSE){
if (decreasing){
if (partial){
which(diff(c(TRUE,diff(x)<=0,FALSE))>0)