diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 0b9a44b..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.Rbuildignore b/.Rbuildignore index 25b971e..1a4671a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,2 +1,3 @@ ^hgwordcloud\.Rproj$ ^\.Rproj\.user$ +^LICENSE\.md$ diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index 912423b..0000000 --- a/.Rhistory +++ /dev/null @@ -1,512 +0,0 @@ -library(textclean) -library(stringr) -library(quanteda) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando tm -corpus <- Corpus(VectorSource(texto)) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- removePunctuation(texto) # Eliminar puntuación -texto <- removeNumbers(texto) # Eliminar números -texto <- stripWhitespace(texto) # Eliminar espacios en blanco extra -texto <- removeWords(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- str_replace_all(texto, "http\\S+|www\\S+", "") # Eliminar URLs -return(texto) -} -# Aplicar la función de limpieza al corpus -corpus <- tm_map(corpus, content_transformer(limpiar_texto)) -# Ver el texto limpio -inspect(corpus) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando tm -corpus <- Corpus(VectorSource(texto)) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- removePunctuation(texto) # Eliminar puntuación -texto <- removeNumbers(texto) # Eliminar números -texto <- stripWhitespace(texto) # Eliminar espacios en blanco extra -texto <- removeWords(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- str_replace_all(texto, "http\\S+|www\\S+", "") # Eliminar URLs -return(texto) -} -limpiar_texto -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -library(quanteda) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visítame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Crear una función de limpieza de texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, pattern = stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- tokens_limpios %>% -tokens_wordstem(language = "spanish") %>% # Stemming de palabras -dfm() %>% -convert(to = "data.frame") -# Mostrar el texto limpio -print(texto_limpio) -tokens_limpios %>% -tokens_wordstem(language = "spanish") -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -library(tidyr) -library(highcharter) -crear_grafico <- function(datos, tooltip) { -highchart() |> -hc_chart(type = "bar") |> -hc_title(text = "Gráfico de Barras Horizontal") |> -hc_yAxis_multiples( -list( -title = list(text = "Valor 1") -), -list( -title = list(text = "Valor 2"), -opposite = TRUE -) -)|> -hc_xAxis(categories = datos$categoria) |> -hc_add_series(name = "Valor 1", data = datos$valor1, yAxis = 0) |> -hc_add_series(name = "Valor 2", data = datos$valor2, yAxis = 1) |> -hc_tooltip(formatter = JS(sprintf("function() { return %s; }", tooltip)), -shared = FALSE, -useHTML = TRUE) -} -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -library(quantmod) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_remove(texto, pattern = "http\\S+|www\\S+") # Eliminar URLs -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -crear_grafico <- function(datos, tooltip) { -highchart() |> -hc_chart(type = "bar") |> -hc_title(text = "Gráfico de Barras Horizontal") |> -hc_yAxis_multiples( -list( -title = list(text = "Valor 1"), -opposite = TRUE -), -list( -title = list(text = "Valor 2") -) -)|> -hc_xAxis(categories = datos$categoria) |> -hc_add_series(name = "Valor 1", data = datos$valor1, yAxis = 0) |> -hc_add_series(name = "Valor 2", data = datos$valor2, yAxis = 1) |> -hc_tooltip(formatter = JS(sprintf("function() { return %s; }", tooltip)), -shared = FALSE, -useHTML = TRUE) -} -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -crear_grafico <- function(datos, tooltip) { -highchart() |> -hc_chart(type = "bar") |> -hc_title(text = "Gráfico de Barras Horizontal") |> -hc_yAxis_multiples( -list( -title = list(text = "Valor 1"), -opposite = TRUE -), -list( -title = list(text = "Valor 2") -) -)|> -hc_xAxis(categories = datos$categoria) |> -hc_add_series(name = "Valor 1", data = datos$valor1, yAxis = 0) |> -hc_add_series(name = "Valor 2", data = datos$valor2, yAxis = 1) |> -hc_tooltip(formatter = JS(sprintf("function() { return %s; }", tooltip)), -shared = FALSE, -useHTML = TRUE) -} -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -library(highcharter) -library(tidyr) -crear_grafico <- function(datos, tooltip) { -highchart() |> -hc_chart(type = "bar") |> -hc_title(text = "Gráfico de Barras Horizontal") |> -hc_yAxis_multiples( -list( -title = list(text = "Valor 1"), -opposite = TRUE -), -list( -title = list(text = "Valor 2") -) -)|> -hc_xAxis(categories = datos$categoria) |> -hc_add_series(name = "Valor 1", data = datos$valor1, yAxis = 0) |> -hc_add_series(name = "Valor 2", data = datos$valor2, yAxis = 1) |> -hc_tooltip(formatter = JS(sprintf("function() { return %s; }", tooltip)), -shared = FALSE, -useHTML = TRUE) -} -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -library(highcharter) -library(tidyr) -crear_grafico <- function(datos, tooltip) { -highchart() |> -hc_chart(type = "bar") |> -hc_title(text = "Gráfico de Barras Horizontal") |> -hc_yAxis_multiples( -list( -title = list(text = "Valor 1"), -opposite = TRUE -), -list( -title = list(text = "Valor 2") -) -)|> -hc_xAxis(categories = datos$categoria) |> -hc_add_series(name = "Valor 1", data = datos$valor1, yAxis = 0) |> -hc_add_series(name = "Valor 2", data = datos$valor2, yAxis = 1) |> -hc_tooltip(formatter = JS(sprintf("function() { return %s; }", tooltip)), -shared = FALSE, -useHTML = TRUE) -} -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 70, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -datos <- data.frame( -categoria = c("A", "B", "C", "D"), -valor1 = c(10, 20, 30, 40), -valor2 = c(15, 25, 35, 45) -) -tooltip_text <- "'Categoría: ' + this.point.category + '
Valor : ' + this.point.y " -crear_grafico(datos, tooltip_text) -library(quanteda) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -tokens_remove(texto, c(stopwords("es"))) -tokens_remove(texto, c(stopwords("spanish"))) -tokens_remove(texto, stopwords("spanish")) -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -tokens_remove(texto, stopwords("spanish")) -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto -# Ejemplo de texto en español -texto <- c("¡Hola! ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -tokens_remove(texto, stopwords("spanish")) -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -# Ejemplo de texto en español -texto <- c("¡Hola! hola ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!") -tokens_remove(texto, stopwords("spanish")) -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -# Ejemplo de texto en español -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!", -"chaooo, a todos") -) -tokens_remove(texto, stopwords("spanish")) -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto) -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto$text) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -# Ejemplo de texto en español -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!", -"chaooo, a todos los habitantes de la tierra") -) -tokens_remove(texto, stopwords("spanish")) -# Crear un corpus usando quanteda -corpus <- corpus(texto) -# Función para limpiar texto -limpiar_texto <- function(texto) { -texto <- tolower(texto) # Convertir a minúsculas -texto <- tokens(texto, remove_punct = TRUE) # Eliminar puntuación -texto <- tokens_remove(texto, stopwords("spanish")) # Eliminar palabras vacías (stopwords) en español -texto <- tokens_tolower(texto) # Convertir a minúsculas -return(texto) -} -# Aplicar la función de limpieza al corpus -tokens_limpios <- limpiar_texto(texto$text) -# Convertir los tokens a texto limpio -texto_limpio <- as.character(tokens_limpios) -print(texto_limpio) -devtools::load_all(".") -# Ejemplo de texto en español -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!", -"chaooo, a todos los habitantes de la tierra") -) -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -?hcaes -devtools::load_all(".") -?hcaes -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -data_viz$..colors <- "red" -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -aggregate_data -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -aggregate_data -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", tooltip_template = "hola") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text", title="hola") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all() -devtools::document() -devtools::install() -devtools::load_all(".") -# Ejemplo de texto en español -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo estás? Este es un ejemplo de texto en español. Visitame en www.ejemplo.com. Gracias!!!", -"chaooo, a todos los habitantes de la tierra") -) -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -usethis::use_test("hg-word-cloud") -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo están? este es un mensaje de saludo desde la galaxia andromeda. un test de comunicación!!!", -"respondan, chaooo, a todos los habitantes de la tierra") -) -hg_word_cloud(texto, var_cat = "text") -devtools::load_all(".") -hg_word_cloud(texto, var_cat = "text") -devtools::load_all() -devtools::document() -devtools::load_all() -devtools::document() -devtools::install() -texto <- data.frame(text = -c("¡Hola! hola ¿Cómo están? este es un mensaje de saludo desde la galaxia andromeda. un test de comunicación!!!", -"respondan, chaooo, a todos los habitantes de la tierra") -) -hg_word_cloud(texto, var_cat = "text") diff --git a/DESCRIPTION b/DESCRIPTION index b1285c3..b0f1e22 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,18 +1,31 @@ Package: hgwordcloud -Title: What the Package Does (One Line, Title Case) -Version: 0.0.0.9000 -Authors@R: - person("First", "Last", , "first.last@example.com", role = c("aut", "cre"), - comment = c(ORCID = "YOUR-ORCID-ID")) -Description: What the package does (one paragraph). -Depends: - quanteda, - highcharter, +Title: Highcharter Word Cloud Visualization Wrapper +Version: 0.0.1 +Authors@R: c( + person("Datasketch", , , "hi@datasketch.co", role = c("aut", "cre", "cph")), + person("Camila", "Achuri", , "camila@datasketch.co", role = "aut"), + person("Daniel", "Delgado", , "daniel.delgado@datasketch.co", role = "aut") + ) +Description: A highcharter-based word cloud visualization package that + creates interactive and customizable word clouds from text data. It + integrates with Datasketch's data processing pipeline, and offers + extensive customization options through dsopts. +License: MIT + file LICENSE +Imports: + dplyr, dsdatawiz, - R (>= 3.0.0) -Encoding: UTF-8 -Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 -Suggests: + dsopts, + hdtable, + highcharter, + purrr, + quanteda +Suggests: testthat (>= 3.0.0) +Remotes: + datasketch/dsdatawiz, + datasketch/dsopts, + datasketch/hdtable Config/testthat/edition: 3 +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.2 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1606ddc --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2025 +COPYRIGHT HOLDER: hgwordcloud authors diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..810be11 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2025 hgwordcloud authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NAMESPACE b/NAMESPACE index 886e567..83f7144 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,4 @@ # Generated by roxygen2: do not edit by hand -export(hc_titles) export(hg_word_cloud) export(hg_word_cloud_Cat) -export(hg_word_cloud_CatNum) diff --git a/R/hg-word-cloud.R b/R/hg-word-cloud.R index 8e8ffaa..2479a8a 100644 --- a/R/hg-word-cloud.R +++ b/R/hg-word-cloud.R @@ -1,55 +1,79 @@ +#' Create a word cloud visualization +#' +#' @description +#' Generates a word cloud using highcharter. +#' +#' @param data A data frame containing the text data to visualize. +#' @param dic Optional. A data dictionary for variable mapping and formatting. +#' @param var_cat Character. The name of the categorical variable containing the +#' text data. +#' @param var_num Optional. The name of the numerical variable to use for word +#' sizing. +#' @param ... Additional \code{dsopts} arguments passed for customization, +#' for example: +#' \itemize{ +#' \item \code{title}: Main title of the visualization +#' \item \code{subtitle}: Subtitle text +#' \item \code{caption}: Caption text +#' } +#' +#' @return A highcharter object containing the word cloud visualization. +#' +#' @examples +#' \dontrun{ +#' # Basic word cloud from text data +#' data <- data.frame(text = c("word1", "word2", "word3", "word1")) +#' hg_word_cloud(data, var_cat = "text") +#' +#' # Word cloud with frequency +#' data <- data.frame( +#' text = c("word1", "word2", "word3"), +#' freq = c(10, 5, 3) +#' ) +#' hg_word_cloud(data, var_cat = "text", var_num = "freq") +#' } +#' #' @export -hg_word_cloud <- function(data, - dic = NULL, - clean_text = TRUE, - var_cat = NULL, - var_num = NULL, ...) { +hg_word_cloud <- function(data, dic = NULL, + var_cat = NULL, + var_num = NULL, + ...) { + if (is.null(data)) stop("Data must be provided.") + if (is.null(var_cat)) stop("var_cat must be provided.") + data <- data |> + dplyr::select(dplyr::any_of(var_cat), dplyr::any_of(var_num)) - if (is.null(var_cat)) return(stop("You must indicate the name of the column with text in var_cat")) - dic <- NULL - opts_theme <- dsopts_merge(..., categories = "theme") - if (clean_text) { - data <- data.frame(words = clean_text(data[[var_cat]])) - names(data) <- var_cat - } + if (is.null(var_num)) { + data <- data |> + dplyr::select(dplyr::all_of(var_cat)) |> + purrr::map(function(x) text_prep(x, ...)) |> + unlist(use.names = FALSE) - ht <- hdtable(data, dic) - data_viz <- data_prep(ht$data, ht$dic, var_cat, var_num, ...) - names(data_viz)[1:2] <- c("word", "frequency") - color_by <- "word" - data_viz <- colors_data(data_viz, color_by = color_by, ...) - - if (nrow(data_viz) > 500) { - data_viz <- data_viz |> dplyr::arrange(desc(frequency)) - data_viz <- data_viz[1:500,] + data <- data.frame(word = data) + var_cat <- "word" + dic <- NULL } - # data_viz <- data_viz |> rename(label = ..labels) - if (is.null(var_num)) var_num <- "Conteo" - - hchart(data_viz, "wordcloud", - hcaes(name = word, weight = frequency), name = var_num, zoomType= 'xy') |> - hc_titles(opts = dsopts_merge(..., categories = "titles")) |> - hc_colors(data_viz$..colors) |> - # hc_tooltip(useHTML = TRUE, - # formatter = JS(paste0("function () {return this.point.label;}")) - # ) |> - hc_add_theme(hgch_theme(opts = opts_theme)) + ht <- hdtable::hdtable(data, dic) + data_viz <- ht$data |> + data_prep(ht$dic, var_cat, var_num, agg_text = "freq", ...) |> + dsdatawiz::colors_data(color_by = names(ht$data)[1], ...) + highcharter::highchart() |> + highcharter::hc_chart(type = "wordcloud") |> + highcharter::hc_add_theme(hg_theme(...)) |> + highcharter::hc_colors(data_viz$..colors) |> + highcharter::hc_series(process_wordcloud(data_viz)) |> + highcharter::hc_tooltip( + useHTML = TRUE, + formatter = highcharter::JS("function () {return this.point.label;}") + ) |> + hg_titles(...) } #' @export -hg_word_cloud_Cat <- function(data, dic = NULL, clean_text = TRUE, ...) { - vars <- data_vars(data) - hg_word_cloud(data, dic, var_cat = vars[1], clean_text = clean_text, ...) +hg_word_cloud_Cat <- function(data, dic = NULL, ...) { + hg_word_cloud(data, dic, var_cat = names(data)[1], ...) } - -#' @export -hg_word_cloud_CatNum <- function(data, dic = NULL, ...) { - vars <- data_vars(data) - hg_word_cloud(data, dic, var_cat = vars[1], var_num = vars[2], clean_text = FALSE, ...) -} - - diff --git a/R/utils-data.R b/R/utils-data.R index 1ecfe24..727fc14 100644 --- a/R/utils-data.R +++ b/R/utils-data.R @@ -1,31 +1,15 @@ -data_prep <- function(data, - dic = NULL, +#' @keywords internal +data_prep <- function(data, dic = NULL, var_group = NULL, var_num = NULL, ...) { - - - dic <- dic %||% hdtable(data)$dic - var_group <- var_group %||% default_var_group(dic) - - data <- aggregate_data(data = data, - dic = dic, - group_vars = var_group, - var_num_to_agg = var_num, ...) - data - -} - -clean_text <- function(text) { - text <- tolower(text) - text <- tokens(text, remove_punct = TRUE) - text <- tokens_remove(text, stopwords("spanish")) - text <- tokens_tolower(text) - as.character(text) -} - - -data_vars <- function(data) { - data_names <- names(hdtable(data)$data) - data_names + dic <- dic %||% hdtable::hdtable(data)$dic + + dsdatawiz::aggregate_data( + data = data, + dic = dic, + group_vars = var_group, + var_num_to_agg = var_num, + ... + ) } diff --git a/R/utils-series.R b/R/utils-series.R new file mode 100644 index 0000000..41fd3fc --- /dev/null +++ b/R/utils-series.R @@ -0,0 +1,15 @@ +#' @keywords internal +process_wordcloud <- function(data) { + list( + data = purrr::pmap( + list(data[[1]], data[[2]], data$..labels), + function(word, freq, label) { + list( + name = word, + weight = freq, + label = label + ) + } + ) + ) +} diff --git a/R/utils-text.R b/R/utils-text.R new file mode 100644 index 0000000..9b966f1 --- /dev/null +++ b/R/utils-text.R @@ -0,0 +1,26 @@ +#' @keywords internal +text_prep <- function(text, ...) { + opts <- dsopts::dsopts_merge(..., categories = "textprep") + text <- ifelse(is.na(text), "", as.character(text)) + + text <- text |> + quanteda::tokens(remove_punct = TRUE, remove_numbers = TRUE) |> + quanteda::tokens_tolower() + + if (!is.null(opts$stopwords_source) && !is.null(opts$stopwords_language)) { + text <- text |> + quanteda::tokens_remove( + quanteda::stopwords( + language = opts$stopwords_language, + source = opts$stopwords_source + ) + ) + } + + if (!is.null(opts$stopwords_custom)) { + text <- text |> + quanteda::tokens_remove(opts$stopwords_custom) + } + + as.character(text) +} diff --git a/R/utils_theme.R b/R/utils-theme.R similarity index 58% rename from R/utils_theme.R rename to R/utils-theme.R index 771e235..35cda42 100644 --- a/R/utils_theme.R +++ b/R/utils-theme.R @@ -1,68 +1,82 @@ #' @keywords internal -hgch_theme <- function(opts = NULL) { - message("in theme") +hg_theme <- function(...) { + opts <- dsopts::dsopts_merge(..., categories = "theme") highcharter::hc_theme( useHTML = TRUE, - #colors = opts$palette_colors, styledMode = TRUE, chart = list( + # Chart background backgroundColor = opts$background_color, + # Chart border + borderColor = opts$plot_border_color, + borderWidth = opts$plot_border_width, + # Chart margin marginBottom = opts$plot_margin_bottom, marginLeft = opts$plot_margin_left, marginRight = opts$plot_margin_right, marginTop = opts$plot_margin_top, - + # Plot background plotBackgroundColor = opts$plot_background_color, - borderColor = opts$plot_border_color, - borderWidth = opts$plot_border_width, + # Font style style = list ( + color = opts$text_color, fontFamily = opts$text_family, fontSize = paste0(opts$text_size, 'px') )), title = list( useHTML = TRUE, - align = opts$title_align, + # Title alignment + align = opts$title_align, # left, center, right + # Title style style = list( - fontFamily = opts$title_family, - fontSize = paste0(opts$title_size, 'px'), color = opts$title_color %||% opts$text_color, + fontFamily = opts$title_family %||% opts$text_family, + fontSize = paste0(opts$title_size %||% opts$text_size, 'px'), fontWeight = opts$title_weight ) ), subtitle = list( useHTML = TRUE, - align = opts$subtitle_align, + # Subtitle alignment + align = opts$subtitle_align, # left, center, right + # Subtitle style style = list( - fontFamily = opts$title_family %||% opts$text_family, - fontSize = paste0(opts$subtitle_size, 'px'), color = opts$subtitle_color %||% opts$text_color, + fontFamily = opts$title_family %||% opts$text_family, + fontSize = paste0(opts$subtitle_size %||% opts$text_size, 'px'), fontWeight = opts$subtitle_weight ) ), credits = list( useHTML = TRUE, + # Credits alignment href = opts$caption_link, + # Caption margin margin = opts$caption_margin, + # Caption style style = list( + color = opts$caption_color %||% opts$text_color, fontFamily = opts$caption_family %||% opts$text_family, - fontSize = paste0(opts$caption_size, 'px'), - color = opts$caption_color + fontSize = paste0(opts$caption_size %||% opts$text_size, 'px'), + fontWeight = opts$caption_weight ) ), tooltip = list( useHTML = TRUE, + # Tooltip background backgroundColor = opts$tooltip_background, + # Tooltip border borderColor = opts$tooltip_border_color, borderRadius = opts$tooltip_border_radius, borderWidth = opts$tooltip_border_width, - + # Tooltip style style = list( - width = paste0(opts$tooltip_width, "px"), - whiteSpace = 'normal', - color = opts$tooltip_text_color, + color = opts$tooltip_text_color %||% opts$text_color, fontFamily = opts$tooltip_text_family %||% opts$text_family, - fontSize = paste0(opts$text_size, 'px') + fontSize = paste0(opts$tooltip_text_size %||% opts$text_size, 'px'), + width = paste0(opts$tooltip_width, "px"), + whiteSpace = 'normal' ) ) ) diff --git a/R/utils-titles.R b/R/utils-titles.R index 8cb3a39..f4dd732 100644 --- a/R/utils-titles.R +++ b/R/utils-titles.R @@ -1,8 +1,13 @@ -#' @export -hc_titles <- function(hc, ...) { - opts <- dsopts_merge(..., categories = "titles") +#' @keywords internal +hg_titles <- function(hc, ...) { + opts <- dsopts::dsopts_merge(..., categories = "titles") + hc |> - hc_title(text = opts$title) |> - hc_subtitle(text = opts$subtitle) |> - hc_credits(enabled = opts$caption_show, text = opts$caption) + highcharter::hc_title(text = opts$title, useHTML = TRUE) |> + highcharter::hc_subtitle(text = opts$subtitle, useHTML = TRUE) |> + highcharter::hc_credits( + enabled = opts$caption_show, + text = opts$caption, + useHTML = TRUE + ) } diff --git a/man/hg_word_cloud.Rd b/man/hg_word_cloud.Rd new file mode 100644 index 0000000..32617e4 --- /dev/null +++ b/man/hg_word_cloud.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hg-word-cloud.R +\name{hg_word_cloud} +\alias{hg_word_cloud} +\title{Create a word cloud visualization} +\usage{ +hg_word_cloud(data, dic = NULL, var_cat = NULL, var_num = NULL, ...) +} +\arguments{ +\item{data}{A data frame containing the text data to visualize.} + +\item{dic}{Optional. A data dictionary for variable mapping and formatting.} + +\item{var_cat}{Character. The name of the categorical variable containing the +text data.} + +\item{var_num}{Optional. The name of the numerical variable to use for word +sizing.} + +\item{...}{Additional \code{dsopts} arguments passed for customization, +for example: +\itemize{ +\item \code{title}: Main title of the visualization +\item \code{subtitle}: Subtitle text +\item \code{caption}: Caption text +}} +} +\value{ +A highcharter object containing the word cloud visualization. +} +\description{ +Generates a word cloud using highcharter. +} +\examples{ +\dontrun{ +# Basic word cloud from text data +data <- data.frame(text = c("word1", "word2", "word3", "word1")) +hg_word_cloud(data, var_cat = "text") + +# Word cloud with frequency +data <- data.frame( + text = c("word1", "word2", "word3"), + freq = c(10, 5, 3) +) +hg_word_cloud(data, var_cat = "text", var_num = "freq") +} + +} diff --git a/tests/.DS_Store b/tests/.DS_Store deleted file mode 100644 index e18f28e..0000000 Binary files a/tests/.DS_Store and /dev/null differ diff --git a/tests/testthat/test-hg-word-cloud.R b/tests/testthat/test-hg-word-cloud.R index fb70c81..1119824 100644 --- a/tests/testthat/test-hg-word-cloud.R +++ b/tests/testthat/test-hg-word-cloud.R @@ -1,30 +1,37 @@ -test_that("hg_word_cloud", { - texto <- data.frame(text = - c("¡Hola! hola ¿Cómo están? este es un mensaje de saludo desde la galaxia andromeda. un test de comunicación!!!", - "respondan, chaooo, a todos los habitantes de la tierra") - ) - - hg_word_cloud(texto, var_cat = "text") - -}) +library(dsdatawiz) +test_that("hg_word_cloud", { + test <- sample_data("Txt-Txt-Txt", names = c("txt1", "txt2", "txt3")) -test_that("hg_word_cloud_Cat", { - texto <- data.frame(text = - c("¡Hola! hola ¿Cómo están? este es un mensaje de saludo desde la galaxia andromeda. un test de comunicación!!!", - "respondan, chaooo, a todos los habitantes de la tierra") + hg_word_cloud( + data = test, + var_cat = c("txt1") ) - hg_word_cloud_Cat(texto) - -}) - + hg_word_cloud( + data = test, + var_cat = c("txt1", "txt2") + ) -test_that("hg_word_cloud_CatNum", { - texto <- data.frame(text = c("hola", "mensaje", "galaxia", "andromeda", "Buenas", "Bien","Saludo"), - frecuencia = c(2, 5, 3, 10, 1, 3, 2) + hg_word_cloud( + data = test, + var_cat = c("txt1", "txt2", "txt3") ) - hg_word_cloud_CatNum(texto, text_size = 20) + hg_word_cloud( + data = iris, + var_cat = "Species", + title = "My first word cloud", + subtitle = "This is a subtitle", + caption = "This is a caption" + ) + hg_word_cloud( + data = iris, + var_cat = "Species", + var_num = "Sepal.Length", + title = "My first word cloud", + subtitle = "This is a subtitle", + caption = "This is a caption" + ) })