Using values selected from a javascript based select menu in shiny












1















I am using the jQuery plugin ComboTree to display a tree-like select menu in my shiny app.enter image description here



I am having trouble retrieving those values (e.g. c("Item 2", "Item 2-1")) to use in some output. So the issue here is to retrieve whatever values are selected from the select menu ($("example").val();).



ui.r:



ui <- function(){
fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("myData.json"),
# layouy content ----
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu")
),
mainPanel(width = 9)
)
)
}


server.r:



server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

# want to do some manipulation with the resulting selections from the
# combo tree. Something along the lines of:

# selections <- eventReactive(input$click, {
# return(input$comboTreeSelections)
# })
}


example.js:



comboTree1 = $('#example').comboTree({
source: myData,
isMultiple: true
});


myData.json:



var myData = [
{
id: 0,
title: 'Item 1 '
}, {
id: 1,
title: 'Item 2',
subs: [
{
id: 10,
title: 'Item 2-1'
}, {
id: 11,
title: 'Item 2-2'
}, {
id: 12,
title: 'Item 2-3'
}
]
}, {
id: 2,
title: 'Item 3'
}
];


I've tried to add an extra piece of js script like so:



selectedValues = $("#example").val();

Shiny.onInputChange("comboTreeSelections", selectedValues);


Thank you!










share|improve this question




















  • 1





    Did you check out this package shinyTree?

    – SeGa
    Nov 29 '18 at 15:23











  • Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

    – JdeMello
    Nov 29 '18 at 15:32











  • Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

    – SeGa
    Nov 29 '18 at 16:13











  • Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

    – JdeMello
    Nov 29 '18 at 16:26
















1















I am using the jQuery plugin ComboTree to display a tree-like select menu in my shiny app.enter image description here



I am having trouble retrieving those values (e.g. c("Item 2", "Item 2-1")) to use in some output. So the issue here is to retrieve whatever values are selected from the select menu ($("example").val();).



ui.r:



ui <- function(){
fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("myData.json"),
# layouy content ----
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu")
),
mainPanel(width = 9)
)
)
}


server.r:



server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

# want to do some manipulation with the resulting selections from the
# combo tree. Something along the lines of:

# selections <- eventReactive(input$click, {
# return(input$comboTreeSelections)
# })
}


example.js:



comboTree1 = $('#example').comboTree({
source: myData,
isMultiple: true
});


myData.json:



var myData = [
{
id: 0,
title: 'Item 1 '
}, {
id: 1,
title: 'Item 2',
subs: [
{
id: 10,
title: 'Item 2-1'
}, {
id: 11,
title: 'Item 2-2'
}, {
id: 12,
title: 'Item 2-3'
}
]
}, {
id: 2,
title: 'Item 3'
}
];


I've tried to add an extra piece of js script like so:



selectedValues = $("#example").val();

Shiny.onInputChange("comboTreeSelections", selectedValues);


Thank you!










share|improve this question




















  • 1





    Did you check out this package shinyTree?

    – SeGa
    Nov 29 '18 at 15:23











  • Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

    – JdeMello
    Nov 29 '18 at 15:32











  • Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

    – SeGa
    Nov 29 '18 at 16:13











  • Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

    – JdeMello
    Nov 29 '18 at 16:26














1












1








1








I am using the jQuery plugin ComboTree to display a tree-like select menu in my shiny app.enter image description here



I am having trouble retrieving those values (e.g. c("Item 2", "Item 2-1")) to use in some output. So the issue here is to retrieve whatever values are selected from the select menu ($("example").val();).



ui.r:



ui <- function(){
fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("myData.json"),
# layouy content ----
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu")
),
mainPanel(width = 9)
)
)
}


server.r:



server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

# want to do some manipulation with the resulting selections from the
# combo tree. Something along the lines of:

# selections <- eventReactive(input$click, {
# return(input$comboTreeSelections)
# })
}


example.js:



comboTree1 = $('#example').comboTree({
source: myData,
isMultiple: true
});


