Search code examples
rshinypurrrsentimentr

Purrr map function working in console but failing in shiny app


I have a shiny app looking at sentiment analysis on news articles. As part of this I am using sentimentr functions get_sentences() and sentiment(). When I run this code in the console it works fine, but when I try to run it through a shiny I get the error Error in mutate_impl: Evaluation error: unused argument (.x)

The relevant code is:

ui <- fluidPage(
  useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      ...
    mainPanel(
      tabsetPanel(type = "tab",
        ...
        tabPanel("Sentiment", 
           selectInput(inputId = "sourceSelect", label = "Media Source", sources$title),
           plotOutput("sentiment"),
         )
      )
    )
  )
)

server <- function(input, output) {

  ...

  output$sentiment <- renderPlot(
    sentiment()
  )
  sentiment <-eventReactive(input$sourceSelect, {
    max_min = getMinMax(input)
    min = max_min[1]
    max = max_min[2]
    news1 <- news %>%
      filter(isDuplicate == "FALSE") %>%
      filter(date < max) %>%
      filter(date > min) %>%
      mutate(id = seq(1, nrow(.), 1))  %>%
      mutate(selectedSource = str_detect(title, input$sourceSelect)) %>%
      select(date, body, source, title, id, selectedSource)
    news_sentiment <- news1 %>%
      mutate(sentences = map(body, ~(get_sentences(.x))), (sentiment = map(sentences, ~(sentiment(.x)))))
    ...
})
...

}


shinyApp(ui = ui, server = server)

With the error occurring on the mutate(sentences = map(body, ~(get_sentences(.x))), (sentiment = map(sentences, ~(sentiment(.x))))) line.

When I run this through the console, it does not throw an error and works perfectly, creating a dataframe with a sentences column, and a sentiment column with relevant lists in them (the correct output from the sentimentr functions). I have tested to see that the frame being passed in to this pipe is the same in both console and shiny versions. I have a suspicion that there is something to do with the map call using (.x) that may not be playing nicely with shiny.


Solution

  • I have discovered the error, it was that the function was called sentiment and was also trying to call sentiment from the sentimentr library. Once I had renamed all of these to be distinct it worked.