{formatR} is not working.

{formatR} is not working. I got it to work once using Yihui's first example from formatR but since then I am getting an error.

> library(formatR)
> tidy_source()
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") : clipboard cannot be opened or contains no text

I have tried restarting R and RStudio I have no clue what that error message is trying to tell me. There is definitely something on the clipboard. Oh well, if nothing else I can clean up code in Quarto as the option

>| tidp: true

works there.

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
 [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

time zone: America/New_York
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] formatR_1.14

loaded via a namespace (and not attached):
 [1] compiler_4.3.2    pryr_0.1.6        magrittr_2.0.3    lobstr_1.1.2      cli_3.6.2        
 [6] tools_4.3.2       glue_1.6.2        rstudioapi_0.15.0 Rcpp_1.0.11       stringi_1.8.3    
[11] codetools_0

Unfortunately I don't have an Ubuntu system on hand to test (I tried on another Linux but don't seem to reproduce).

That we can understand by looking at the source code, simplified as:

if(is_apple){
 ...
} else{
 text = readLines(source, warn = FALSE)
}

And indeed, on many non-Apple systems, this reads the content of the clipboard:

readLines("clipboard")

But that gives a similar error message as above:

readLines("not clipboard")
#> Error in file(con, "r") : cannot open the connection
#> In addition: Warning message:
#> In file(con, "r") :
#>   cannot open file 'not clipboard': No such file or directory

To further ensure it's not a {formatR} problem, you can try:

tidy_source(text = clipr::read_clip() |> strsplit("\n") |> unlist())

If indeed {clipr} does work but not readLines("clipboard"), I suspect it might have to do with Wayland vs X11: There is definitely some code in the R source to handle reading from the X11 clipboard, I'm not so sure it works on pure Wayland.

Thanks,

tidy_source(text = clipr::read_clip() |> strsplit("\n") |> unlist())

seems to be working just fine on my simple examples.

Your code will work nicely as a work around until I see if I can find anyone discussing the problem on Ubuntu. My googling was not finding anything.

I had to go check to make sure I was using Wayland as I think I had it disabled on an earlier installation but yes I am using Wayland.

Much appreciated.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.