myData.json:



var myData = [
{
id: 0,
title: 'Item 1 '
}, {
id: 1,
title: 'Item 2',
subs: [
{
id: 10,
title: 'Item 2-1'
}, {
id: 11,
title: 'Item 2-2'
}, {
id: 12,
title: 'Item 2-3'
}
]
}, {
id: 2,
title: 'Item 3'
}
];


I've tried to add an extra piece of js script like so:



selectedValues = $("#example").val();

Shiny.onInputChange("comboTreeSelections", selectedValues);


Thank you!










share|improve this question
















I am using the jQuery plugin ComboTree to display a tree-like select menu in my shiny app.enter image description here



I am having trouble retrieving those values (e.g. c("Item 2", "Item 2-1")) to use in some output. So the issue here is to retrieve whatever values are selected from the select menu ($("example").val();).



ui.r:



ui <- function(){
fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("myData.json"),
# layouy content ----
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu")
),
mainPanel(width = 9)
)
)
}


server.r:



server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

# want to do some manipulation with the resulting selections from the
# combo tree. Something along the lines of:

# selections <- eventReactive(input$click, {
# return(input$comboTreeSelections)
# })
}


example.js:



comboTree1 = $('#example').comboTree({
source: myData,
isMultiple: true
});


myData.json:



var myData = [
{
id: 0,
title: 'Item 1 '
}, {
id: 1,
title: 'Item 2',
subs: [
{
id: 10,
title: 'Item 2-1'
}, {
id: 11,
title: 'Item 2-2'
}, {
id: 12,
title: 'Item 2-3'
}
]
}, {
id: 2,
title: 'Item 3'
}
];


I've tried to add an extra piece of js script like so:



selectedValues = $("#example").val();

Shiny.onInputChange("comboTreeSelections", selectedValues);


Thank you!







javascript r shiny






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 16:26







JdeMello

















asked Nov 28 '18 at 21:25









JdeMelloJdeMello

781418




781418








  • 1





    Did you check out this package shinyTree?

    – SeGa
    Nov 29 '18 at 15:23











  • Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

    – JdeMello
    Nov 29 '18 at 15:32











  • Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

    – SeGa
    Nov 29 '18 at 16:13











  • Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

    – JdeMello
    Nov 29 '18 at 16:26














  • 1





    Did you check out this package shinyTree?

    – SeGa
    Nov 29 '18 at 15:23











  • Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

    – JdeMello
    Nov 29 '18 at 15:32











  • Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

    – SeGa
    Nov 29 '18 at 16:13











  • Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

    – JdeMello
    Nov 29 '18 at 16:26








1




1





Did you check out this package shinyTree?

– SeGa
Nov 29 '18 at 15:23





Did you check out this package shinyTree?

– SeGa
Nov 29 '18 at 15:23













Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

– JdeMello
Nov 29 '18 at 15:32





Thanks @SeGa. Definitely checking shinyTree. I will leave this question opened though in case someone has a different suggestion or solution. Cheers

– JdeMello
Nov 29 '18 at 15:32













Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

– SeGa
Nov 29 '18 at 16:13





Your js-file might have a wrong name too (combroTreePlugin). Shouldnt it be comboTreePlugin without "r"?

– SeGa
Nov 29 '18 at 16:13













Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

– JdeMello
Nov 29 '18 at 16:26





Ooops, I mistakenly added an 'r' to my .js source plugin file. The app works though (as evidenced by screenshot). I will correct it in the question anyway. thanks

– JdeMello
Nov 29 '18 at 16:26












2 Answers
2






active

oldest

votes


















1














This is just a quick fix, as I don't really recommend using a pure jQuery plugin, since you will have to write all the interaction between combotree and Shiny yourself. But when you're only interested in the actual selected items, you could do this:



In comboTreePlugin.js change the function at line 129 to:



this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);

var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem);
});


This example will only work, when you really click on an item, it wont fire when you select an item by hitting Enter. You would have to copy/paste the last 2 lines above in the keydown-event handler (code 13).



Then you can access the variable selTitle with input$selTitle in Shiny.





Here's a small ShinyApp which prints out the selected titles:



library(shiny)

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer


























  • Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

    – JdeMello
    Nov 29 '18 at 19:58






  • 1





    I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

    – SeGa
    Nov 29 '18 at 20:12



















1














I found another method, where you dont have to mess with the source code and just inject some javascript.
This will trigger a setInterval function, when the dropdown is visible/openend and will re-run every 500ms.



library(shiny)

js <- HTML("
$(function() {
var selection = setInterval(function() {
if($('.comboTreeDropDownContainer').is(':visible')) {
var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem)
}
}, 500);
});
")

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$script(js),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer
























  • Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

    – JdeMello
    Nov 29 '18 at 20:55












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528354%2fusing-values-selected-from-a-javascript-based-select-menu-in-shiny%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is just a quick fix, as I don't really recommend using a pure jQuery plugin, since you will have to write all the interaction between combotree and Shiny yourself. But when you're only interested in the actual selected items, you could do this:



In comboTreePlugin.js change the function at line 129 to:



this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);

var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem);
});


This example will only work, when you really click on an item, it wont fire when you select an item by hitting Enter. You would have to copy/paste the last 2 lines above in the keydown-event handler (code 13).



Then you can access the variable selTitle with input$selTitle in Shiny.





Here's a small ShinyApp which prints out the selected titles:



library(shiny)

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer


























  • Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

    – JdeMello
    Nov 29 '18 at 19:58






  • 1





    I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

    – SeGa
    Nov 29 '18 at 20:12
















1














This is just a quick fix, as I don't really recommend using a pure jQuery plugin, since you will have to write all the interaction between combotree and Shiny yourself. But when you're only interested in the actual selected items, you could do this:



In comboTreePlugin.js change the function at line 129 to:



this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);

var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem);
});


This example will only work, when you really click on an item, it wont fire when you select an item by hitting Enter. You would have to copy/paste the last 2 lines above in the keydown-event handler (code 13).



Then you can access the variable selTitle with input$selTitle in Shiny.





Here's a small ShinyApp which prints out the selected titles:



library(shiny)

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer


























  • Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

    – JdeMello
    Nov 29 '18 at 19:58






  • 1





    I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

    – SeGa
    Nov 29 '18 at 20:12














1












1








1







This is just a quick fix, as I don't really recommend using a pure jQuery plugin, since you will have to write all the interaction between combotree and Shiny yourself. But when you're only interested in the actual selected items, you could do this:



In comboTreePlugin.js change the function at line 129 to:



this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);

var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem);
});


This example will only work, when you really click on an item, it wont fire when you select an item by hitting Enter. You would have to copy/paste the last 2 lines above in the keydown-event handler (code 13).



Then you can access the variable selTitle with input$selTitle in Shiny.





Here's a small ShinyApp which prints out the selected titles:



library(shiny)

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer















This is just a quick fix, as I don't really recommend using a pure jQuery plugin, since you will have to write all the interaction between combotree and Shiny yourself. But when you're only interested in the actual selected items, you could do this:



In comboTreePlugin.js change the function at line 129 to:



this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);

var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem);
});


This example will only work, when you really click on an item, it wont fire when you select an item by hitting Enter. You would have to copy/paste the last 2 lines above in the keydown-event handler (code 13).



Then you can access the variable selTitle with input$selTitle in Shiny.





Here's a small ShinyApp which prints out the selected titles:



library(shiny)

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 '18 at 6:07









marc_s

583k13011241270




583k13011241270










answered Nov 29 '18 at 19:31









SeGaSeGa

4,72631136




4,72631136













  • Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

    – JdeMello
    Nov 29 '18 at 19:58






  • 1





    I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

    – SeGa
    Nov 29 '18 at 20:12



















  • Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

    – JdeMello
    Nov 29 '18 at 19:58






  • 1





    I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

    – SeGa
    Nov 29 '18 at 20:12

















Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

