Ben February 2016

Update Shiny-R custom progressbar

I use to put some progressbar in my shiny apps using shinyBS package. But the new version working with bootstrap 3 does not have the option. As shiny included progressbar is not customizable as wanted, I tried to remake the BS one compatible with bootstrap 3. It works well but I do not manage to update it.

Thanks in advance for any help about this!

Here is an exemple, NB : label and size are not included in the js yet.

Server : (from 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)
  }

d        

Answers


Ben February 2016

So the solution was quite easy, just change the level of the id in the function :

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)
)
)
}

I hope that it will be helpfull for any shiny developper to add custom progressbar.

Post Status

Asked in February 2016
Viewed 3,644 times
Voted 13
Answered 1 times

Search




Leave an answer