Unable to render wordcloud and valueBox together on Rshiny dashboard

28 Views Asked by At

I have created a shiny dashboard which contains a sidebar with one selectInput filter and the dashboard body has 3 valueBox scorecards, one barchart and one wordcloud. When I added the wordcloud code in the server section, my 1st scorecard disappeared from the dashboard completely. Here is my code snippet:

ui.R

header <- dashboardHeader(title = "Dashboard")
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Reviews Analysis", tabName = "reviews"),
    selectInput("app_select", "Choose an App to filter the dashboard:", choices = unique(reviews_data$App))
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = "reviews",
      fluidRow(
        valueBoxOutput("avg_sentiment_polarity"),
        valueBoxOutput("avg_sentiment"),
        valueBoxOutput("avg_sentiment_subjectivity")
      ),
      fluidRow(
        column(
          width = 6,
          box(plotlyOutput("review_bar"))
        ),
        column(
          width = 6,
          box(wordcloud2Output("word_cloud"))
        )
      )
    )
  )
)

dashboardPage(header, sidebar, body)

server.R

server <- function(input, output, session) {
  selected_app_reviews <- reactive({
    reviews_data %>%
      filter(App == input$app_select)
  })
  
  output$avg_sentiment_polarity <- renderValueBox({
    valueBox(round(selected_app_reviews()$Average_Sentiment_Polarity, 2),
             "Average Sentiment Polarity", color = "purple", icon = icon("compass"))
  })

  ## code for 2 more valuBox
  ## code for bar chart
  
  output$word_cloud <- renderWordcloud2({
    corpus <- Corpus(VectorSource(selected_app_reviews()$Combined_Reviews))
    corpus <- tm_map(corpus, content_transformer(tolower))
    corpus <- tm_map(corpus, removePunctuation)
    corpus <- tm_map(corpus, removeNumbers)
    corpus <- tm_map(corpus, removeWords, stopwords("english"))

    wordcloud_data <- TermDocumentMatrix(corpus)
    word_freq <- as.data.frame(as.matrix(wordcloud_data))
    word_freq <- data.frame(Word = rownames(word_freq), Freq = word_freq$`1`)
    word_freq <- word_freq[complete.cases(word_freq), ]

    wordcloud2(word_freq, backgroundColor = "white")
  })
}

When I tried uncommenting this line of code wordcloud2(word_freq, backgroundColor = "white"), then the 1st valueBox appears again. Here is a screenshot of both scenarios:

On commenting wordcloud

Without commenting wordcloud

Is there something interfering with the scorecard while rendering the wordcloud? Can anyone help me with this?

0

There are 0 best solutions below