Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name space issue with Shiny modules using pre-generated java script links #1261

Closed
gtumuluri opened this issue Jul 25, 2016 · 3 comments
Closed

Comments

@gtumuluri
Copy link

@gtumuluri gtumuluri commented Jul 25, 2016

I am trying to use shiny modules to re-use the UI and server code to present off of three different data sets that share the same presentation.

Running into a bit of a challenge dealing with namespace when using javascript based modal popup link creation outside of the UI / server code.

Here is my non-working app code:

    library(shiny)
    library(shinyBS)
    library(DT)

    df <- data.frame(id = c('a', 'b', 'c'), value = c(1, 2, 3))

    on_click_js = "
    Shiny.onInputChange('myLinkName', '%s');
    $('#myModal').modal('show')
    "

    convert_to_link = function(x) {
      as.character(tags$a(href = "#", onclick = sprintf(on_click_js, x), x))
    }
    df$id_linked <- sapply(df$id, convert_to_link)
    df <- df[, c('id_linked', 'value')]

    mySampleUI <- function(id) {
      ns <- NS(id)

      fluidPage(
        mainPanel(
          dataTableOutput(ns('myDT')),
          bsModal(id = 'myModal',
                  title = 'My Modal Title',
                  trigger = '',
                  size = 'large',
                  textOutput(ns('modalDescription'))
          ),
          width = 12
        )
      )
    }

    ui <- fluidPage(mySampleUI('myUI'))

    myServerFunc <- function(input, output, session, df) {
      output$myDT <- DT::renderDataTable({
        datatable(df, escape = FALSE, selection='none')
      })
      output$modalDescription <- renderText({
        sprintf('My beautiful %s', input$myLinkName)
      })
    }

    server <- function(input, output) {
      callModule(myServerFunc, 'myUI', df)
    }

    shinyApp(ui = ui, server = server)

A working version would successfully display myLinkName in the description portion of the modal pop up. The reason this code does not work is because the UI component ID value is created outside of the UI code without the namespace containment. I get that. But, I am not able to figure out how to re-work it so that the name space matches.

I don't want to generate the table and links dynamically, if avoidable as the table size is large.

Any ideas / options?

UPDATE:

Using a namespace definition 'outside' and then using paste to string together the java script line seems to work. But, this seems like a gigantic hack.

ns <- NS('myUI')

on_click_js = paste("
Shiny.onInputChange('", ns('myLinkName'), "', '%s');
$('#myModal').modal('show')", sep = "")

Is this the best that can be done?

@wch
Copy link
Collaborator

@wch wch commented Sep 20, 2016

I'm not able to follow the example. Could you provide a simplified version that strips it down to just the necessary components?

@gtumuluri
Copy link
Author

@gtumuluri gtumuluri commented Sep 20, 2016

Yeah, that example is a bit crazy. When I first ran into the problem, that was the app scenario. But, I now know there are others like the one show in this example. I answered my own question after getting it to work with 'global' NS function:
http://stackoverflow.com/questions/39273043/shiny-modules-not-working-with-renderui

It seems like you need the NS function to be global to write shiny module code that works on UI / server side. I found it to be hacky at first. But, I am not sure there is any better solution.

@alandipert
Copy link
Contributor

@alandipert alandipert commented Apr 29, 2019

Closing because the original issue was resolved by using the NS() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants