问题描述
在处理 Shiny 应用程序时,我偶然发现了以下问题,这似乎与更新更改输入的顺序***输入与反应顺序有关.
while working on a Shiny application I stumbled upon the following problem which seems related to the order in which input are changed by the update***Input vs. the reactivity order.
我已经能够将重现问题的代码和步骤缩小到以下几个:
I have been able to narrow down the code and steps to reproduce the problem to the following ones:
- 我有一个介于 1 和 5 之间的 numericInput,默认值是 3,其选定的值用于产生一些输出(为了简单起见,如果值为 2,这里只是一个好"消息,3 或 4,如果值为 1 或 5,则为Bad"消息);
- 我希望用户能够更改输入值并在应用程序的其余部分使用其选择的值(通过按提交按钮)或使用默认值(通过按重置按钮);
- 条件 1
代码片段如下
用户界面:
The code snippets are the following
ui.R:
shinyUI(fluidPage(
titlePanel(
fluidRow(headerPanel(HTML("Test a possible bug"), windowTitle = "Test a possible bug")
)
),
mainPanel(
tabsetPanel(
tabPanel("Try this", br(),
numericInput(inputId="foo", label="Input me", value=3,min=1, max=5),
actionButton(inputId="reset", label="Use default"),
actionButton(inputId="submit", label="Use new value"),br(),br(),br(),
textOutput(outputId="bar")
)
)
)
))
server.R:
server.R:
shinyServer(function(input, output, session) {
observeEvent(input$reset, {
updateNumericInput(session=session, inputId="foo", value=3)
})
checkInput <- reactive({
input$submit
input$reset
isolate({
input$foo > 1 && input$foo < 5
})
})
output$bar <- renderText({
if (checkInput())
"Good"
else
"Bad"
})
})
我遇到的问题如下
- 如果我选择 5,应用程序会正确打印错误"消息
- 如果我现在按使用默认值",则 numericInput 会正确更新为默认值 3,但消息仍为错误",因为 Shiny(尚未)确认输入的修改
- 如果我现在再次按下使用默认值"按钮,或者如果我按下使用新值"按钮,消息现在会正确更新为好"
另一方面,我希望闪亮确认更新的输入,因为输入字段已更改
I would expect on the other hand that shiny acknowledges the updated input, since the input field has changed
这是有意为之吗?有什么建议可以解决问题吗?我可以通过要求用户单独将值重置为默认值然后提交新值来解决这个问题,但这听起来有点不令人满意......
Is this behaviour by design? Any suggestion to solve the problem?I could work around the issue by requiring the user to separately reset the value to default and then to submit the new value, but it sounds a little bit unsatisfactory...
附言我的实际代码有十几个数字输入字段,因此确实需要使用默认值"按钮,因为除了此处发布的简化设置之外,手动恢复所有值并不是一个真正可行的选项;-)
p.s. my actual code has a dozen of numericInput fields, thus the "Use default" button is really needed because manually restoring all values is not really a feasible option outside the simplified settings posted here ;-)
推荐答案
我相信这就是它的工作方式.如果您检查 updateNumericInput 或 updateSelectInput 的文档,则会在生成所有输出后完成更新.
I believe this is how it is intended to work. If you check the documentation, of updateNumericInput or updateSelectInput, the updation is done after all the outputs are produced.
"输入更新器函数向客户端发送消息,告诉它更改输入对象的设置.消息已收集并在所有观察者(包括输出)完成后发送运行."
我建议将功能设置为仅在点击提交"时显示好"或坏"消息,并在点击重置"时清除".希望这是有用的
I would suggest that the functionality be set in such a way that the Message "good' or 'bad' be displayed only when 'Submit' is hit, AND that it is 'cleared' when "Reset' is hit. Hope this is useful
请看例子
library(shiny)
ui<-(fluidPage(
titlePanel(
fluidRow(headerPanel(HTML("Test a possible bug"), windowTitle = "Test a possible bug")
)
),
mainPanel(
tabsetPanel(
tabPanel("Try this", br(),
numericInput(inputId="foo", label="Input me", value=3,min=1, max=5),
actionButton(inputId="reset", label="Use default"),
actionButton(inputId="submit", label="Use new value"),br(),br(),br(),
textOutput(outputId="bar")
)
)
)
))
server<-(function(input, output, session) {
rv <- reactiveValues()
observeEvent(input$reset, {
updateNumericInput(session=session, inputId="foo", value=3)
rv$Message = " "
})
observeEvent(input$submit,{
rv$checkInput<- input$foo > 1 && input$foo < 5
if (rv$checkInput)
rv$Message<- "Good"
else
rv$Message<- "Bad"
})
output$bar <- renderText({
rv$Message
})
})
shinyApp(ui,server)
这篇关于R 闪亮 - 更新可能存在问题***输入和反应性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!