How to remove the ASCII nul at the end of clipr::wirte_clip?

29 Views Asked by At

I love clipr::write_clip but I've noticed every time I write something to the clipboard with it, there is always a newline following it. Here's some sample code:

install.packages("clipr")
library(clipr)

text = "Hello world"
clipr::write_clip(text)
result = "Hello world
"

As you can see, when I paste the result into a set of quotation marks there is a pesky newline. Ideally when I paste the result into a set of quotes it would look like this:

result = "Hello world"

I've dug through the documentation at https://www.rdocumentation.org/packages/clipr/versions/0.8.0/topics/write_clip and tried different settings for breaks and eos, but so far no luck. The description of eos says "The terminator to be written after each string, followed by an ASCII nul." and I think the ASCII nul is causing my problem here, does anyone know a way to get rid of it?

Thank you!

2

There are 2 best solutions below

1
Grzegorz Sapijaszko On

Hard to replicate, because:

text = "Hello world"
clipr::write_clip(text, allow_non_interactive = TRUE)

clipr::read_clip(allow_non_interactive = TRUE)
#> [1] "Hello world"

Created on 2024-02-29 with reprex v2.1.0

Please note, allow_non_interactive = TRUE is just for reprex to be able to execute in background, it doesn't influence the results.

0
Jacob Neff On

I found a workaround using utils::writeClipboard instead

install.packages("utils")
library(utils)

text <- "Hello world"
utils::writeClipboard(str = charToRaw(paste0(text, " ")), format = 1)
"Hello world"

As you can see, pasting into a set of quotes after that call has no line break. This could probably also work with clipr:write_clip but it didn't seem to like being passed charToRaw so I just switched to utils::writeClipboard. Thanks for the help all!