For Loops statements in Shiny

I am trying to create a shiny app that allows the user to select from 10 movies, what style of plot output they would like to see, and what 1/10th of the movie they would like to see plotted. R shiny seems to take issue with my for loops, and I don't know of any alternative ways to do what I'm attempting with those calls. This is a truncated version of what I am attempting:

ui <- fluidPage(
#Set background color
setBackgroundColor(color = "Black", shinydashboard = FALSE),

#Add a title
titlePanel("Color in Movies"),
mainPanel(),
tabsetPanel(
# Separating graphs into tabs
tabPanel(
title = "AFINN",
# Sidebar layout with input and output definitions ----
sidebarLayout(

    # Sidebar to demonstrate various slider options ----
    sidebarPanel(
      
      fileInput("choice", "Please Select Your Movie:", accept = ".txt"),
      
        selectInput("style", "Please Select Your Output Style", c("Common Words", "Common Sentiments",
                                                                  "Colors Only")),
      
      sliderInput("chunk", "Please Select Your Scene:", min = 1, max = 10, value = 1,
                  round = TRUE, animate = TRUE)),
      mainPanel(
        plotOutput("movie_plot_AFINN") 
      )
    ))
#Place BING and NRC tabs here
)) 

Define server logic

server <- function(input, output) {
script <- reactiveVal(story)

#Reactive data - AFINN
output$movie_plot_AFINN <- renderPlot({
if(input$style == "Common Words") {
# Breaking selected move into chunks
story_trun <- script %>% unite(col = "word", sep = " ", na.rm = TRUE)
story_split <- split(story_trun, sample(1:10, nrow(story_trun), replace=T))

  #Word analysis
  for(i in 1:10){
    story_split[[i]] <- story_split[[i]] %>% unnest_tokens(input = word, output = word) %>% 
      anti_join(stop_words, by = "word") %>% count( word, sort=TRUE)
  }
  #Actual Plot
par(bg="#919191")
  story_split[[input$chunk]] %>% with(wordcloud(word, n, max.words = 50, colors = movpal))

} else if (input$style == "Common Sentiments") {
# Breaking selected move into chunks
story_trun <- script %>% unite(col = "word", sep = " ", na.rm = TRUE)
story_split <- split(story_trun, sample(1:10, nrow(story_trun), replace=T))

#Word analysis
for(i in 1:10){
  story_split[[i]] <- story_split[[i]] %>% unnest_tokens(input = word, output = word) %>% 
    anti_join(stop_words, by = "word") %>% count( word, sort=TRUE)
}
# Actual Plot
story_sent <- story_split[[input$chunk]] %>% inner_join(get_sentiments("afinn"))
  ggplot(story_sent[1:10,], aes(label = word, size = n, color = word)) +
    geom_text_wordcloud() +
    scale_size_area(max_size = 10) +
    scale_color_manual(values = movpal) +
    theme_dark()

} else {
# Breaking selected move into chunks
story_trun <- script %>% unite(col = "word", sep = " ", na.rm = TRUE)
story_split <- split(story_trun, sample(1:10, nrow(story_trun), replace=T))

#Word analysis
for(i in 1:10){
  story_split[[i]] <- story_split[[i]] %>% unnest_tokens(input = word, output = word) %>% 
    anti_join(stop_words, by = "word") %>% count( word, sort=TRUE)
}
#Actual Plot
placehold <- as.data.frame(story_split[[input$chunk]])
  ggplot(placehold[1:10,], aes(y = n, x = word, fill = word)) +
    geom_col() +
    coord_polar(start = 0) +
    ylim(-20, 150) +
    theme_dark() +
    theme(axis.text = element_blank(),
          axis.title = element_blank(),
          panel.grid = element_blank(),
          plot.margin = unit(rep(-4,4), "in"),
          plot.title = element_text(margin = margin(t = 10, b = -10))) +
    scale_fill_manual(values = movpal)

}
})
}

Any help would be greatly appreciated!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.