英文:
Numeric Input button not working with action button
问题
我正在尝试创建一个Shiny应用程序,在该应用程序中,我要求用户输入总的营销预算。根据这个输入,服务器代码将执行依赖于这个初始值的多个操作。
然而,我面临两个问题:
- 动作按钮不起作用。当我运行代码时,它自动将输入的初始值设置为"值"参数(3243452),我放在numericInput中。提交按钮最终只是装饰性的。以下是代码示例:
ui <- fluidPage(
titlePanel("Calculadora"),
sidebarLayout(
sidebarPanel(
numericInput("total_budget", "Valor total do Investimento:", value = 3243452, min = 0),
actionButton("submit", "Otimizar")
),
mainPanel(
plotlyOutput("grafico_investimento_atual"),
tableOutput("table")
)
)
)
- 我希望我的程序每当用户更改投资值时都会运行。然而,由于动作按钮不起作用,任何输入值的更改都会导致代码再次运行(实际上,它甚至不等待用户完成输入,只要有任何简单的更改就会运行)。我该如何解决这个问题?
观察:输入值仅用作在服务器函数内执行查询的常量。这里的目标是了解如何更好地使用动作按钮(在我构建UI的方式中是无用的)。
英文:
I am trying to create a shiny application where I ask the user for the input of the total marketing budget. With this input, the server code will perform several operations that depend on this initial value.
However, I am facing two issues:
The Action button does not work. When I run the code, it automatically sets the initial value of the input to the "value" parameter (3243452) that I put in the numericInput. The submit button ends up being just decorative. The code example is as follows:
ui <- fluidPage(
titlePanel("Calculadora"),
sidebarLayout(
sidebarPanel(
numericInput("total_budget", "Valor total do Investimento:", value = 3243452, min = 0),
actionButton("submit", "Otimizar")
),
mainPanel(
plotlyOutput("grafico_investimento_atual"),
tableOutput("table")
)
)
)
2.I would like my program to run every time the user changes the investment value. However, since the actionButton does not work, any change in the input value already causes the code to run again (actually, it doesn't even wait the user to give complete de value, it runs with any simply change). How can I fix this?
Observation: the input value is only used as a constant to perform a query inside the server function. The goal here is to understand how can i make a better usage of the action button (which is useless in the way that i built the UI)
答案1
得分: 1
在服务器端,您的代码需要对操作按钮做出反应。以下是如何实现的简单示例:
library(shiny)
ui <- fluidPage(
titlePanel("Calculadora"),
sidebarLayout(
sidebarPanel(
numericInput("total_budget", "Valor total do Investimento:", value = 3243452, min = 0),
actionButton("submit", "Otimizar")
),
mainPanel(
textOutput("test")
)
)
)
server <- function(input, output, session) {
output$test <- renderText({
if (input$submit > 0) {
paste("用户选择了一个值为", input$total_budget)
}
})
}
shinyApp(ui, server)
您可以看到,在选择操作按钮之前,文本不会呈现在主面板上。
英文:
On the server side your code needs to react to the action button. Here is a simple example of how to implement this:
library(shiny)
ui <- fluidPage(
ui <- fluidPage(
titlePanel("Calculadora"),
sidebarLayout(
sidebarPanel(
numericInput("total_budget", "Valor total do Investimento:", value = 3243452, min = 0),
actionButton("submit", "Otimizar")
),
mainPanel(
textOutput("test")
)
)
)
)
server <- function(input, output, session) {
output$test <- renderText(paste("User has selected a value of", input$total_budget)) |>
bindEvent(input$submit)
}
shinyApp(ui, server)
You can see that text does not render to the main panel until the action button is selected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论