I am having problems with generating an output in shiny where I want the client to manually change the colour upon their request and the data is reactive. I simply get a blank page and nothing shown into it apart from the selectInput filter, and the dropdown item, but not the plot.
My attempt consisted of a combination of lapply with colourpicker function and created a dropdown item where the user can pick the colour he likes. Unfortunately, I could not provide a reproducible example or the dataset because there is data that could not be shared and I apologise in advance. I hope that someone might recognize the problem by looking at the code in case it is very obvious. In summary the data that has been used for this example consists of three columns: title, partner, quantity(e.x. a title of a book, the partner that prints it and the number of copies printed).
I believe that it has to do sth with the 3 lines of code for the 'cols' before I generate the plot inside the renderPlot. I would greatly appreciate if sb could lead me in the right direction. Thanks a lot in advance.
### 1) Loading the libraries ----
library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(plotly)
library(colourpicker)
library(tidyverse)
### UI ----
header <-
dashboardHeader( title = HTML("Dashboard"),
disable = FALSE,
titleWidth = 230)
sidebar <- dashboardSidebar(
sidebarMenu(id="sbmenu",
menuItem("General Dashboard", tabName = "dashboard", icon = icon("dashboard"))))
body <- dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
fluidRow(
selectizeInput("choice","Please select",
choices=levels(Complete$title),multiple=T)),
fluidPage(
uiOutput("myPanel"),
plotOutput("plot")))))
## UI wrapper -----
ui <- dashboardPage(header, sidebar, body)
## Server ----
server <- function(input, output) {
data_complete<-reactive(Complete %>%
filter(title%in% input$choice,quantity>0))
### Plot
output$myPanel <- renderUI({
lev <- sort(unique(input$choice))
cols <- length(lev)
dropdownButton(lapply(seq_along(lev), function(i) {
colourpicker::colourInput(inputId = paste0("col", lev[i]),
label = paste0("Choose colour", lev[i]),
value = cols[i])}),
circle = TRUE, size="xs",status = "danger", icon = icon("gear"), width = "50px",
tooltip = tooltipOptions(title = "Change your colour"))
})
output$plot <- renderPlot({
cols <- paste0("c(", paste0("input$col", sort(input$choice), collapse = ", "), ")")
cols <- eval(parse(text = cols))
req(length(cols) == length(input$choice))
data_complete() %>%
group_by(title,partner) %>%
summarise(total=sum(quantity)) %>%
ggplot(aes(x=partner,y=total,fill=title))+
geom_col(stat="identity",position = position_dodge())+
scale_fill_manual(values = cols)
})
}
shinyApp(ui, server)
