I want to copy a dataframe to the clipboard so as to paste it in an email.
I tried clipr::write_clip and write.table but none of those solutions renders a well-formatted output (the names of the columns are shifted)
When I copy from the console of RStudio, it works though.
df <- data.frame(Column1 = c(0.2222, 0.1), Column2 = c("Valeur3", "Valeur4"))
print(df)
Column1 Column2
1 0.2222 Valeur3
2 0.1000 Valeur4
I guess your problem is that you are using default fonts in your email client. Most of such fonts are proportional.
See the screenshot of your sample pasted into text processor with default (proportional) font:
For code, scripts and alike text it is necessary to use so called monospace fonts that have save width of symbols. Such fonts are used by stackowerflow when text is formatted as code:
Proportional fonts are used by Stackowerflow for plain text:
Column1 Column2
1 0.2222 Valeur3
2 0.1000 Valeur4
See here the screenshot from the same text processor with monospace font set:
It is exactly the same clipboard content pasted into the same window. The text copied from RStudio console is plain text and so when pasted the text processor sets the last or default one that is commonly proportional (the letters have different widths depending upon optical density of symbols). You can select a font of your choice. See the list for instance at Wikipedia