我正在寻找调试错误的方法,该错误是我创建的用于处理R闪亮应用的Tab颜色的函数。我尝试对制表符<-list(...)进行一些操作,但无法确定错误所在。
该函数的工作原理与tabsetPanel函数的结构相同,但增加了tabcolors,该颜色接受必须与tabsetPanel中的制表符长度匹配的颜色列表。
tabsetPanel2 = function (
..., id = NULL, selected = NULL, type = c("tabs", "pills"),
position = c("above", "below", "left", "right"), tabcolors = NULL,
title = NULL) {
if(is.null(colors)){
returner = tabsetPanel(..., id, selected, type, position)
} else {
tabs <- list(...)
if(length(tabs) != length(tabcolors)){stop("The number of colors
must match the number of tabs")}
type <- match.arg(type)
tabset <- buildTabset(tabs, paste0("nav nav-", type), NULL, id,
selected)
for (i in 1:length(tabcolors)){
tabset$navList$children[[i]]$children[[1]]$attribs$style =
paste0("background-color: ",tabcolors[i],";")
tabset$content$children[[i]]$attribs$style = paste0("border:
4px solid", tabcolors[i],";")
}
tabset$navList$attribs$class =
paste0(tabset$navList$attribs$class," nav-stacked col-xs-2")
tabset$navList$attribs$style = "padding-right: 0px;"
tabset$content$attribs$class =
paste0(tabset$content$attribs$class," col-xs-10")
tabset$content$attribs$style = "padding-right: 0px; padding-
left: 0px;"
position <- match.arg(position)
if (position %in% c("above", "left", "right")) {
first <- tabset$navList
second <- tabset$content
} else if (position %in% c("below")) {
first <- tabset$content
second <- tabset$navList
}
if(!is.null(title)){
first$children[[length(tabcolors)+1]] = div(title)
first$children = first$children[c(length(tabcolors) + 1, 1:
(length(tabcolors)))]
}
returner = tags$div(class = paste("tabbable tabs-", position,
sep = ""), first, second)
}
return(returner)
}
environment(tabsetPanel2) = environment(tabsetPanel)
曾经是一个可行的例子。
UI
library(shiny)
shinyUI(fluidPage(
uiOutput("tweak"),
tabsetPanel2(
id = "MainTabs",
type = "tabs",
position = "left",
tabcolors = c("#FFAAAA","#AAFFAA","#AAAAFF"),
title = "Colorful Tabs",
tabPanel(title = "Tab1", "This is tab
1.",tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br()),
tabPanel(title = "Tab2", "This is tab
2.",tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br()),
tabPanel(title = "Tab3",
tabsetPanel(
id = "stuff",
type = "tabs",
position = "above",
tabPanel(title = "InnerTab1", "This is inner tab 1"),
tabPanel(title = "InnerTab2", "This is inner tab 2")
))
)
))
服务器
library(shiny)
shinyServer(function(input, output) {
output$tweak = renderUI({
tagList(list(tags$head(tags$style(HTML("
tabs-left > .nav-tabs{
border-bottom-width: 0px;
}
tabs-left > .nav-tabs > li{
margin-right: 0px;
}
tabs-left > .nav-tabs > li > a{
margin-right: 0px;
border-right-width: 0px;
border-radius: 10px 0px 0px 10px;
border-top-width: 0px;
border-bottom-width: 0px;
border-left-width: 5px;
}
tabs-left > .nav-tabs > li.active{
margin-right: 0px;
}
tabs-left > .nav-tabs > li.active > a,
tabs-left > .nav-tabs > li.active > a:focus,
tabs-left > .nav-tabs > li.active > a:hover{
margin-right: 0px;
border-radius: 10px 0px 0px 10px;
border-right-width: 0px;
border-top-width: 0px;
border-bottom-width: 0px;
border-color: blue;
border-left-width: 5px;
}
")))))
})
})