javascript not working in a Shiny module

383 views
Skip to first unread message

Stevan Earl

unread,
Aug 1, 2017, 8:04:27 PM8/1/17
to Shiny - Web Framework for R
Hi Folks - I have a Shiny app that allows a user to interactively delete rows from a dataTable, which deletes the corresponding record(s) from a database. This app worked great until I tried to implement it in a Shiny module upon which the dataTable still renders but the user can no longer delete a record. I have a feeling this may be due to the name space, and I found a nice thread about this here where the author identified a work around by including the name space in his javascript, though I am not sure if that is definitely the problem in my case or how I would include the name space in the javascript used in this instance. I need to construct many such dataTables so would really like to be able to put this in a module but need also to maintain that ability to delete records/rows. Any thoughts would be appreciated. Thanks, Stevan

shinyInput <- function(FUN, len, id, ...) {
  inputs
<- character(len)
 
for (i in seq_len(len)) {
    ident
= as.character(insectData() %>% select(insect_count_id) %>% slice(i:i))
    inputs
[i] <- as.character(FUN(paste0(id, ident), ...))
 
}
  inputs
}


tableButtons
<- reactive({

  insectData
() %>%
    mutate
(Delete = shinyInput(actionButton, nrow(insectData()), '', label = "Delete", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)'))

})


output$buttonsInTable
<- renderDataTable({

  tableButtons
()

}, escape = FALSE, options = list(bFilter = 0, bLengthChange = F, bPaginate = F, bSort = T), rownames = F)


observeEvent
(input$select_button, {

  pg_local
<- databaseConn()

  deleteInsectCountBaseQuery
<- '
    DELETE FROM insectDataBase.sweepnet_sample_insect_counts
    WHERE insect_count_id = ?insectCountID;'


  deleteInsectCountQuery
<- sqlInterpolate(ANSI(), deleteInsectCountBaseQuery, insectCountID = as.numeric(input$select_button))

  dbExecute
(pg_local, deleteInsectCountQuery)

  dbDisconnect
(pg_local)

 
# change listener state when deleting a record
  rv$dbVersion
<- isolate(rv$dbVersion + 1)

})


Carl Ganz

unread,
Aug 2, 2017, 12:29:48 AM8/2/17
to Shiny - Web Framework for R
Hello,

You are correct that this is a namespace issue.

When you use Shiny.onInputChange("select_button") it triggers input$select_button in a regular app but in a module this will not work. Only inputs with that modules prefix will be accessible in the inputs for that module's server function.

So if you replace "select_button" with ns("select_button") it will work.

Kind Regards

Stevan Earl

unread,
Aug 2, 2017, 1:38:29 PM8/2/17
to Shiny - Web Framework for R
Hi Carl,

Thank you for taking the time to reply. The name space issue is indeed tricky. Unfortunately, changing "select_button" to ns("select_button") did not seem to do the trick in this case. Might there be a different or additional location where I may need to reference the ns, such as in the observeEvent, as well?


Thanks again,
Stevan

Stevan Earl

unread,
Aug 2, 2017, 5:00:02 PM8/2/17
to Shiny - Web Framework for R

A bit more on this issue of javascript in Modules. It is curious to me that the ns comes into play at all in this case since all of the javascript functionality is occurring WITHIN the Module. Anyway, Carl has hit on something here as, though I was not able to get ns("select_button") to work, I can get this to work if I hard-code the name space id. For example, if I am using the name space id of "demo", then the following code does provide the desired functionality:

mutate(Delete = shinyInput(actionButton, nrow(insectData()), '', label = "Delete", onclick = 'Shiny.onInputChange("demo-select_button",  this.id)'))

That is a huge step forward, but obviously it is counter to the point to have to hard-code the ns id into Module functions. I am still not sure why Carl's excellent suggestion of adding ns("select_button") (e.g., below) did not work but if anyone has insight on where I am going wrong with my syntax your comments would be appreciated.

mutate(Delete = shinyInput(actionButton, nrow(insectData()), '', label = "Delete", onclick = 'Shiny.onInputChange(ns("select_button"),  this.id)'))


Thanks!

Joe Cheng

unread,
Aug 2, 2017, 5:05:50 PM8/2/17
to Stevan Earl, Shiny - Web Framework for R
Can you try replacing

'Shiny.onInputChange(ns("select_button"),  this.id)'

with

sprintf('Shiny.onInputChange("%s",  this.id)', ns("select_button"))

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ae644c5b-70f3-4485-8f59-8caf3f91ca29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Cheng

unread,
Aug 2, 2017, 5:06:44 PM8/2/17
to Stevan Earl, Shiny - Web Framework for R
Sorry, should be:

sprintf('Shiny.onInputChange("%s",  this.id)', session$ns("select_button"))

Stevan Earl

unread,
Aug 2, 2017, 5:11:09 PM8/2/17
to Shiny - Web Framework for R, steva...@asu.edu
Perfect, thanks Joe (and Carl)!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages