Fonts 如何使checkboxgroupinput颜色编码为闪亮
这有点心胸开阔。我用谷歌做了一些研究,但没有成功 我有一个闪亮的项目,其中我在侧面板中使用checkboxGroupInput,类似这样:Fonts 如何使checkboxgroupinput颜色编码为闪亮,fonts,colors,shiny,controls,Fonts,Colors,Shiny,Controls,这有点心胸开阔。我用谷歌做了一些研究,但没有成功 我有一个闪亮的项目,其中我在侧面板中使用checkboxGroupInput,类似这样: my_checkboxGroupInput <- function(variable, label,choices, selected, colors){ my_names <- choices if(length(names(choices))>0) my_names <- names(choices) div(id=v
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))
})
其中“名称”是一个字符向量
我在主面板的图表中包含图例(不同名称的不同颜色)。现在我想在侧面板中更改名称的文本颜色(每个名称一种颜色),这样我就可以去掉图表中的图例
有人知道怎么做吗?非常感谢。您不能在Shinny中直接执行此操作,但您可以在浏览器检查器中分析Shinny构建的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>
编辑
为了仅以彩色显示选定的项目,您需要稍微修改函数并使用reactiveValue将input$变量映射为当前选择,如下所示:
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))
})
my_checkboxGroupInput您可以使用tags$style
来实现:
lapply(1:length(names), function(x) {
n <- length(names)
col <- gplots::col2hex(rainbow(n)[x])
css_col <- paste0("#variable div.checkbox:nth-child(",x,
") span{color: ", col,";}")
tags$style(type="text/css", css_col)
}),
checkboxGroupInput("variable", "Variable:",
choices = names, selected = names)
lapply(1:长度(名称)、函数(x){
n@HubertL
你的最终版本效果很好。但是,我希望事情更具动态性——我不想永久地为一个选项指定颜色,我更喜欢为N个选定项指定前N个颜色。不幸的是,我定制的代码没有按照我想要的方式工作
例如,我有6种颜色,并且在默认情况下选择了所有六个变量,当我取消检查(二、三、四、五)中的任何一个时,我假设取消检查后的颜色会正确更新。比如说(“蓝色”、“红色”、“绿色”、“紫色”、“柠檬色”、“棕色”)表示(“一”、“二”、“三”、“四”、“五”、“六”);当我取消勾选“三”时,我想看到('1'、'2'、'4'、'5'、'6')的('blue'、'red'、'green'、'purple'、'lemon'、'blue'),但实际颜色是('blue'、'red'、'purple'、'lemon'、'blue')
以下是我用于测试的代码:
my_names您应该尽量避免使用现有的R函数名,如names
来存储您的内容。更喜欢使用my_names
。谢谢提醒。这不是我的代码,我只是复制了一个简单的代码作为示例。请您看看我在您的答案下发布的问题好吗?感谢您为我编写了这么多代码解释。非常有用。让我用你的提示修改我的项目。谢谢,HubertL,你的方式很好。现在,我有一个更高级的问题:如果我只希望选中的复选框进行颜色编码,这意味着,当我取消选中一个复选框时,字体颜色将动态更改为默认颜色(黑色),反之亦然。我尽了最大努力,但没有找到任何交互方式来实现这一点。我没有完全遵循上次更新中的逻辑。您在服务器会话中调用了函数“my_checkboxGroupInput”,但1)复选框显然是输入2)输入变量(“我的$selected当需要对ui进行更深入的控制时,在服务器部分构建ui是很常见的。我从来没有这样做过b4。谢谢,祝你有一个愉快的一天。抱歉,我在我的一个应用程序中测试了此代码,但忘了更改(将编辑此代码).#filespar
是我的checkboxGroupInput
的id。在你的情况下,它将是#variable
。一定要指明checkboxGroupInput
的id,否则我担心你应用程序中的所有checkboxGroupInput
都将按照这些标记$style
着色。你不应该在回答时问问题,提出新问题instead@HubertL对不起,我不知道这个规则。这是新帖子:请删除这个“答案”
lapply(1:length(names), function(x) {
n <- length(names)
col <- gplots::col2hex(rainbow(n)[x])
css_col <- paste0("#variable div.checkbox:nth-child(",x,
") span{color: ", col,";}")
tags$style(type="text/css", css_col)
}),
checkboxGroupInput("variable", "Variable:",
choices = names, selected = names)
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[1:length(my$selected)]))
})