为闪亮应用程序中的框设置默认文本值

时间:2018-02-22 16:40:05

标签: r shiny

我收到了以下Shiny Application:

## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(textOutput("button_outcome")),
      box(width = 2,  actionButton("runRF", "Predict"))
    )
  )
)


server <- function(input, output) {

  observeEvent(input$runRF, {
    #output$button_outcome = DT::renderDataTable({
    # titanic_train <- select(titanic_train, Pclass, Name)
    # head(titanic_train,5)
    #})
    output$button_outcome = renderPrint({ "foo" })
  })

}

shinyApp(ui, server)

这基本上打印了文本&#34; foo&#34;当我按下按钮。这一切都有效。但是,我想要的是一个基本的文字,例如&#34;请按下按钮&#34;当你按下按钮时,它被&#34; foo&#34;

取代

所以我正在寻找一种为button_outcome设置默认值的方法。

有关如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:3)

通常,从观察者中创建反应或输出是不好的做法。见this slide and the two after it from a presentation by Joe Cheng。在这种情况下,我们可以将文本存储在一个名为text_to_display的reaciveVal中,我们可以从观察者那里更新,有关它们工作的详细信息,请参阅here。例如:

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(textOutput("button_outcome")),
      box(width = 2,  actionButton("runRF", "Predict"))
    )
  )
)


server <- function(input, output) {

  text_to_display <- reactiveVal('Please press the button.')

  observeEvent(input$runRF, {
    text_to_display('foo') # set new value to reactiveVal
  })

  output$button_outcome = renderPrint({  text_to_display() })

}

shinyApp(ui, server)

希望这有帮助!

答案 1 :(得分:0)

您可以在反应性上下文之外呈现默认文本

<android.support.constraint.ConstraintLayout
    android:id="@+id/container"
    ... etc.