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

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)

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.

Ignore regular expression in R with gsub [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
How to escape a question mark in R?
(4 answers)
Closed 3 years ago.
df <- data.frame(
videos = c("Moon vs Grubby", "Moon vs Happy", "Happy vs Th00"),
links = c("https://www.youtube.com/watch?v=QlNc-jb4ESk&t", "https://www.youtube.com/watch?v=VESO8YQVFSE", "https://www.youtube.com/watch?v=RI3IJT8ZzBM")
)
df$links <- as.character(df$links)
df$links <- gsub("watch?v=", "embed/", df$links)
I have got the following code with links to YouTube which I want to embed in a shiny App. However YouTube needs to replace part of the string which is interpreted as a regular expression. I did not find a helpful solution here.
So how can I gsub this pattern?
Current Links:
https://www.youtube.com/watch?v=QlNc-jb4ESk&t
Expected Outcome:
https://www.youtube.com/embed/=QlNc-jb4ESk&t
We need to escape the ? and = as these are metacharacters
gsub("watch\\?v\\=", "embed/=", df$links)
or with fixed = TRUE
gsub("watch?v=", "embed/=", df$links, fixed = TRUE)
Also, as there is only a single instance, we can use sub
sub("watch?v=", "embed/=", df$links, fixed = TRUE)
#[1] "https://www.youtube.com/embed/=QlNc-jb4ESk&t"
#[2] "https://www.youtube.com/embed/=VESO8YQVFSE"
#[3] "https://www.youtube.com/embed/=RI3IJT8ZzBM"
My guess is that this expression might work:
(\S*)watch\?v=(\S*)
The expression is explained on the top right panel of this demo, if you wish to explore further or modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
and our code might look like:
gsub("(\\S*)watch\\?v\\=(\\S*)", "\\1embed/\\2", df$links)
My guess is that this would be the desired output:
https://www.youtube.com/embed/QlNc-jb4ESk&t

Simplifying characters with ornaments in R [duplicate]

This question already has answers here:
Replace multiple letters with accents with gsub
(11 answers)
Closed 5 years ago.
I have the names of some music artists which I am working with within the Spotify API. I'm having some issues dealing with some strings because of the characters' accents. I don't have much understanding of character encoding.
I'll provide more context a bit further below, but essentially I am wondering if there is a way in R to "simplify" characters with ornaments.
Essentially, I am interested if there is a function which will take c("ë", "ö") as an input, and return c("e", "o"), removing the ornaments from the characters.
I don't think I can create a reproducible example because of the issues with API authentication, but for some context, when I try to run:
artistName <- "Tiësto"
GET(paste0("https://api.spotify.com/v1/search?q=",
artistName,
"&type=artist"),
config(token = token))
The following gets sent to the API:
https://api.spotify.com/v1/search?q=Tiësto&type=artist
Returning me a 400 bad request error. I am trying to alter the strings I pass to the GET function so I can get some useful output.
Edit: I am not looking for a gsub type solution, as that relies on me anticipating the sorts of accented characters which might appear in my data. I'm interested whether there is a function already out there which does this sort of translation between different character encodings.
Here is what I found, and may work for you. Simpler and convenient to apply on any form of data.
> artistName <- "Tiësto"
> iconv(artistName, "latin1", "ASCII//TRANSLIT")
[1] "Tiesto"
Based on the answers to this question , you could do this:
artistName <- "Tiësto"
removeOrnaments <- function(string) {
chartr(
"ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ",
"SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy",
string
)
}
removeOrnaments(artistName)
# [1] "Tiesto"

Formatting R console output [duplicate]

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

Operation overloading in R [duplicate]

This question already has answers here:
Making a string concatenation operator in R
(5 answers)
Closed 8 years ago.
What's the most straight forward way of overloading '+' for characters?
I have defined '%+%' <- function(...) paste(...,sep=""):
str <- "aa"%+%"bb"%+%"cc" #str="aabbcc"
But I don't like the syntax. I think str <- "aa"+"bb"+"cc" would be nicer.
(I am building long SQL queries to use with RODBC, the usual paste is not very handy in such situations. Any suggestions?)
You may try something like that :
R> oldplus <- `+`
R> `+` <- function(e1, e2) {
R> if (is.character(e1) && is.character(e2)) {
R> paste(e1,e2,sep="")
R> }
R> else {
R> oldplus(e1,e2)
R> }
R> }
Which gives :
R> 2+3
[1] 5
R> "aa"+"bb"
[1] "aabb"
But as Sacha pointed out, overloading such a basic function is very dangerous, and I can't assure you it will not break your R session and make your computer explode :-)
I think that using two arguments is better than the dots:
'%+%' <- function(x,y) paste(x,y,sep="")
"a"%+%"b"%+%"C"
[1] "abC"
If you really really want to you can overwrite +, but be veeeeery careful when doing this as you will break one of the most important functions in R. I can't think of any reason why you would want to do that over %+%:
# '+' <- function(x,y) paste(x,y,sep="")
# "a"+"b"+"C"
# [1] "abC"
rm('+')
commented it out to be sure I don't accidently break someones R:)
Why is the usual 'paste' not very handy? It's what it's meant for. Suggestions:
Write yourself an unusual paste function that does what you want. Maybe you just don't like typing 'sep=""' all the time. So write a function that calls paste with sep="". Or whatever.
Building long SQL queries with string concatenation is potential fail anyway. See http://xkcd.com/327/ for the canonical example.
Another possibility is some kind of templating solution. I've used the brew package in the past and it's great for that.
You can find this operator in stringi package.
http://docs.rexamine.com/R-man/stringi/oper_plus.html

Resources