Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions R/readSO.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
#' Read in StackOverflow table-formatted data
#'
#' A wrapper for \link{read.table}. Reads a table from text or from the clipboard and creates a data.frame from it.
#'
#' @author Ananda Mahto
#'Read in StackOverflow pasted table-formatted data
#'
#'A wrapper for \link{read.table}. Reads a table from text or from the clipboard and creates a data.frame from it.
#'
#'For many questions at Stack Overflow, the question asker does not properly
#'share their question (for example, using \code{\link{dput}} or by sharing
#'some commands to make up the data). Most of the time, you can just copy and
#'paste the text into R using \code{read.table(text = "clipboard", header =
#'TRUE, stringsAsFactors = FALSE)}. This function is basically a convenience
#'function for the above.
#'
#'The output of \code{\link{readSO}} is automatically assigned to an object in
#'your workspace called "\code{mydf}" unless specified using the \code{out}
#'argument.
#'
#' @param sep character. Determines the field separator character passed to \code{read.table}.
#' @param header logical. Determines whether the first row consists of names of variables.
#' @param stringsAsFactors logical. Whether strings are converted to factors or remain character variables.
#' @param out character. Desired output object name. Defaults to \code{"mydf"}.
#' @return A data.frame as \code{read.table} produces.
#' @note By default, stringsAsFactors is FALSE which is different to the R default.
#'
#' @export

readSO <- function(sep = "", header = TRUE, stringsAsFactors = FALSE) {
suppressWarnings(
read.table(text = gsub("^#", "", readLines("clipboard")),
header = header, stringsAsFactors = stringsAsFactors,
sep = sep))
#' @author Ananda Mahto
#' @seealso \code{\link{dput}}, \code{\link{read.table}}
#' @examples
#'
#'\dontrun{
#'## Copy the following text (select and ctrl-c)
#'
#'# A B
#'# 1 2
#'# 3 4
#'# 5 6
#'
#'## Now, just type:
#'
#'readSO()
#'}
#'
readSO <- function(sep = "", header = TRUE, stringsAsFactors = FALSE, out = "mydf") {
OS <- ifelse(Sys.info()["sysname"] == "Darwin", "Mac", "others")
temp <- switch(
OS,
Mac = {
suppressWarnings(
read.table(text = gsub("^#", "", pipe("pbpaste")), header = header,
stringsAsFactors = stringsAsFactors, sep = sep))
},
others = {
suppressWarnings(
read.table(text = gsub("^#", "", readLines("clipboard")),
header = header, stringsAsFactors = FALSE, sep = sep))
})
assign(out, temp, envir = .GlobalEnv)
message("data.frame ", dQuote(out), " created in your workspace")
temp
}