JavaScript for R Shiny Module that uses module's namespace with setInputValue

Hi!

I have a complex modular app in Rshiny where I use ns() in my differents modules.
I have a part where I have to create my inputId directly in my javascript code with "setInputValue". I do a test with a function who prints a random value.

Shiny.setInputValue("testrandom", Math.random());

I do some test without modules so without ns() and it's working.
In the Shiny App with this code:

observeEvent( input$testrandom , {
print(input$testrandom)
})

So the problem is the idInput.
It seems to not be created because I don't have the ns().

So I tried to do some soluce I found on stackOverflow or here but nothing worked.

Test 1 : Shiny.setInputValue(ns("testrandom"), Math.random());

Test 2 : Shiny.setInputValue("namespace-testrandom", Math.random());
source: Shiny.setInputValue in modular app with namespaces

Test 3 : Shiny.setInputValue(NS("explore_module-image", id= "testrandom"), Math.random());

Test 4 : Shiny.setInputValue(ns("explore_module-image"), id="testrandom", Math.random());

I test a lot of different methods but nothing work.

For the test 3 and 4 the "explore_module-image" is the way with my different ns of my app. I concatenate my both ns id because I thought that each ns use are concatenated to form a path?

-> The first module is called with the id "explore_module"

callModule(Module_explore_server, "explore_module", global_data)})

and in this module I call another module with an id ns "image"
callModule(explore_image_server, "image", rv)
and in this module I use the code I write before with the observeEvent.

So I thought that I could give all the pass of ns "explore_module-image" but it's not working.

I would like to ask if anyone have a solution or understand my error ?

Thank you!

because your namespace is explore_module then I think the command would be

 Shiny.setInputValue("explore_module-testrandom", Math.random());

I tried just now, but it is not working explore_module is namespace but there is also image which is a namespace too so maybe I have to use both?

can you produce a minimal reprex? the smallest possible module code(s) and the smallest possible app to call maybe 2 of them and use them in a simple example way, with example data (if needed) taken from R's built in datasets or for images freely available online img urls.
strip out everything irrelevant to the question at hand for the smallest possible code that still shows your issue

I reduce for only have the module with the ns explore_module but it still not working so maybe the command is not the good solution

 Shiny.setInputValue("explore_module-testrandom", Math.random());

I can only guess at your architecture and intentions... would like to help. but need something tangible to analyse. I'm afraid if you want support you will have to do some prepatory work

Thank you for your help, I had an other error which was blocking the app.
Your solution work!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.