Lchiffon / wordcloud2

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

Reactive word cloud failing in modularized shiny app #66

Open andrepvieira opened 5 years ago

andrepvieira commented 5 years ago

The word cloud is not reacting to inputs when used in a modularized shiny. But it works when the app is created using a single app.R file.

Small reproducible example:

Single app.R file:

choices_select <- wordcloud2::demoFreq %>% 
        dplyr::mutate(word = as.character(word)) %>%
        dplyr::filter(nchar(word) > 4) %>% 
        pull(word)

ui <- bs4Dash::bs4DashPage(
        navbar = bs4Dash::bs4DashNavbar(rightUi = NULL, controlbarIcon = "", sidebarIcon = "arrows-alt-h"),
        sidebar = bs4Dash::bs4DashSidebar(

                bs4Dash::bs4SidebarMenu(
                        # bs4Dash::bs4SidebarHeader("Poupe+"),
                        bs4Dash::bs4SidebarMenuItem(
                                "Interactions",
                                tabName = "message",
                                icon = "comments"
                        )
                )

        ),
        bs4Dash::bs4DashBody(

                bs4Dash::bs4TabItems(

                        bs4Dash::bs4TabItem(tabName = "message",

                                            column(width = 6,
                                                   bs4Dash::bs4Card(
                                                           width = 12,
                                                           title = "Word Cloud",
                                                           status = "primary",
                                                           solidHeader = T,
                                                           column(width = 8,
                                                                  selectizeInput("filter_word",
                                                                                 "Filter",
                                                                                 selected = NULL,
                                                                                 choices = choices_select,
                                                                                 multiple = T,
                                                                                 width = "100%",
                                                                                 options = list(
                                                                                         placeholder = "Select words."
                                                                                 )
                                                                  )
                                                           ),
                                                           wordcloud2::wordcloud2Output("wordcloud") # %>% withSpinner()
                                                   )
                                            )
                        )

                )

        )
)

server <- function(input, output, session) { 

        dt_wc <- shiny::reactive({

                df <-  wordcloud2::demoFreq %>% 
                        dplyr::mutate(word = as.character(word)) %>%
                        dplyr::filter(nchar(word) > 4) %>%
                        # head
                        dplyr::filter(!word %in% c(input$filter_word)) %>%
                        data.frame

                return(df)

        })

        wordcloud_rep <- shiny::repeatable(wordcloud2::wordcloud2)

        output$wordcloud <- wordcloud2::renderWordcloud2({

                wordcloud_rep(data = dt_wc(),
                              size = 2, minRotation = -pi/2, maxRotation = -pi/2
                )
              })
        }

shinyApp(ui = ui, server = server)

Modularized app:

Module:

mod_messages_ui <- function(id){

        ns <- NS(id)

        choices_select <- wordcloud2::demoFreq %>% 
                dplyr::mutate(word = as.character(word)) %>%
                dplyr::filter(nchar(word) > 4) %>% 
                pull(word)

        bs4Dash::bs4TabItem(tabName = "message",

                            column(width = 6,
                                   bs4Dash::bs4Card(
                                           width = 12,
                                           title = "Word Cloud",
                                           status = "primary",
                                           solidHeader = T,
                                           column(width = 8,
                                                  selectizeInput("filter_word",
                                                                 "Filter",
                                                                 selected = NULL,
                                                                 choices = choices_select,
                                                                 multiple = T,
                                                                 width = "100%",
                                                                 options = list(
                                                                         placeholder = "Select words."
                                                                 )
                                                  )
                                           ),
                                           wordcloud2::wordcloud2Output(ns("wordcloud")) # %>% withSpinner()
                                   )
                            )
        ) 
}

mod_messages_server <- function(input, output, session){

        ns <- session$ns

        dt_wc <- shiny::reactive({

                df <-  wordcloud2::demoFreq %>% 
                        dplyr::mutate(word = as.character(word)) %>%
                        dplyr::filter(nchar(word) > 4) %>%
                        # head
                        dplyr::filter(!word %in% c(input$filter_word)) %>%
                        data.frame

                return(df)

        })

        wordcloud_rep <- shiny::repeatable(wordcloud2::wordcloud2)

        output$wordcloud <- wordcloud2::renderWordcloud2({

                wordcloud_rep(data = dt_wc(),
                              size = 2, minRotation = -pi/2, maxRotation = -pi/2
                )

        })

        }

Ui.R

ui <- bs4Dash::bs4DashPage(
        navbar = bs4Dash::bs4DashNavbar(rightUi = NULL, controlbarIcon = "", sidebarIcon = "arrows-alt-h"),
        sidebar = bs4Dash::bs4DashSidebar(

                bs4Dash::bs4SidebarMenu(
                        # bs4Dash::bs4SidebarHeader("Poupe+"),
                        bs4Dash::bs4SidebarMenuItem(
                                "Interactions",
                                tabName = "message",
                                icon = "comments"
                        )
                        )

        ),
        bs4Dash::bs4DashBody(

                bs4Dash::bs4TabItems(

                        mod_messages_ui("messages")

                        )
         )
)

Server.R

server <- function(input, output, session) { 
 callModule(module = mod_messages_server, "messages")
        }

App.R

source("R/mod_messages.R")
source("R/ui.R")
source("R/server.R")
shinyApp(ui = ui, server = server)