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

Allow DOM element replacement in htmlOutput #694

Closed
wants to merge 1 commit into from

Conversation

wch
Copy link
Collaborator

@wch wch commented Jan 21, 2015

This PR adds a replace option to htmlOutput(). When set to TRUE, it tells Shiny that the output from renderUI should replace the container DOM element, instead of being placed inside of the container. This is useful in cases where an intervening <div> or <span> element isn't allowed, or will interfere with CSS selectors.

When replacement happens, the new element gets the old element's ID, and also gets assigned the classes "shiny-html-output shiny-html-replace-output". If renderUI returns something other than a single tag (like a tagList or bare text), it's not possible to replace the container element with the new content, preserving ID and and keeping those classes; in this case, the content will be placed inside the container element, as though replace=FALSE.

This example doesn't show the proper styling for the li (red text), because of the extra div in the DOM structure:

shinyApp(
  ui = bootstrapPage(
    tags$head(tags$style(HTML("ul > li { color: red; }"))),
    tags$ul(
      tags$li("static item"),
      uiOutput("item")
    )
  ),
  server = function(input, output) {
    output$item <- renderUI(tags$li("dynamic item"))
  }
)

With replace=TRUE, the li comes up with the correct styling:

shinyApp(
  ui = bootstrapPage(
    tags$head(tags$style(HTML("ul > li { color: red; }"))),
    tags$ul(
      tags$li("static item"),
      uiOutput("item", replace = TRUE)
    )
  ),
  server = function(input, output) {
    output$item <- renderUI(tags$li("dynamic item"))
  }
)

This addresses rstudio/shinydashboard#1.

@wch
Copy link
Collaborator Author

wch commented Jan 27, 2015

Closing because:

  • Items updated with this code can only be updated once.
  • To allow further updates, it requires changes to the output binding behavior.
  • Changing the binding behavior is too risky for the relatively small benefit.

@wch wch closed this Jan 27, 2015
@wch wch deleted the html-replace branch February 4, 2015 19:34
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

Successfully merging this pull request may close these issues.

1 participant