Color highlighting text in R for a pre-defined list of words
Suppose I have a collection of documents such as:
text = c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
I am wondering whether it is possible to highlight documents (particularly for a large corpus) with colors for a pre-defined list of words using R. Each word in the list will get a specific color. For example, highlighting "words" to be red and "text" to be blue as shown below.
r text highlight word
add a comment |
Suppose I have a collection of documents such as:
text = c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
I am wondering whether it is possible to highlight documents (particularly for a large corpus) with colors for a pre-defined list of words using R. Each word in the list will get a specific color. For example, highlighting "words" to be red and "text" to be blue as shown below.
r text highlight word
2
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41
add a comment |
Suppose I have a collection of documents such as:
text = c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
I am wondering whether it is possible to highlight documents (particularly for a large corpus) with colors for a pre-defined list of words using R. Each word in the list will get a specific color. For example, highlighting "words" to be red and "text" to be blue as shown below.
r text highlight word
Suppose I have a collection of documents such as:
text = c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
I am wondering whether it is possible to highlight documents (particularly for a large corpus) with colors for a pre-defined list of words using R. Each word in the list will get a specific color. For example, highlighting "words" to be red and "text" to be blue as shown below.
r text highlight word
r text highlight word
asked Nov 22 '18 at 5:00
Sam SSam S
849
849
2
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41
add a comment |
2
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41
2
2
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41
add a comment |
2 Answers
2
active
oldest
votes
This is a somewhat hackish solution to this question and not very scalable for large corpus. I will be curious to see if there is a much more parsimonious, elegant, and scalable way to do this.
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
P.S. Unfortunately, reprex
doesn't work with colored text, so can't produce the complete reprex.
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
add a comment |
Indrajeet's asnwer is great. This is an answer based on Indrajeet's answer, just a little bit change.
unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
Having the output in two different lines (Collapse text by group in data frame):
df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]
The answer looks Ok if I wanted to print it in R console. How it works if I want to have the output in R shinyapp?
Looking for other alternatives and appreciate your help.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424164%2fcolor-highlighting-text-in-r-for-a-pre-defined-list-of-words%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a somewhat hackish solution to this question and not very scalable for large corpus. I will be curious to see if there is a much more parsimonious, elegant, and scalable way to do this.
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
P.S. Unfortunately, reprex
doesn't work with colored text, so can't produce the complete reprex.
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
add a comment |
This is a somewhat hackish solution to this question and not very scalable for large corpus. I will be curious to see if there is a much more parsimonious, elegant, and scalable way to do this.
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
P.S. Unfortunately, reprex
doesn't work with colored text, so can't produce the complete reprex.
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
add a comment |
This is a somewhat hackish solution to this question and not very scalable for large corpus. I will be curious to see if there is a much more parsimonious, elegant, and scalable way to do this.
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
P.S. Unfortunately, reprex
doesn't work with colored text, so can't produce the complete reprex.
This is a somewhat hackish solution to this question and not very scalable for large corpus. I will be curious to see if there is a much more parsimonious, elegant, and scalable way to do this.
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
P.S. Unfortunately, reprex
doesn't work with colored text, so can't produce the complete reprex.
answered Nov 22 '18 at 5:55
Indrajeet PatilIndrajeet Patil
1,535313
1,535313
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
add a comment |
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
Thanks Indrajeet. yes, it does not produce the complete one, but good work!
– Sam S
Nov 22 '18 at 22:07
add a comment |
Indrajeet's asnwer is great. This is an answer based on Indrajeet's answer, just a little bit change.
unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
Having the output in two different lines (Collapse text by group in data frame):
df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]
The answer looks Ok if I wanted to print it in R console. How it works if I want to have the output in R shinyapp?
Looking for other alternatives and appreciate your help.
add a comment |
Indrajeet's asnwer is great. This is an answer based on Indrajeet's answer, just a little bit change.
unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
Having the output in two different lines (Collapse text by group in data frame):
df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]
The answer looks Ok if I wanted to print it in R console. How it works if I want to have the output in R shinyapp?
Looking for other alternatives and appreciate your help.
add a comment |
Indrajeet's asnwer is great. This is an answer based on Indrajeet's answer, just a little bit change.
unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
Having the output in two different lines (Collapse text by group in data frame):
df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]
The answer looks Ok if I wanted to print it in R console. How it works if I want to have the output in R shinyapp?
Looking for other alternatives and appreciate your help.
Indrajeet's asnwer is great. This is an answer based on Indrajeet's answer, just a little bit change.
unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
Having the output in two different lines (Collapse text by group in data frame):
df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]
The answer looks Ok if I wanted to print it in R console. How it works if I want to have the output in R shinyapp?
Looking for other alternatives and appreciate your help.
edited Nov 22 '18 at 23:57
answered Nov 22 '18 at 22:29
Sam SSam S
849
849
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424164%2fcolor-highlighting-text-in-r-for-a-pre-defined-list-of-words%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
This would be easy if you were going to save the text to a HTML file or something. Where are you hoping to save the results, and where should the colours be viewable?
– Marius
Nov 22 '18 at 5:19
Not within R itself, but the R2wd and officer packages are good for formatting things to a word document, then the knittr and markdown ones are good for making a html.
– RAB
Nov 22 '18 at 5:54
Marius - I am using R shinyapp and the results will be demonstrated in R shinyapp using DataTables.
– Sam S
Nov 22 '18 at 6:41