King Frazier
King Frazier

Reputation: 343

opening a new empty shiny ui through actionbutton

My objective is to create a ShinyApp that opens a new empty UI whenever user clicks on submitButton.

Currently this is my code below. If the user types something in the text box and press Submit. The app shows what the user typed in the main panel. However I dont want to see the text, instead when the user clicks on the submit button , it should open a new empty UI.

ui = shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    column(3, wellPanel(

      textInput("text", "Text:", "text here"),
      submitButton("Submit")
    )),

           verbatimTextOutput("text")
    )
  )
)


server = function(input, output) {
  output$plot1 <- renderPlot({
    hist(rnorm(input$n))
  })

  output$text <- renderText({
    paste("Input text is:", input$text)
  })
}


shinyApp(ui=ui, server=server)

Is this possible ? Any tips or pointers are appreciated.

Upvotes: 2

Views: 2110

Answers (1)

K. Rohde
K. Rohde

Reputation: 9676

Well, this is not yet very functional, but does what you asked for.

ui = shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    uiOutput("newWindowContent", style = "display: none;"),
    tags$script(HTML("
      $(document).ready(function() {
        if(window.location.hash != '') {
          $('div:not(#newWindowContent)').hide();
          $('#newWindowContent').show();
          $('#newWindowContent').appendTo('body');
        }
      })
    ")),
    a(href = "#NEW", target = "_blank",
      actionButton("Submit", "Submit")
    )
  ))
)


server = function(input, output) {

  output$newWindowContent <- renderUI({
    "Welcome to your new window!"
  })
}

shinyApp(ui=ui, server=server)

The app is created, such that the ui created in newWindowContent is displayed in the new window. Sadly, new windows are somewhat cut off from the parent page, such that there is no easy way to configure each page independently. At the moment, all show the same content. None have reactivity features. I guess there can be initial configurations, if one uses the window's hash. But this works only client sided.

Nevertheless, it's a good start!

Upvotes: 2

Related Questions