更新Shiny-R自定义进度条

时间:2016-02-08 14:09:37

标签: r progress-bar shiny

我习惯使用shinyBS包在我的闪亮应用程序中添加一些进度条。但使用bootstrap 3的新版本没有选项。 由于闪亮包含的进度条不能按需定制,我试图重新制作与引导程序3兼容的BS。它运行良好但我无法更新它。

提前感谢您对此有任何帮助!

这是一个例子, 注意:标签和尺寸尚未包含在js中。

服务器:(来自https://gist.github.com/artemklevtsov/d280c4343b052c2aaaef

server <- function(input, output,session) {

tags$script(src="ShinyProgress.js"),
  progressBar <- function(inputId,value = 0, label = FALSE, color = "info", size = NULL,
                      striped = FALSE, active = FALSE, vertical = FALSE) {
if (!is.null(size))
  size <- match.arg(size, c("sm", "xs", "xxs"))
text_value <- paste0(value, "%")
if (vertical)
  style <- htmltools::css(height = text_value, `min-height` = "2em")
else
  style <- htmltools::css(width = text_value, `min-width` = "2em")
htmltools::tags$div(
  class = "progress",
  id=inputId,
  class = if (!is.null(size)) paste0("progress-", size),
  class = if (vertical) "vertical",
  class = if (active) "active",
  htmltools::tags$div(
    class = "progress-bar",
    class = paste0("progress-bar-", color),
    class = if (striped) "progress-bar-striped",
    style = style,
    role = "progressbar",
    `aria-valuenow` = value,
    `aria-valuemin` = 0,
    `aria-valuemax` = 100,
    htmltools::tags$span(class = if (!label) "sr-only", text_value)
  )
)
}

     updatePB=function(session,inputId,value=NULL,label=NULL,color=NULL,size=NULL,striped=NULL,active=NULL,vertical=NULL) {
data <- dropNulls(list(id=inputId,value=value,label=label,color=color,size=size,striped=striped,active=active,vertical=vertical))
session$sendCustomMessage("updateprogress", data)
  }

dropNulls=function(x) {
  x[!vapply(x,is.null,FUN.VALUE=logical(1))]
}

  observe({input$n1 ; updatePB(session,inputId="pb1",value=input$n1)})

    }

用户界面:

ui <- fluidPage(
  numericInput(inputId="n1", label="numeric input", value=10, min = 0, max = 100, step = 1),
mainPanel(progressBar(inputId="pb1",value=10))
)

我将以下js代码添加到www(作为ShinyProgress.js):

Shiny.addCustomMessageHandler("updateprogress",
  function(data) {
$el = $("#"+data.id);
if(data.hasOwnProperty('value')) {
  $el.css('width', data.value+'%').attr('aria-valuenow', data.value);
};
if(data.hasOwnProperty('color')) {
  $el.removeClass("progress-bar-standard progress-bar-info progress-bar-success progress-bar-danger progress-bar-warning");
  $el.addClass("progress-bar-"+data.color);
};
if(data.hasOwnProperty('striped')) {
  $el.toggleClass('progress-bar-striped', data.striped);
};
if(data.hasOwnProperty('active')) {
  $el.toggleClass('active', data.active);
};
if(data.hasOwnProperty('vertical')) {
  $el.toggleClass('vertical', data.vertical);
};
  }
);

编辑:

我能够添加一些说明,当执行js代码时,aria-valuenow和width已经很好地更新但是在主div中因此不考虑修改:

<div aria-valuenow="100" style="width: 100%;" id="pb1">
          <div aria-valuemax="100" aria-valuemin="0" aria-valuenow="0" class="progress-bar progress-bar-info" role="progressbar" style="width:0%;min-width:2em;">
            <span class="sr-only">0%</span>
          </div>
</div>

1 个答案:

答案 0 :(得分:1)

所以解决方案非常简单,只需更改函数中id的级别:

progressBar <- function(inputId, value=0, label=F, color="info", size=NULL, striped=F, active=F, vertical=F) {
 stopifnot(is.numeric(value))
if (value < 0 || value > 100)
stop("'value' should be in the range from 0 to 100", call. = FALSE)
if (!(color %in% shinydashboard:::validColors || color %in% shinydashboard:::validStatuses))
stop("'color' should be a valid status or color.", call. = FALSE)
if (!is.null(size))
size <- match.arg(size, c("sm", "xs", "xxs"))
text_value <- paste0(value, "%")
if (vertical)
style <- htmltools::css(height = text_value, `min-height` = "2em")
else
style <- htmltools::css(width = text_value, `min-width` = "2em")
htmltools::tags$div(
class = "progress",
# id=inputId,
class = if (!is.null(size)) paste0("progress-", size),
class = if (vertical) "vertical",
class = if (active) "active",
htmltools::tags$div(
  id=inputId,
  class = "progress-bar",
  class = paste0("progress-bar-", color),
  class = if (striped) "progress-bar-striped",
  style = style,
  role = "progressbar",
  `aria-valuenow` = value,
  `aria-valuemin` = 0,
  `aria-valuemax` = 100,
  htmltools::tags$span(class = if (!label) "sr-only", text_value)
)
)
}

我希望任何闪亮的开发者都可以添加自定义进度条。