1

I have a data frame like that:

x <- data.table(c('ACCN-NJ-A55O-01A-11D-A25L-08','ACCN-NJ-A55O-11D-11D-A25L-08', 'ACCN-05-4249-01A-01D-1105-08', 'ACCN-S2-AA1A-15C-12D-A397-08'))

Now I want to put every word inside the double quote and then place a comma. I tried dput, cat, paste, etc but they return print instead of assigning to an object. I need to assign it to an object for copying it to the clipboard. I tried dput and dget

dput( x$V1, "names")
xx <- dget("names")
xxxx <- cat(paste0('"', paste(xx, collapse="\", \""), '"'))

It return the expected output like

"ACCN-NJ-A55O-01A-11D-A25L-08", "ACCN-NJ-A55O-11D-11D-A25L-08", "ACCN-05-4249-01A-01D-1105-08", "ACCN-S2-AA1A-15C-12D-A397-08"

but I want to assign it into an object. Any help would be appreciated. Thanks in advance

SUMIT
  • 563
  • 4
  • 12
  • This looks like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Roland Aug 23 '21 at 07:27

2 Answers2

2

You can use -

val <- sprintf('"%s"', x$V1)
val

#[1] "\"ACCN-NJ-A55O-01A-11D-A25L-08\"" "\"ACCN-NJ-A55O-11D-11D-A25L-08\""
#[3] "\"ACCN-05-4249-01A-01D-1105-08\"" "\"ACCN-S2-AA1A-15C-12D-A397-08\""

Note that R escapes " hence you see the quotes with backslash. The "real" string can be seen with cat.

cat(val)

"ACCN-NJ-A55O-01A-11D-A25L-08" "ACCN-NJ-A55O-11D-11D-A25L-08" 
"ACCN-05-4249-01A-01D-1105-08" "ACCN-S2-AA1A-15C-12D-A397-08"

If you want the output to be of length one comma-separated string use toString(val).

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you dear sir, but AFAK cat is the print function, I cannot assign to df <- cat(val). Yes R escapes ". Actually, I have 1000s of files which I need to move to another dir. Since I cannot copy the whole cat/dput output beyond the moniter in rstudio so I thought to assign it to an object, copy that object to clip, and paste it another object filestocopy file.copy(from=filestocopy, to=targetdir, copy.mode = TRUE) – SUMIT Aug 23 '21 at 06:40
  • Its OK if it is not possible, I am doing it by dividing the all files into 100-100 files and then via dupt i am trying to move – SUMIT Aug 23 '21 at 06:41
  • 1
    `cat` doesn't return any value. It returns `NULL`. If you want to move files use `list.files` to get the path of the files and pass the path directly to `file.copy`. – Ronak Shah Aug 23 '21 at 06:52
  • I need to filter the list.files based condition and then trying to move the selected file, I tried your suggestion but return character(0) – SUMIT Aug 23 '21 at 07:05
  • Brother its OK I am accepting the answer and closing it. My process taking time but its ok. I don't want to waste your precious time. Thanks – SUMIT Aug 23 '21 at 07:06
1

write.table to clipboard works.

    write.table(t(as.vector(x)), "clipboard", row.names = FALSE, col.names = FALSE, sep = ",")

then ctrl+V to script returns

    "ACCN-NJ-A55O-01A-11D-A25L-08","ACCN-NJ-A55O-11D-11D-A25L-08","ACCN-05-4249-01A-01D-1105-08","ACCN-S2-AA1A-15C-12D-A397-08"

I'm not sure if this is what you wanted.

Park
  • 14,771
  • 6
  • 10
  • 29
  • Thank you so much but it return error "Error in file(file, ifelse(append, "a", "w")) : 'mode' for the clipboard must be 'r' on Unix". Earlier I was also getting the same error with the clipboard so I use xclip – SUMIT Aug 23 '21 at 05:58
  • clipboard <- function(x, sep="\t", row.names=FALSE, col.names=TRUE){ con <- pipe("xclip -selection clipboard -i", open="w") write.table(x, con, sep=sep, row.names=row.names, col.names=col.names) close(con) } clipboard(x) – SUMIT Aug 23 '21 at 05:59
  • 1
    https://stackoverflow.com/questions/10959521/how-to-write-to-clipboard-on-ubuntu-linux-in-r this link may be helpful – Park Aug 23 '21 at 06:04
  • That is for xclip. would you modify the code and assign the output to an object? Afterward I will do. Thanks in advance – SUMIT Aug 23 '21 at 06:12
  • 1
    Are you working on LINUX? clip <- pipe("pbcopy", "w") \ write.table(t(as.vector(x)), file=clip) \ close(clip) \ I'll apologize that it is hard to understand your error status or situation. – Park Aug 23 '21 at 06:41
  • Yes it is in linux. it retrun the error Error: unexpected '\\' in "clip <- pipe("pbcopy", "w") \" – SUMIT Aug 23 '21 at 06:42
  • 1
    this comment place doesn't allow changing the line, if I understand error message you've got. Would you try that code again with out \'s? Code has three lines, > clip <- pipe("pbcopy", "w") >write.table(t(as.vector(x)), file=clip) >close(clip). Thanks – Park Aug 23 '21 at 06:48
  • Sorry my bad, I am sorry dear, dear first line clip <- pipe("pbcopy", "w") return error sh: 1: pbcopy: not found any idea? why – SUMIT Aug 23 '21 at 06:52
  • 1
    I'll try with LINUX and then reply later. My apologies – Park Aug 23 '21 at 06:57
  • 1
    Thank you, dear. It's OK I am not going to give trouble, I will work around the problem. Thanks – SUMIT Aug 23 '21 at 06:59