– JdeMello
Nov 29 '18 at 19:58





Thanks for this @SeGa. I still wonder if there is a simpler solution by injecting some js code on ui.r. Something along the lines of tags$script('$(document).on("shiny:sessioninitialized", function() {$("#example").on("change", function() {var = $("#example").val(); Shiny.onInputChange("selTitle", var);});}). Thanks again!

– JdeMello
Nov 29 '18 at 19:58




1




1





I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

– SeGa
Nov 29 '18 at 20:12





I think thats the easiest way. Since #example is just a select input with a click event, it wont trigger everytime the selection change just when you click inside the select input. You may have to add some event delegation on that comboTree list. That might work. I tried to add some click events on .ComboTreeItemParent/.ComboTreeItemChlid/.comboTreeDropDownContainer without success.

– SeGa
Nov 29 '18 at 20:12













1














I found another method, where you dont have to mess with the source code and just inject some javascript.
This will trigger a setInterval function, when the dropdown is visible/openend and will re-run every 500ms.



library(shiny)

js <- HTML("
$(function() {
var selection = setInterval(function() {
if($('.comboTreeDropDownContainer').is(':visible')) {
var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem)
}
}, 500);
});
")

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$script(js),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer
























  • Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

    – JdeMello
    Nov 29 '18 at 20:55
















1














I found another method, where you dont have to mess with the source code and just inject some javascript.
This will trigger a setInterval function, when the dropdown is visible/openend and will re-run every 500ms.



library(shiny)

js <- HTML("
$(function() {
var selection = setInterval(function() {
if($('.comboTreeDropDownContainer').is(':visible')) {
var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem)
}
}, 500);
});
")

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$script(js),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer
























  • Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

    – JdeMello
    Nov 29 '18 at 20:55














1












1








1







I found another method, where you dont have to mess with the source code and just inject some javascript.
This will trigger a setInterval function, when the dropdown is visible/openend and will re-run every 500ms.



library(shiny)

js <- HTML("
$(function() {
var selection = setInterval(function() {
if($('.comboTreeDropDownContainer').is(':visible')) {
var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem)
}
}, 500);
});
")

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$script(js),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)





share|improve this answer













I found another method, where you dont have to mess with the source code and just inject some javascript.
This will trigger a setInterval function, when the dropdown is visible/openend and will re-run every 500ms.



library(shiny)

js <- HTML("
$(function() {
var selection = setInterval(function() {
if($('.comboTreeDropDownContainer').is(':visible')) {
var selItem = comboTree1.getSelectedItemsTitle();
Shiny.onInputChange('selTitle', selItem)
}
}, 500);
});
")

ui <- {fluidPage(
tags$head(
tags$script(src = "comboTreePlugin.js"),
tags$script(src = "icontains.js"),
tags$script(js),
tags$link(rel = "stylesheet", type = "text/css", href = "comboTreeStyle.css")
),
includeScript("www/myData.json"),
sidebarLayout(
sidebarPanel(width = 3,
tags$input(type = "text", id = "example", placeholder = "Select"),
uiOutput("comboTreeMenu"),
verbatimTextOutput("selected")
),
mainPanel(width = 9)
)
)}

server <- function(input, output, session){
output$comboTreeMenu <- renderUI({
includeScript("www/example.js")
})

output$selected <- renderPrint({
req(input$selTitle)
print(input$selTitle)
})
}

shinyApp(ui, server)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '18 at 20:37









SeGaSeGa

4,72631136




4,72631136













  • Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

    – JdeMello
    Nov 29 '18 at 20:55



















  • Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

    – JdeMello
    Nov 29 '18 at 20:55

















Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

– JdeMello
Nov 29 '18 at 20:55





Thank you. So setInterval() runs "indefinitely" every .5s whenever the combo tree menu is visible. Brilliant

– JdeMello
Nov 29 '18 at 20:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528354%2fusing-values-selected-from-a-javascript-based-select-menu-in-shiny%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks