JeanVuda
JeanVuda

Reputation: 1778

Show text in main panel of shiny app after click of a button

I am trying to learn shiny. I would like to display the text in main panel only after clicking Go button. So, when the app loads, it will have no text in the main panel. How do I do it? Here is the tweaked code from an example:

library(shiny)

UI<-fluidPage(

  titlePanel("Reactivity"),

  sidebarLayout(
    sidebarPanel(
      textInput("caption", "Caption:", "Data Summary"),

      textInput("TEXT1", "tEXT:", "Data Summary"), 
      submitButton("Go")
    ),

    mainPanel(
      h3(textOutput("caption", container = span)),
      h3(textOutput("TEXT1", container = span))
    )
  )
)

Server<-function(input, output) {

  output$caption <- renderText({
    input$caption
  })

  output$TEXT1 <- renderText({
    input$TEXT1
  })
}

shinyApp(UI, Server)

Upvotes: 3

Views: 9578

Answers (1)

ruchijain90
ruchijain90

Reputation: 181

You can try the following code:

ui.R

library(shiny)

shinyUI(fluidPage(

  titlePanel("Reactivity"),

  sidebarLayout(
    sidebarPanel(
       textInput("caption", "Caption:", "Data Summary"),
       textInput("TEXT1", "tEXT:", "Data Summary"), 
       actionButton("goButton", "Go")
  ),

    mainPanel(
        h3(textOutput("caption", container = span)),
        h3(textOutput("TEXT1", container = span))
       )
     )
   )
 )

server.R

shinyServer(function(input, output) {

   cap <- eventReactive(input$goButton, {
      input$caption
   })

   text <- eventReactive(input$goButton, {
      input$TEXT1
   })

   output$caption <- renderText({
      cap()
   })

   output$TEXT1 <- renderText({
      text()
   })

})

Upvotes: 6

Related Questions