tantal148
tantal148

Reputation: 57

Dashboard working inside the shiny app but not on flexdashboard

I'm trying to incorporate shiny interactive sheet to my already existing flexdashboard.

Here is the data structure created using dput() function

structure(list(words1 = structure(c(1L, 10L, 19L, 20L, 7L, 8L, 
3L, 15L, 2L, 9L, 11L, 14L, 12L, 4L, 5L, 6L, 16L, 18L, 17L, 13L
), .Label = c("app", "can", "connect", "dont", "free", "get", 
"good", "great", "like", "map", "navig", "need", "one", "phone", 
"purchas", "road", "rout", "updat", "use", "work"), class = "factor"), 
    pct1 = c(70.8, 40.4, 26.4, 25.6, 22.7, 18.4, 13.7, 11.2, 
    10.8, 10.8, 10.8, 10.5, 10.5, 10.1, 9.4, 9.4, 9, 8.3, 8.3, 
    8.3), words2 = structure(c(1L, 18L, 8L, 19L, 16L, 4L, 17L, 
    5L, 15L, 12L, 14L, 6L, 3L, 11L, 20L, 9L, 10L, 2L, 7L, 13L
    ), .Label = c("app", "download", "get", "good", "great", 
    "just", "like", "map", "navig", "need", "now", "phone", "rout", 
    "time", "tom", "tomtom", "updat", "use", "work", "year"), class = "factor"), 
    pct2 = c(69.1, 32.9, 27.3, 25, 21.4, 20, 16.6, 16.2, 14.7, 
    13.1, 13, 13, 13, 12.7, 12.2, 11.8, 11.2, 10.4, 10.3, 10.2
    ), words3 = structure(c(1L, 17L, 8L, 19L, 16L, 4L, 12L, 14L, 
    2L, 5L, 20L, 15L, 13L, 3L, 18L, 11L, 9L, 6L, 10L, 7L), .Label = c("app", 
    "copilot", "get", "good", "great", "just", "like", "map", 
    "navig", "need", "new", "now", "road", "rout", "time", "updat", 
    "use", "version", "work", "year"), class = "factor"), pct3 = c(57.9, 
    45.7, 30.1, 21.7, 19.5, 19, 18.5, 18, 17.1, 16.7, 14.9, 14.6, 
    14, 12.7, 12.5, 12.2, 11.5, 10.9, 10.6, 10.5), words4 = structure(c(1L, 
    19L, 11L, 20L, 8L, 18L, 14L, 15L, 9L, 7L, 16L, 4L, 12L, 10L, 
    6L, 3L, 2L, 13L, 5L, 17L), .Label = c("app", "can", "doesnt", 
    "download", "even", "find", "get", "good", "great", "just", 
    "map", "navig", "navmii", "now", "rout", "time", "tri", "updat", 
    "use", "work"), class = "factor"), pct4 = c(56.1, 41.2, 36.5, 
    32.5, 22.6, 20.9, 18.9, 15.8, 14.9, 14.1, 14, 12.9, 12.4, 
    11.7, 11.3, 11.1, 10.9, 10.6, 10.3, 10.1), words5 = structure(c(1L, 
    4L, 19L, 10L, 6L, 11L, 20L, 2L, 16L, 18L, 12L, 17L, 5L, 8L, 
    9L, 15L, 14L, 3L, 13L, 7L), .Label = c("app", "best", "get", 
    "good", "gps", "great", "just", "like", "love", "map", "navig", 
    "nice", "now", "offlin", "rout", "sygic", "time", "updat", 
    "use", "work"), class = "factor"), pct5 = c(39.7, 23.5, 20.5, 
    19.3, 13.8, 11.3, 10.5, 10.2, 8.6, 7.7, 6.7, 6.7, 6.1, 6, 
    6, 5.9, 5.5, 5.1, 5, 4.9), words6 = structure(c(1L, 10L, 
    19L, 5L, 6L, 14L, 2L, 20L, 13L, 11L, 7L, 17L, 9L, 15L, 4L, 
    8L, 16L, 3L, 18L, 12L), .Label = c("app", "best", "can", 
    "download", "good", "great", "help", "like", "love", "map", 
    "navig", "need", "nice", "offlin", "rout", "thank", "travel", 
    "updat", "use", "work"), class = "factor"), pct6 = c(37.4, 
    26.4, 23, 20.7, 13.3, 13.2, 10.6, 9.1, 6.5, 6.1, 6, 6, 5.6, 
    5.4, 5.1, 5.1, 4.9, 4.9, 4.8, 4.6), words7 = structure(c(1L, 
    18L, 19L, 11L, 15L, 20L, 14L, 6L, 5L, 8L, 10L, 4L, 7L, 17L, 
    3L, 9L, 13L, 16L, 12L, 2L), .Label = c("app", "best", "drive", 
    "get", "good", "googl", "gps", "great", "like", "love", "map", 
    "navig", "road", "rout", "time", "traffic", "updat", "use", 
    "waze", "work"), class = "factor"), pct7 = c(57.9, 32.4, 
    24.6, 20.3, 18.9, 17.1, 15.9, 15.2, 12.8, 12.3, 12.2, 11.8, 
    11.8, 10.9, 10, 9.9, 9.7, 9.7, 9.4, 9.1), words8 = structure(c(1L, 
    12L, 19L, 15L, 6L, 13L, 2L, 20L, 9L, 5L, 7L, 18L, 11L, 16L, 
    10L, 17L, 8L, 14L, 4L, 3L), .Label = c("app", "best", "better", 
    "can", "download", "good", "googl", "gps", "great", "like", 
    "love", "map", "navig", "need", "offlin", "rout", "time", 
    "updat", "use", "work"), class = "factor"), pct8 = c(44.6, 
    39.3, 25.2, 22.3, 18.5, 15.4, 15.4, 14.1, 12.6, 8.8, 8.7, 
    8.6, 7.3, 7.1, 6.8, 6.6, 6.6, 6.4, 6, 5.5)), class = "data.frame", row.names = c(NA, 
-20L))

Data is about frequent words in reviews of 8 different offline navigation apps.

I have created small shiny application using following code (which works)

library(shiny)
library(ggplot2)
library(plyr)
library(dplyr)

# Data Preparation Steps 
data <- read.csv("word_frequency_v2.csv")

my_colors2 <- c("#F29E4C", "#EFEA5A", "#6930C3", "#0077B6",
                "#F94144", "#16DB93", "#621708", "#DA627D")



ui <- fluidPage(
  
  # App title ----
  titlePanel("Shiny - First Interactive Visualization Example"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      selectInput(inputId="app1",label="App1",choices = c("Genius"="Genius",
                                                                        "TomTom"="TomTom",
                                                                        "CoPilot"="CoPilot",
                                                                        "Navmii"="Navmii",
                                                                        "Sygic"="Sygic",
                                                                        "Mapsme"="Mapsme",
                                                                        "Waze"="Waze",
                                                                        "Here"="Here"),
                  selected = "Genius",multiple = F),
      
      selectInput(inputId="app2",label="App2",choices = c("Genius"="Genius",
                                                          "TomTom"="TomTom",
                                                          "CoPilot"="CoPilot",
                                                          "Navmii"="Navmii",
                                                          "Sygic"="Sygic",
                                                          "Mapsme"="Mapsme",
                                                          "Waze"="Waze",
                                                          "Here"="Here"),
                  selected = "TomTom",multiple = F)
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      plotOutput(outputId = "distPlot1"),
      plotOutput(outputId = "distPlot2")

    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output){
  
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  
  output$distPlot1 <- renderPlot({
    
    p2 <- data %>% ggplot()
    if(input$app1 == "Genius"){
      p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
        geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "TomTom"){
      p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
        geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "CoPilot"){
      p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
        geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Navmii"){
      p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
        geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Sygic"){
      p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
        geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Mapsme"){
      p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
        geom_text(aes(x=reorder(words6, -pct6), y=pct6, labe6 =pct6),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Waze"){
      p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
        geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Here"){
      p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
        geom_text(aes(x=reorder(words8, -pct1), y=pct8, label =pct8),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }
    p2 <- p2 +  theme_bw()+
      scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),
            plot.title = element_text(hjust = 0.5),
            axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) + 
      labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app1,sep = " "))
    
    p2
  })
  
  output$distPlot2 <- renderPlot({
    
    p2 <- data %>% ggplot()
    if(input$app2 == "Genius"){
      p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
        geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "TomTom"){
      p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
        geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "CoPilot"){
      p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
        geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Navmii"){
      p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
        geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Sygic"){
      p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
        geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Mapsme"){
      p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
        geom_text(aes(x=reorder(words6, -pct6), y=pct6, label =pct6),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Waze"){
      p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
        geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Here"){
      p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
        geom_text(aes(x=reorder(words8, -pct8), y=pct8, label =pct8),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }
    p2 <- p2 +  theme_bw()+
      scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),
            plot.title = element_text(hjust = 0.5),
            axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) + 
      labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app2,sep = " "))
    
    p2
  })
}

