【发布时间】:2020-11-20 15:31:09
【问题描述】:
我在使用闪亮的 Select 输入时遇到问题,如果有人能提供帮助,我将不胜感激。我试图创建一个图,用户可以根据他的要求更改图表的颜色。似乎不知何故,如果 Selectinput 中的输入名称带有连字符,那么渲染图将不会返回任何内容。当我从名称中删除连字符(例如 A2019 而不是 A-2019)时,渲染图会返回正确的图并且一切正常。我已经尝试了几个小时来修复它,但没有任何成功。我在语法上做错了什么?
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)
【问题讨论】:
标签: r shiny shinydashboard