0

I am trying to plot the common phrases just before a crash versus its frequency. Instead of creating a new slider, I decided to use a slider that is already there on the same page. Since in the chart I only have 17 bars, I want that if the slider input is 17 or less it plot the corresponding number of bars from the slider else it should plot 17 bars.

My UI Code is:

tabPanel("Word Cloud",
         sidebarLayout(
             sidebarPanel( tags$div(
                 tags$strong("About Word Cloud"), br(),
                 "A word cloud (weighted list in visual design) is a novelty visual representation of text data. It depicts the words which occured more frequently in the aircrash summary. The importance of each tag is shown with font size and color. The user has the ability to change the number of words they want to see in the word cloud by adjusting the slider below.",
                 br(), br()),
                 sliderInput("numberInput2", "Maximum Number of Words:", min=1, max=105, 
                             value=35)
                 
             ),
             mainPanel(plotOutput("WordCloudPlot"),
                 br(),
                 plotlyOutput("WordCloudBarPlot"),
                 br(),
                 plotlyOutput("AircrashPhrases")
             )
         )
)

My server code is:

        d8<- reactive({
            req(input$numberInput2)
            if (input$numberInput2 <= 17) {
                common_phrases %>%
                    dplyr::top_n(input$numberInput2)
            }
            else {
                common_phrases %>%
                    dplyr::top_n(17)
            }
        })
        
        output$AircrashPhrases <-renderPlotly({
            a8 <- ggplot(d8(), aes(x=reorder(word, -frequency), y=frequency))+
                geom_bar(stat="identity", fill='steelblue')+theme_bw()+
                theme(plot.title = element_text(color = "black", size = 20, face = "bold", hjust = 0.5), 
                      axis.title.x =element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
                      axis.title.y = element_text(color = "black", size = 14, face = "bold", hjust = 0.5))+
                labs(y = "Frequency",x="Words",title = "Word Cloud Frequency")+
                theme(axis.text.x = element_text(angle = 45, hjust = 1))
            ggplotly(a8, source = "select", tooltip = "frequency")  
            
        })
        

The error that I'm getting for my second plot (output$AircrashPhrases) is "invalid argument to unary operator". Please let me know how I can fix it.

Thanks

Deepansh Arora
  • 724
  • 1
  • 3
  • 15
  • 2
    What does `class(common_phrases$frequency)` return? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 16 '20 at 04:36
  • 1
    The error suggests that `-frequency` might be the issue. – neilfws Dec 16 '20 at 04:40
  • Thanks for a quick response. The -frequency was the issue. I changed it to ggplot(d8(), aes(x=word, y=frequency)) and it worked. I wonder if that is a bug in one of the packages. – Deepansh Arora Dec 16 '20 at 05:11

0 Answers0