考虑以下actionButton演示:
http://shiny.rstudio.com/gallery/actionbutton-demo.html

服务器。R:

shinyServer(function(input, output) {

  # builds a reactive expression that only invalidates
  # when the value of input$goButton becomes out of date
  # (i.e., when the button is pressed)
  ntext <- eventReactive(input$goButton, {
    input$n
  })

  output$nText <- renderText({
    ntext()
  })
})

ui.R:
shinyUI(pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
))

在此示例中,在按下操作按钮之前,右侧面板为空。我希望默认情况下呈现默认值为“50”的文本。

如果尚未按下 Action 按钮,如何使输出显示为默认输入?

最佳答案

eventReactive也将ignoreNULL视为已记录的here,这使您无需if语句即可初始化对象。

通过将,ignoreNULL = FALSE添加到原始帖子(给予或采用某种格式),verbatimTextOutput在启动时显示50。

我猜这在服务器端可以节省一些费用。

ui <- fluidPage(titlePanel("actionButton test"),
                sidebarLayout(
                  sidebarPanel(
                    numericInput(
                      "n",
                      "N:",
                      min = 0,
                      max = 100,
                      value = 50
                    ),
                    br(),
                    actionButton("goButton", "Go!"),
                    p("Click the button to update the value displayed in the main panel.")
                  ),
                  mainPanel(verbatimTextOutput("nText"))
                ))

server <- function(input, output) {

  ntext <- eventReactive(input$goButton, {
    input$n
  }
  # Adding this parameter to the original example makes it work as intended
  # with 50 in the output field to begin with
  , ignoreNULL = FALSE
  )

  output$nText <- renderText({
    ntext()
  })
}

shinyApp(ui = ui, server = server)

关于r - Shiny :如何使电抗值初始化为默认值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33662033/

10-12 17:40
查看更多