Open andrepvieira opened 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)
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:
Modularized app:
Module:
Ui.R
Server.R
App.R