Skip to content

Instantly share code, notes, and snippets.

@noamross
Created November 19, 2012 21:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save noamross/4114258 to your computer and use it in GitHub Desktop.
Save noamross/4114258 to your computer and use it in GitHub Desktop.
Use R's native data editor to edit a CSV
#' Use the data editor for a CSV file
#'
#' This function loads a CSV file, lets the user edit it in the native data
#' editor, then re-saves it, prompting the user for a new file name if desired.
#'
fix.csv <- function(file, new.name=TRUE, sep=",", comment.char="") {
tmpframe <- read.csv(file, sep=sep,quote="", colClasses="character",
stringsAsFactors=FALSE, comment.char="",
blank.lines.skip=FALSE, na.strings="")
tmpframe <- edit(tmpframe)
if(is.character(new.name)) {
out.name <- new.name
} else if(new.name <- TRUE) {
out.name <- readline(prompt="Enter file name to save (Hit enter to use original):")
} else {
out.name <- file
}
if(out.name=="") out.name <- file
write.table(tmpframe, file=out.name, append=FALSE, quote=FALSE, sep=sep,
row.names=FALSE)
}
@tylerhoecker
Copy link

This is cool! I occasionally have to make small, permanent edits to a csv that are easier done in an editor than in code. Having to open Excel is annoying, and this is a great solution.

@kenahoo
Copy link

kenahoo commented Jun 20, 2023

Probably should be if (new.name) on line 13 instead of if(new.name <- TRUE).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment