单击按钮后,在闪亮应用程序的主面板中显示文本

时间:2015-12-22 04:08:22

标签: r shiny

我正在努力学习闪亮。我想在单击Go按钮后才在主面板中显示文本。因此,当应用加载时,主面板中将没有文本。我该怎么做? 以下是来自示例的调整代码:

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)

1 个答案:

答案 0 :(得分:5)

您可以尝试以下代码:

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()
   })

})