Lchiffon / wordcloud2

R interface to wordcloud for data visualization.
396 stars 107 forks source link

Click Event and Mouse pointer #74

Open fomofahg opened 3 years ago

fomofahg commented 3 years ago

This is a copy of a questioned I asked over at stackoverflow.

I recently asked a question, rather it is possible to change the mouse to a pointer, when it hovers over the words in a wordcloud in shinyapp. The answer worked successfully.

library(shiny)
library(wordcloud2)
# Global variables can go here
n <- 1

# Define the UI
ui <- bootstrapPage(
  tags$head(tags$style(HTML('.wcLabel {cursor: pointer; pointer-events: auto !important;}'))),
  numericInput('size', 'Size of wordcloud', n),
  wordcloud2Output('wordcloud2'),
)

# Define the server code
server <- function(input, output) {

  output$wordcloud2 <- renderWordcloud2({
    # wordcloud2(demoFreqC, size=input$size)
    wordcloud2(demoFreq, size=input$size)
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

But now, input$Wordcloud_clicked_word won't react anymore.

(Edit: as HubertL correctly mentioned, using click event in the package of CRAN is not possible. However, installing the Version of GitHub makes it possible as noted here. So devtools::install_github("lchiffon/wordcloud2") is needed to make click events possible)

library(shiny)
library(wordcloud2)
# Global variables can go here
n <- 1

# Define the UI
ui <- bootstrapPage(
  tags$head(tags$style(HTML('.wcLabel {cursor: pointer; pointer-events: auto !important;}'))),
  numericInput('size', 'Size of wordcloud', n),
  wordcloud2Output('wordcloud2'),
  verbatimTextOutput("ClickedWord")
)

# Define the server code
server <- function(input, output) {

  output$wordcloud2 <- renderWordcloud2({
    # wordcloud2(demoFreqC, size=input$size)
    wordcloud2(demoFreq, size=input$size)
  })

  output$ClickedWord <- renderText(input$wordcloud2_clicked_word)

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

When I change .wcLabel, then the verbatimTextOutput won't react anymore on clicking on a word in the cloud.

Is it possible to have both, the mouse changing to pointer on hover and reacting to clicking on a word?

Thanks!

Frederik