gpt4 book ai didi

r - Shiny:如何使用默认值初始化 react 值

转载 作者:行者123 更新时间:2023-12-03 12:43:21 25 4
gpt4 key购买 nike

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

server.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”的文本。

如果尚未按下操作按钮,如何使用默认输入显示输出?

最佳答案

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:如何使用默认值初始化 react 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com