shinyApp(ui = ui, server = server)

Now I'm trying to use that code and incorporate it to my already existing flexdashboard using the following code, but I can't manage to make it work.

---
title: "Shiny in flexdashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny      
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(ggplot2)
library(shiny)
```

```{r}
#Import data
data <- read.csv("word_frequency_v2.csv")
my_colors2 <- c("#F29E4C", "#EFEA5A", "#6930C3", "#0077B6",
                "#F94144", "#16DB93", "#621708", "#DA627D")
```

# Word frequency

### Embedding a Shiny Module {.sidebar}

```{r, include=FALSE}
selectInput(inputId="app1",label="App1",choices = c("Genius"="Genius",
                                                    "TomTom"="TomTom",
                                                    "CoPilot"="CoPilot",
                                                    "Navmii"="Navmii",
                                                    "Sygic"="Sygic",
                                                    "Mapsme"="Mapsme",
                                                    "Waze"="Waze",
                                                    "Here"="Here"),
            selected = "Genius",multiple = F)
      
selectInput(inputId="app2",label="App2",choices = c("Genius"="Genius",
                                                    "TomTom"="TomTom",
                                                    "CoPilot"="CoPilot",
                                                    "Navmii"="Navmii",
                                                    "Sygic"="Sygic",
                                                    "Mapsme"="Mapsme",
                                                    "Waze"="Waze",
                                                    "Here"="Here"),
            selected = "TomTom",multiple = F)
```

```{r, include=FALSE}
renderPlot({
    
    p2 <- data %>% ggplot()
    if(input$app1 == "Genius"){
      p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
        geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "TomTom"){
      p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
        geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "CoPilot"){
      p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
        geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Navmii"){
      p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
        geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Sygic"){
      p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
        geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Mapsme"){
      p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
        geom_text(aes(x=reorder(words6, -pct6), y=pct6, labe6 =pct6),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Waze"){
      p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
        geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app1 == "Here"){
      p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
        geom_text(aes(x=reorder(words8, -pct1), y=pct8, label =pct8),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }
    p2 <- p2 +  theme_bw()+
      scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),
            plot.title = element_text(hjust = 0.5),
            axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) + 
      labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app1,sep = " "))
    
    p2
  })
  
renderPlot({
    
    p2 <- data %>% ggplot()
    if(input$app2 == "Genius"){
      p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
        geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "TomTom"){
      p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
        geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "CoPilot"){
      p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
        geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Navmii"){
      p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
        geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Sygic"){
      p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
        geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Mapsme"){
      p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
        geom_text(aes(x=reorder(words6, -pct6), y=pct6, label =pct6),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Waze"){
      p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
        geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }else if(input$app2 == "Here"){
      p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
        geom_text(aes(x=reorder(words8, -pct8), y=pct8, label =pct8),
                  position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
    }
    p2 <- p2 +  theme_bw()+
      scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),
            plot.title = element_text(hjust = 0.5),
            axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) + 
      labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app2,sep = " "))
    
    p2
  })
