Formatting R console output [duplicate] - r

This question already has answers here:
Remove quotes from a character vector in R
(11 answers)
Closed 6 years ago.
I would like to be able to copy and paste the output in R console without always having to remove these tags and quotes
print('love')
[1] "love"
What I require
print('love')
love
Note: I also want to remove the [1] not only the double quotes.

You could use the cat function, it will indeed print your string without parenthesis:
cat('love')
#### love
See the help page ?cat
Outputs the objects, concatenating the representations. cat performs
much less conversion than print.
You might also use print with the quote argument:
print("love", quote=FALSE)
#### [1] love
That way, you still get the [1]
See also this thread: https://stackoverflow.com/a/5218361/3871924

Related

Replace latex with r strings using gsub [duplicate]

This question already has an answer here:
"'\w' is an unrecognized escape" in grep
(1 answer)
Closed 1 year ago.
I would like to find and replace tabular instances by tabularx. I tried with gsub but it seems to enter me into a world of escaping pain. Following other questions and answers I find fixed=TRUE which is the best I so far have. The code snippet below almost works, \B is unrecognized. If I escape it twice I get \BEGIN as output!
texText <- '\begin{tabular}{rl}\begin{tabular}{rll}'
texText <- gsub("\begin{tabular}{rl}", "\BEGIN{tabular}{rll}", texText, fixed=TRUE)
I'm using BEGIN as my test to see what is happening. This is before I get to tackling the question of what goes on in the brackets {rl} {ll} {rrl} etc. Ideally I'm looking for a regex that would output:
\begin{tabularx}{rX}\begin{tabularx}{rlX}
That is the final column is replaced by X.
Try using proper escaping:
texText <- "\begin{tabular}{rl}\begin{tabular}{rll}"
output <- gsub("\begin\\{tabular\\}", "\begin{tabularx}", texText)
output
[1] "\begin{tabularx}{rl}\begin{tabularx}{rll}"
A literal backslash requires two backslashes, and also metacharacters such as { and } require two backslashes.

How do I remove the extraneous printing of [1] in R? [duplicate]

This question already has answers here:
Output in R, Avoid Writing "[1]"
(4 answers)
R output without [1], how to nicely format?
(1 answer)
Closed 3 years ago.
Consider the following example:
library(digest)
hash <- digest("hello world", algo="md5", serialize=F)
hash
produces [1] "5eb63bbbe01eeed093cb22bb8f5acdc3"
For my purposes, I only want the raw string output with no embellishments or extras. The objective is to alter the script so it produces 5eb63bbbe01eeed093cb22bb8f5acdc3.
I've spent over an hour looking for any way to get rid of the [1] and the documentation has been absolutely terrible. Most of the search results are manipulation, clickbait, wrong, or scams.
Array indexing doesn't work:
hash[1]
produces [1] "5eb63bbbe01eeed093cb22bb8f5acdc3", because apparently an array is the first element of itself which makes no programmatic sense whatsoever.
typeof(hash)
produces [1] "character". Really?
substr(hash[1], 4, 1000)
produces [1] "63bbbe01eeed093cb22bb8f5acdc3".
How do I just make that [1] and preferably the quotes as well go away? There's absolutely no instructions searchable on the web as far as I know.
More generally, I'd like a function or procedure to convert anything to a string for manipulation and post-processing.
library(digest)
hash <- digest("hello world", algo="md5", serialize=F)
cat(hash)

How to automatically handle strings/paths with backslashes? [duplicate]

This question already has answers here:
How to escape backslashes in R string
(3 answers)
Efficiently convert backslash to forward slash in R
(11 answers)
Closed 3 years ago.
I often want to read in csv files and I get the path by using shift + right click and then clicking "copy path".
I paste this path into my code. See an example below:
read_csv("C:\Users\me\data\file.csv")
Obviously this doesn't work because of the backslashes. My current solution is to escape each one, so that my code looks like this:
read_csv("C:\\Users\\me\\data\\file.csv")
It works, but it's annoying and occasionally I'll get errors because I missed one of the backslashes.
I wanted to create a function automatically adds the extra slashes
fix_path <- function(string) str_replace(string, "\\\\", "\\\\\\\\")
but R won't recognize the string in the first place until the backslashes are taken care of.
Is there another way to deal with this? Python has the option of adding an "r" before strings to note that the backslashes should be treated just as regular backslashes, is there anything similar in R? To be clear, I know that I can escape the backslashes, but I am looking for a way to do it automatically.
You can use this hack. Suppose you had copied your path as mentioned then you could use
scan("clipboard", "character", quiet = TRUE)
scan reads the text copied from the clipboard and takes care about the backslashes. Then copy again what is returned from scan

How to put \' in my string using paste0 function [duplicate]

This question already has answers here:
How to escape backslashes in R string
(3 answers)
Closed 5 years ago.
I have an array:
t <- c("IMCR01","IMFA02","IMFA03")
I want to make it look like this:
"\'IMCR01\'","\'IMFA02\'","\'IMFA03\'"
I tried different ways like:
paste0("\'",t,"\'")
paste0("\\'",t,"\\'")
paste0("\\\\'",t,"\\\\'")
But none of them is correct. Any other functions are OK as well.
Actually your second attempt is correct:
paste0("\\'",t,"\\'")
If you want to tell paste to use a literal backslash, you need to escape it once (but not twice, as you would need within a regex pattern). This would output the following to the console in R:
[1] "\\'IMCR01\\'" "\\'IMFA02\\'" "\\'IMFA03\\'"
The trick here is that the backslash is even being escaped by R in the console output. If you were instead to write t to a text file, you would only see a single backslash as you wanted:
write(t, file = "/path/to/your/file.txt")
But why does R need to escape backslash when writing to its own console? One possibility is that if it were to write a literal \n then this would actually be interpreted by the console as a newline. Hence the need for eacaping is still there.

How to paste special characters in R [duplicate]

This question already has an answer here:
paste quotation marks into character string, within a loop
(1 answer)
Closed 5 years ago.
I have started learning R and am trying to create vector as below:
c(""check"")
I need the output as : "check". But am getting syntax error. How to escape the quotes while creating a vector?
As #juba mentioned, one way is directly escaping the quotes.
Another way is to use single quotes around your character expression that has double quotes in it.
> x <- 'say "Hello!"'
> x
[1] "say \"Hello!\""
> cat(x)
say "Hello!"
Other answers nicely show how to deal with double quotes in your character strings when you create a vector, which was indeed the last thing you asked in your question. But given that you also mentioned display and output, you might want to keep dQuote in mind. It's useful if you want to surround each element of a character vector with double quotes, particularly if you don't have a specific need or desire to store the quotes in the actual character vector itself.
# default is to use "fancy quotes"
text <- c("check")
message(dQuote(text))
## “check”
# switch to straight quotes by setting an option
options(useFancyQuotes = FALSE)
message(dQuote(text))
## "check"
# assign result to create a vector of quoted character strings
text.quoted <- dQuote(text)
message(text.quoted)
## "check"
For what it's worth, the sQuote function does the same thing with single quotes.
Use a backslash :
x <- "say \"Hello!\""
And you don't need to use c if you don't build a vector.
If you want to output quotes unescaped, you may need to use cat instead of print :
R> cat(x)
say "Hello!"

Resources