Open yonicd opened 7 years ago
Here is how I would do it. The problem with the earlier approach is that dotImages
is trapped in the JSON payload of the widget and as a result is not available directly in the global scope. There is a way to access it, but a simpler approach would be to directly define it in the global scope.
s2 <- htmltools::tags$script(
sprintf("var dotImages = %s", jsonlite::toJSON(teamImg))
)
s1 <- slickR(
images = playerTable$img,
dotImages = teamImg,
slickOpts = list(
initialSlide = 0,
slidesToShow = 5,
slidesToScroll = 5,
focusOnSelect = T,
dots = T,
customPaging = cP2
)
)
htmltools::browsable(htmltools::tagList(s2, s1))
thanks! that works great for a once over solution. could i call those in the main slickR function call? I would guess that regular users (like me) wouldnt do that in a natural way. I think that I would need to write it in for them under the hood. could i take advantage of putting it in the 'shared variables for this instance'?
thanks again and sorry for the newbie questions
this is the best I could come up with.
Is there a more efficient way?
thanks again
slickR <- function(images ,
slideId='baseDiv',
slideIdx=list(1:length(images)),
slickOpts=list(dots=T),
synchSlides=NULL,
dotImages=NULL,
width = NULL,
height = NULL,
elementId = NULL) {
if(!is.character(images)) break('images must be a character vector')
images=lapply(images,function(x){
if(!grepl('www|http|https|data:image/',x)) x=readImage(x)
x
})
if(length(images)>1) images=unlist(images)
if(length(slideId)!=length(slideIdx)) slideId=paste0('baseDiv',1:length(slideId))
x = vector('list',length(slideIdx))
for(xId in 1:length(x)){
x[[xId]]$divName=slideId[xId]
x[[xId]]$images=images[slideIdx[[xId]]]
if(length(slickOpts)>0){
if(all(sapply(slickOpts,class)=='list')){
sOL=slickOpts[[xId]]
}else{
sOL=slickOpts
}
if(!is.null(synchSlides)){
sOL$asNavFor=sprintf(".%s",synchSlides[!(synchSlides%in%slideId[xId])])
}
#<------------ Chunk to set dotImages in global js scope
s2=NULL
if(!is.null(dotImages)){
s2 <- htmltools::tags$script(
sprintf("var dotImages = %s", jsonlite::toJSON(dotImages))
)
x[[xId]]$dotImages=dotImages
}
#<-----------------
if(!is.null(sOL[[1]])) x[[xId]]$slickOpts=sOL
}
}
# forward options using x
# create widget
s1=htmlwidgets::createWidget(
name = 'slickR',
x,
width = width,
height = height,
package = 'slickR',
elementId = elementId
)
#<------- Ifelse to handle existence of dotImages output
if(!is.null(s2)) {
htmltools::browsable(htmltools::tagList(s2, s1))
}else{
s1
}
}
I think there is a better way to do this. I will try and post it over the weekend.
I am trying to use customPaging in slickjs as part of building the functionality of slickR. when I use the JS function as something simple like cP1 there is no problem. But when I try to call a function that needs a variable on the global scope I get errors in the console log that seem to hint at a problem in the htmlwidget library
below is the script I am running, and here is an analog jsfiddle that works. and this is the slickR.js
Thanks for any pointers to what I am doing wrong with JS(.)