Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive word cloud failing in modularized shiny app #66

Open
andrepvieira opened this issue Jun 4, 2019 · 0 comments
Open

Reactive word cloud failing in modularized shiny app #66

andrepvieira opened this issue Jun 4, 2019 · 0 comments

Comments

@andrepvieira
Copy link

andrepvieira commented Jun 4, 2019

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)
@andrepvieira andrepvieira changed the title Reactive wordcloud2 failing in modularized shiny app Reactive word cloud failing in modularized shiny app Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant