lepennec / ggwordcloud

A word cloud geom for ggplot2
https://lepennec.github.io/ggwordcloud/
GNU General Public License v3.0
172 stars 8 forks source link

How can I tell user about fitting error in shiny app ? #18

Closed shibahead closed 2 weeks ago

shibahead commented 2 years ago

Hello! Thank you for your great package. I am having one problem. I am currently working on a shiny application for wordcloud that allows the user to adjust the size of the plot as shown below.

runApp(shinyApp(
  ui = fluidPage(
    sliderInput("size","size",min=100,max=1000,step=100,value=300),
    plotOutput("cloud")
  ),
  server = function(input,output, session) {

    output$cloud = renderPlot(width=reactive(input$size),height=reactive(input$size),{
      data("love_words")
      ggplot(love_words,aes(label=word)) +
        geom_text_wordcloud(
          rm_outside = T
        )
    })
  }
))

When the plot size is small, the R studio console displays the message "Some words could not fit on page. They have been removed.", is there any way to make this visible to Shiny app users as well? I have tried combining renderUI and sink() myself, etc., but it didn't work. In the wordcloud package, I can set rm_outside to FALSE to center the words that could not be fitted, but I would like to explicitly notify the user about the fitting failure.

I apologize for the lack of clarity as I am not a native English speaker, but I would appreciate your help. Thank you.

Put this message on Shiny app for users. image

lepennec commented 2 years ago

An excellent question! Can you try the latest version on github? I have modified a msg into a true warning so that it can be captured.

shibahead commented 2 years ago

Thanks for the quick response. I immediately tried again with your response, but due to my lack of skill and knowledge, I was unable to get the warning on the Shiny app. Here is my modified code, I tried to notify the user of the contents of warnings() in showNotification, but warnings() is Empty everywhere on Shiny.

# install.packages("remotes")
# remotes::install_github("lepennec/ggwordcloud")
library(shiny)
library(ggwordcloud)
runApp(shinyApp(
  ui = fluidPage(
    sliderInput("size","size",min=100,max=1000,step=100,value=300),
    uiOutput("plotui")
  ),
  server = function(input,output, session) {

    wordcloud_plot = reactive({
      data("love_words")
      assign("last.warning", NULL, envir = baseenv())
      gg <- ggplot(love_words,aes(label=word)) +
        geom_text_wordcloud(
          rm_outside = T
        )
      showNotification(warnings())
      showNotification("aaaaaaaaa")
      return(gg)
    })

    output$cloud = renderPlot(width=reactive(input$size),height=reactive(input$size),{
      assign("last.warning", NULL, envir = baseenv())
      p <- wordcloud_plot()
      showNotification(warnings())
      showNotification("bbbbbbbb")
      return(p)
    })

    output$plotui = renderUI({
      assign("last.warning", NULL, envir = baseenv())
      t <- tagList(
        plotOutput("cloud"),
        paste0("Fitting Error",warnings())
      )
      showNotification(warnings())
      showNotification("ccccccccc")
      return(t)
    })

    observeEvent(input$size,{
      showNotification(warnings())
      showNotification("dddddddddd")
    })

  }
))

On the other hand, if the following code is executed one line at a time locally in a non-Shiny, the warnings will contain a Fitting failure message. However, if all lines are executed at once, the warnings() line is executed earlier than the plot displays, so it still appears to be Empty. This seems to be an unavoidable problem due to the nature of this package, which does not show the result of fitting until the plot is executed.

library(ggwordcloud)
data("love_words")
# clear warnings
assign("last.warning", NULL, envir = baseenv())
# make sure the warning is cleared
warnings()
# tryCatch can't capture ggwordcloud's warnings
tryCatch(
  {ggplot(love_words,aes(label=word)) +
      geom_text_wordcloud(
        rm_outside = T
      )}
  ,warning = function(e){
    print("error!")
  }
)
w <- warnings()
print(w)

This issue of timing of wanings and storing warnings on Shiny appears to be so complicated that I am not willing to have it take up any more of your time. Even if this issue is not resolved, I am grateful to you because I will still be able to make very good use of ggwordcloud in my applications.

Thank you for your response.

lepennec commented 2 years ago

Thank you for the feedback. I will see if I can do something when I will have more bandwidth...