```

If I run rmarkdown:render("filename.Rmd") I get the following error:path for html_dependency not provided

If I run rmarkdown:run("filename.Rmd") I get an app which doesn't show any filters nor charts. I would like to run the render function if possible since I used to do that with my previous flexdashboard and everything looks better if I export the dashboard to .html file.

Upvotes: 1

Views: 389

Answers (1)

starja
starja

Reputation: 10365

The trick is to only use {r} and leave out the include = FALSE. Also, I've corrected the column and sidebar definition.


    ---
    title: "Shiny in flexdashboard"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny      
    ---
    
    ```{r setup, include=FALSE}
    library(dplyr)
    library(ggplot2)
    library(shiny)
    ```
    
    ```{r}
    #Import data
    data <- read.csv("word_frequency_v2.csv")
    my_colors2 <- c("#F29E4C", "#EFEA5A", "#6930C3", "#0077B6",
                    "#F94144", "#16DB93", "#621708", "#DA627D")
    ```
    
    # Word frequency
    
    Column {.sidebar}
    -----------------------------------------------------------------------
    Embedding a Shiny Module
    
    ```{r}
    selectInput(inputId="app1",label="App1",choices = c("Genius"="Genius",
                                                        "TomTom"="TomTom",
                                                        "CoPilot"="CoPilot",
                                                        "Navmii"="Navmii",
                                                        "Sygic"="Sygic",
                                                        "Mapsme"="Mapsme",
                                                        "Waze"="Waze",
                                                        "Here"="Here"),
                selected = "Genius",multiple = F)
          
    selectInput(inputId="app2",label="App2",choices = c("Genius"="Genius",
                                                        "TomTom"="TomTom",
                                                        "CoPilot"="CoPilot",
                                                        "Navmii"="Navmii",
                                                        "Sygic"="Sygic",
                                                        "Mapsme"="Mapsme",
                                                        "Waze"="Waze",
                                                        "Here"="Here"),
                selected = "TomTom",multiple = F)
    ```
    
    Column
    -----------------------------------------------------------------------
    
    ```{r}
    renderPlot({
    
        p2 <- data %>% ggplot()
        if(input$app1 == "Genius"){
          p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
            geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "TomTom"){
          p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
            geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "CoPilot"){
          p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
            geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "Navmii"){
          p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
            geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "Sygic"){
          p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
            geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "Mapsme"){
          p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
            geom_text(aes(x=reorder(words6, -pct6), y=pct6, labe6 =pct6),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "Waze"){
          p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
            geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app1 == "Here"){
          p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
            geom_text(aes(x=reorder(words8, -pct1), y=pct8, label =pct8),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }
        p2 <- p2 +  theme_bw()+
          scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.background = element_blank(), axis.line = element_line(colour = "black"),
                plot.title = element_text(hjust = 0.5),
                axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) +
          labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app1,sep = " "))
    
        p2
      })
    
    renderPlot({
    
        p2 <- data %>% ggplot()
        if(input$app2 == "Genius"){
          p2 <- p2 + geom_col(aes(x=reorder(words1, -pct1), y=pct1), fill = my_colors2[1]) +
            geom_text(aes(x=reorder(words1, -pct1), y=pct1, label =pct1),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "TomTom"){
          p2 <- p2 + geom_col(aes(x=reorder(words2, -pct2), y=pct2), fill = my_colors2[2]) +
            geom_text(aes(x=reorder(words2, -pct2), y=pct2, label =pct2),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "CoPilot"){
          p2 <- p2 + geom_col(aes(x=reorder(words3, -pct3), y=pct3), fill = my_colors2[3]) +
            geom_text(aes(x=reorder(words3, -pct3), y=pct3, label =pct3),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "Navmii"){
          p2 <- p2 + geom_col(aes(x=reorder(words4, -pct4), y=pct4), fill = my_colors2[4]) +
            geom_text(aes(x=reorder(words4, -pct4), y=pct4, label =pct4),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "Sygic"){
          p2 <- p2 + geom_col(aes(x=reorder(words5, -pct5), y=pct5), fill = my_colors2[5]) +
            geom_text(aes(x=reorder(words5, -pct5), y=pct5, label =pct5),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "Mapsme"){
          p2 <- p2 + geom_col(aes(x=reorder(words6, -pct6), y=pct6), fill = my_colors2[6]) +
            geom_text(aes(x=reorder(words6, -pct6), y=pct6, label =pct6),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "Waze"){
          p2 <- p2 + geom_col(aes(x=reorder(words7, -pct7), y=pct7), fill = my_colors2[7]) +
            geom_text(aes(x=reorder(words7, -pct7), y=pct7, label =pct7),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }else if(input$app2 == "Here"){
          p2 <- p2 + geom_col(aes(x=reorder(words8, -pct8), y=pct8), fill = my_colors2[8]) +
            geom_text(aes(x=reorder(words8, -pct8), y=pct8, label =pct8),
                      position = position_dodge(0.9), size = 3.5, fontface = "bold", vjust = -1)
        }
        p2 <- p2 +  theme_bw()+
          scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.background = element_blank(), axis.line = element_line(colour = "black"),
                plot.title = element_text(hjust = 0.5),
                axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) +
          labs(x="Words",y="Share of words in reviews",title=paste("Frequency of words -",input$app2,sep = " "))
    
        p2
      })

    ```

Upvotes: 1

Related Questions