How to pass a R vector to shinyjs as a JS array?

701 views Asked by At

I would like to pass a vector to extendshinyjs as a JS array, but it seems that the vector is actually passed as a string. Here is my code:

ui.R

shinyUI( fluidPage(
  useShinyjs(),
  extendShinyjs(text = jsCode),
 tags$div('color names',
  tags$p(class='colorName'),
  tags$p(class='colorName'),
  tags$p(class='colorName')
 )


))

server.R

shinyServer(
    function(input,output,session) {
      x <- c('red', 'yellow', 'green')
        js$pageCol(x)
      })

global.R

library(shinyjs)
library(shiny)
jscode <-  jsCode <- "shinyjs.pageCol = function(params){
                             var $spots = $('p').text(params);
      defaultParams = ['NA','NA','NA'];
      val = shinyjs.getParams(params, defaultParams);


$spots.each(function(i) {
$(this).text(val[i]);
}); 

}"

On the right what it looks like now, on the left what I would expect to see

On the right how it looks like now, on the **left** what I would expect to see

1

There are 1 answers

1
DeanAttali On

The vector is not getting passed as a string, it is being converted to a javascript array. The javascript parameter is an array with one element, where that element is the vector that you passed in (just as I told you in the other thread - I wasn't lying!)

Try this:

shinyjs.pageCol = function(params){
  var cols = params[0];
  $('p').each(
    function(i, el) {
      $(el).text(cols[i]);
    }
  );
}