mages / googleVis

Interface between R and the Google Chart Tools
https://mages.github.io/googleVis/
358 stars 156 forks source link

Adding wordtree? #75

Closed DataStrategist closed 6 years ago

DataStrategist commented 6 years ago

Hey Markus,

As a feature request, any chance you'll be bringing this into the package?

https://developers.google.com/chart/interactive/docs/gallery/wordtree

It looks pretty awesome!

mages commented 6 years ago

Hi Amit,

The Word Tree chart works a little different to the other chart types and would require a bit more work. Unfortunately, I haven’t got the time for this at the moment.

Thanks

Markus

On 1 Mar 2018, at 09:05, Amit Kohli notifications@github.com wrote:

Hey Markus,

As a feature request, any chance you'll be bringing this into the package?

https://developers.google.com/chart/interactive/docs/gallery/wordtree https://developers.google.com/chart/interactive/docs/gallery/wordtree It looks pretty awesome!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mages/googleVis/issues/75, or mute the thread https://github.com/notifications/unsubscribe-auth/ABF-1UCGGXacI9lkBHgd4ZDETnI4RGuXks5tZ7npgaJpZM4SX-Bd.

DataStrategist commented 6 years ago

I came up w/ a super hacky version, using paste all over the place, but it takes a vector of character strings, target word, whether it should be left/mid/right and creates one. would you like me to PR it here? Or should I release it as it's own little package since it's so hacky? 😄

Sorry no documentation or comments... just whipped it up real quick

### Make wordtree
library(tidyverse)
wordtree <- function(text,targetWord,direction,Number_words,fileName){
  Number_words <- Number_words -1
  text <-   text %>% gsub(pattern = "[^a-zA-Z ]","",x=.) %>% 
    gsub(pattern = "http[a-zA-Z]+","",x=.,perl = T)

  S <- "( ?\\w+ ?)"

  if(direction=="double"){
    RE <- paste0("(\\S+\\s+|^)",
                paste(rep(S,Number_words),collapse=""),
                targetWord,
                paste(rep(S,Number_words),collapse=""),
                "(\\s+\\S+|$)")
  } else if(direction=="suffix"){
    RE <- paste0(
      targetWord,paste(rep(S,Number_words),collapse=""),
      "(\\s+\\S+|$)")

  } else if(direction=="prefix"){
    RE <- paste0("(\\S+\\s+|^)",paste(rep(S,Number_words),collapse=""),targetWord)

  }

  x <- stringr::str_extract(text,RE) %>% keep(!is.na(.))

  x <- paste(x,collapse="'],['")
  x <- paste0("['",x,"']")  

  top= "<html>
    <head>
      <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>
      <script type=\"text/javascript\">
        google.charts.load('current', {packages:['wordtree']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {
          var data = google.visualization.arrayToDataTable(
            [ ['Phrases'],"

  bottom <- paste0("]
          );

          var options = {
            wordtree: {
              format: 'implicit',
              word: '",targetWord,"',
              type: '",direction,"'
            }
          };

          var chart = new google.visualization.WordTree(document.getElementById('wordtree_basic'));
          chart.draw(data, options);
        }
      </script>
    </head>
    <body>
      <div id=\"wordtree_basic\" style=\"width: 900px; height: 500px;\"></div>
    </body>
  </html>")

  cat(top,x,bottom,file=fileName)
}

## Example
sample <- c("cats are better than dogs",
            "cats eat kibble",
            "cats are better than hamsters",
            "cats are awesome",
            "cats are people too",
            "cats eat mice",
            "cats meowing",
            "cats in the cradle",
            "cats eat mice",
            "cats in the cradle lyrics",
            "cats eat kibble",
            "cats for adoption",
            "cats are family",
            "cats eat mice",
            "cats are better than kittens",
            "cats are evil",
            "cats are weird",
            "cats eat mice")

wordtree(text=sample,targetWord = "cats",direction="suffix",Number_words = 4,fileName="thingie.html")
browseURL("thingie.html")

wordtree(text=sample,targetWord = "are",direction="double",Number_words=3,fileName="thingie.html")
browseURL("thingie.html")
DataStrategist commented 6 years ago

OK I'll release it myself. :)

ashbaldry commented 5 years ago

Hi @mages, I've been looking at the word tree recently and have got the implicit version to work with the same sort of framework as the rest of the gvis functions. Seems to work quite well. Using the data from above:

data <- data.frame(TEXT = sample)
plot(gvisWordTree(data, textvar = "TEXT", options = list(wordtree = "{word: 'cats'}")))

image

Am happy to write the documentation for it (and make sure it works for the explicit version) if you want to include it in the package.

mages commented 5 years ago

Cool, that would be much appreciated.

On 10 May 2019, at 11:32, Ashley Baldry notifications@github.com wrote:

Hi @mages, I've been looking at the word tree recently and have got the implicit version to work with the same sort of framework as the rest of the gvis functions. Seems to work quite well. Using the data from above:

data <- data.frame(TEXT = sample) plot(gvisWordTree(data, textvar = "TEXT", options = list(wordtree = "{word: 'cats'}")))

Am happy to write the documentation for it (and make sure it works for the explicit version) if you want to include it in the package.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.