Reputation: 156
I am having a problem with shiny Select input and I would appreciate it if sb could help. I was trying to create a plot where the user could change the colour of the chart upon his request. It seems that somehow if the names of the inputs in Selectinput are with hyphens then the renderplot will return nothing. When I remove the hyphen from the name(e.x. A2019 instead of A-2019) the renderplot returns the correct plot and everything works fine. I have been trying for hours to fix it without any success. What am I doing wrong in my syntax?
gg_fill_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
Complete_1 <- data.frame(
title = c("A-2019", "B-2018", "C-2017","D-2018","E-2019","F-2020"),
partner = c("A", "A", "B","B","C","C"),
quantity = c(100, 200, 300,400,500,600)
)
library(shinydashboard)
library(shiny)
library(jsonlite)
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",p("Please select title:",style="color:black; text-align:center"),
choices=levels(Complete_1$title),multiple=T)),
fluidPage(
uiOutput("myPanel"),
plotOutput("plot")))))
## UI wrapper -----
ui <- dashboardPage(header, sidebar, body)
## Server ----
server <- function(input, output) {
data_complete <- reactive({Complete_1 %>%
filter(title%in%input$choice)})
### Plot
output$myPanel <- renderUI({
lev <- sort(unique(input$choice))
cols <- gg_fill_hue(length(lev))
dropdownButton(lapply(seq_along(lev), function(i) {
colourpicker::colourInput(inputId = paste0("col", lev[i]),
label = paste0("Choose colour for", 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)
Upvotes: 0
Views: 185
Reputation: 123893
The issue is not related to SelectInput not accepting names with hyphens. In general, if you want to name a variable or list elements or dataframe columns uisng hypthens or spaces or ... you have to put the name in backticks "`".
Hence, adding backticks to the paste statement inside renderPlot
solves the problem. Try this:
cols <- paste0("c(", paste0("input$`col", sort(input$choice), collapse = "`, "), "`)")
Upvotes: 1