Color highlighting text in R for a pre-defined list of words












1















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.



enter image description here










share|improve this question


















  • 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
















1















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.



enter image description here










share|improve this question


















  • 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














1












1








1


1






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.



enter image description here










share|improve this question














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.



enter image description here







r text highlight word






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















2














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))


enter image description here



P.S. Unfortunately, reprex doesn't work with colored text, so can't produce the complete reprex.






share|improve this answer
























  • Thanks Indrajeet. yes, it does not produce the complete one, but good work!

    – Sam S
    Nov 22 '18 at 22:07



















0














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)


enter image description here



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]


enter image description here



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.






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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))


    enter image description here



    P.S. Unfortunately, reprex doesn't work with colored text, so can't produce the complete reprex.






    share|improve this answer
























    • Thanks Indrajeet. yes, it does not produce the complete one, but good work!

      – Sam S
      Nov 22 '18 at 22:07
















    2














    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))


    enter image description here



    P.S. Unfortunately, reprex doesn't work with colored text, so can't produce the complete reprex.






    share|improve this answer
























    • Thanks Indrajeet. yes, it does not produce the complete one, but good work!

      – Sam S
      Nov 22 '18 at 22:07














    2












    2








    2







    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))


    enter image description here



    P.S. Unfortunately, reprex doesn't work with colored text, so can't produce the complete reprex.






    share|improve this answer













    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))


    enter image description here



    P.S. Unfortunately, reprex doesn't work with colored text, so can't produce the complete reprex.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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)


    enter image description here



    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]


    enter image description here



    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.






    share|improve this answer






























      0














      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)


      enter image description here



      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]


      enter image description here



      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.






      share|improve this answer




























        0












        0








        0







        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)


        enter image description here



        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]


        enter image description here



        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.






        share|improve this answer















        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)


        enter image description here



        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]


        enter image description here



        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 23:57

























        answered Nov 22 '18 at 22:29









        Sam SSam S

        849




        849






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules