【问题标题】:Show text in main panel of shiny app after click of a button单击按钮后在闪亮应用程序的主面板中显示文本
【发布时间】:2015-12-22 04:08:22
【问题描述】:

我正在努力学习闪亮。我只想在单击 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以试试下面的代码:

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

    服务器.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()
       })
    
    })
    

    【讨论】:

    • 它有效。是否可以将output$caption &lt;- renderText({ cap() }) output$TEXT1 &lt;- renderText({ text() }) 放入函数中。
    • 在 ui.R 中做如下更改:mainPanel( verbatimTextOutput("o1") ) 并在 server.R 中做以下更改:shinyServer(function(input, output) { inputs &lt;- eventReactive(input$goButton, { c(input$caption, input$TEXT1) }) output$o1 &lt;- renderPrint({ cat(inputs()[1], "\n", inputs()[2]) }) })
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2021-12-28
    • 2021-12-15
    • 2023-03-27
    • 2015-03-02
    • 2018-07-14
    • 2018-09-07
    相关资源
    最近更新 更多