思想有点开放。我用谷歌做了一些研究,但没有成功。
我有一个 Shiny 的项目,其中我在侧面板中使用 checkboxGroupInput,如下所示:
checkboxGroupInput("variable", "Variable:", choices = names ,selected = names )
其中“名称”是字符向量。
我在主面板的图表中包含图例(不同名称的不同颜色)。现在我正在考虑更改侧面板中名称的文本颜色(每个名称一种颜色),以便我可以摆脱图表中的图例。
有人知道怎么做吗?非常感谢。
最佳答案
你不能直接在shiny中执行此操作,但你可以在浏览器检查器中分析shiny构建的HTML/
<div id="variable" class="form-group shiny-input-checkboxgroup shiny-input-container">
<label class="control-label" for="variable">Variable:</label>
<div class="shiny-options-group">
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="1" checked="checked"/>
<span>one</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="2" checked="checked"/>
<span>two</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="3"/>
<span>three</span>
</label>
</div>
</div>
</div>
然后您可以使用renderUI
重新创建它:
my_checkboxGroupInput <- function(variable, label, choices, selected, colors){
choices_names <- choices
if(length(names(choices))>0) my_names <- names(choices)
div(id=variable,class="form-group shiny-input-checkboxgroup shiny-input-container shiny-bound-input",
HTML(paste0('<label class="control-label" for="',variable,'">',label,'</label>')),
div( class="shiny-options-group",
HTML(paste0('<div class="checkbox" style="color:', colors,'">',
'<label>',
'<input type="checkbox" name="', variable,
'" value="', choices,
'"', ifelse(choices %in% selected, 'checked="checked"', ''),
'/>',
'<span>', choices_names,'</span>',
'</label>',
'</div>', collapse = " "))
)
)
}
library(shiny)
my_names <- c('one'=1,'two'=2,'three'=3)
my_selected <- c(1,2)
my_colors <-c('blue','red','green')
shinyApp(
ui=fluidPage(uiOutput("my_cbgi")),
server = function(input, output, session) {
output$my_cbgi <- renderUI(my_checkboxGroupInput("variable", "Variable:",
choices = my_names,
selected=my_selected,
colors=my_colors))
}
)
编辑
为了仅以颜色显示选定的项目,您需要稍微修改函数并使用reactiveValue将input$variable映射为当前选择,如下所示:
my_checkboxGroupInput <- function(variable, label,choices, selected, colors){
my_names <- choices
if(length(names(choices))>0) my_names <- names(choices)
div(id=variable,class="form-group shiny-input-checkboxgroup shiny-input-container shiny-bound-input",
HTML(paste0('<label class="control-label" for="',variable,'">',label,'</label>')),
div( class="shiny-options-group",
HTML(paste0('<div class="checkbox">',
'<label>',
'<input type="checkbox" name="', variable,
'" value="', choices,
'"', ifelse(choices %in% selected, 'checked="checked"', ''),
'/>',
'<span ', ifelse(choices %in% selected, paste0('style="color:', colors,'"'),''), '>',my_names,'</span>',
'</label>',
'</div>', collapse = " "))
)
)
}
my_names <- c('one'=1,'two'=2,'three'=3)
my_selected <- c(1,2)
my_colors <-c('blue','red','green')
shinyApp(ui=fluidPage(uiOutput("my_cbgi")),
server = function(input, output, session) {
my <- reactiveValues(selected=my_selected)
observeEvent(input$variable,{my$selected <- input$variable})
output$my_cbgi <- renderUI(my_checkboxGroupInput("variable", "Variable:",
choices = my_names,
selected=my$selected,
colors=my_colors))
})
关于fonts - 如何在 Shiny 中使 checkboxgroupinput 进行颜色编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41813960/