I have written a function that allows me to take four columns of data from the clipboard, split it, add an extra column, recombine it, and export it as a text file.
xyztoinp <- function(x) {
x <- read.table(file="clipboard")
a <- x[,-1]
b <- x[,1]
c <- ifelse(b == "C", 6,
ifelse (b == "O", 8,ifelse (b == "H", 1, 3)))
x <- cbind(b,c,a)
write.table(x, file="IRCoutput", quote = FALSE,
sep = "\t", row.names = FALSE, col.names = FALSE)
}
I have two questions:-
Is it possible to create a file that has the table that I've created with this function such that the table is pasted into the middle of some text?
Eg
text_text_text_text_text_text_text_
text_text_text_text_text_text_text_
Neatly formated table
text_text_text_text_text_text_text_
text_text_text_text_text_text_text_
Secondly, I tried to modify the function so that it would take a file name as a variable; ie, xyztoinp <- function(x, NewFileName) {
etc... But this didn't produce an output with the new file name; does anyone know how this is done?
Here is my working code,
xyztoinp <- function(x) {
x <- read.table(file="clipboard")
a <- x[,-1]
b <- x[,1]
c <- ifelse(b == "C", 6,
ifelse (b == "O", 8,
ifelse (b == "H", 1, 3)))
x <- cbind(b,c,a)
zz <- file("NewFile.inp", "w")
cat("text",
" text",
"text",
"",
"text",
"text",
"text", file = zz, sep = "\n")
write.table(x, file=zz, quote = FALSE,
sep = "\t", row.names = FALSE, col.names = FALSE)
cat("text",file=zz)
close(zz)
}
thanks all, esp @Greg. ps It wasn't possible to vary the filename in the function but I'm happy with the above.