【问题标题】:Selectinput doesn't recognize names with hyphenSelectinput 无法识别带有连字符的名称
【发布时间】: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


    【解决方案1】:

    该问题与 SelectInput 不接受带有连字符的名称无关。通常,如果您想使用连字符或空格或...命名变量或列表元素或数据框列,则必须将名称放在反引号“`”中。

    因此,在renderPlot 内的粘贴语句中添加反引号可以解决问题。试试这个:

    cols <- paste0("c(", paste0("input$`col", sort(input$choice), collapse = "`, "), "`)")
    

    【讨论】:

    • 太棒了。我错过了崩溃中的反引号。但我猜你自己想通了。无论如何,我编辑了我的代码,以防其他人有类似的问题。最好的,斯特凡
    • 是的,这是正确的。对于多项选择,需要折叠中的反引号 :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 2014-10-12
    • 2019-05-24
    • 2013-01-13
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多