停止更新*输入无效的反应性

发布于 2025-01-26 06:46:36 字数 1025 浏览 3 评论 0原文

我正在寻找一种方法来停止更新*输入函数在我的Shiny应用程序中的反应性值无效。我希望更新功能仅更改视觉UI组件,而不是基本的反应值。

这是一个reprex:

library(shiny)

ui <- fluidPage(

    sliderInput("slide1", "Slider", min = 0, max = 10, value = 5),
    sliderInput("slide2", "Slider2", min = 0, max = 10, value = 0),
    
    textOutput("slide2_val")
)

server <- function(input, output, session) {
    
    observe({
        updateSliderInput(session, "slide2", value = input$slide1)
    }) |> 
        bindEvent(input$slide1)
    
    output$slide2_val <- renderText({
        paste("Value of `slide2`:", input$slide2)
    })
}

shinyApp(ui, server)

所需的行为是为input $ slide2的值,仅在用户与Slide2交互时更改,但是当Slide 1或Slide2与Slide2相互作用时,Slider UI元素要更改。

重要的是,这需要适用于各种输入*函数。收听点击事件对selectInput之类的输入不起作用(请参阅我的相关问题)。

I'm looking for a way to stop update*Input functions from invalidating reactive values in my Shiny app. I want the update function to change only the visual UI component, not the underlying reactive value.

Here's a reprex:

library(shiny)

ui <- fluidPage(

    sliderInput("slide1", "Slider", min = 0, max = 10, value = 5),
    sliderInput("slide2", "Slider2", min = 0, max = 10, value = 0),
    
    textOutput("slide2_val")
)

server <- function(input, output, session) {
    
    observe({
        updateSliderInput(session, "slide2", value = input$slide1)
    }) |> 
        bindEvent(input$slide1)
    
    output$slide2_val <- renderText({
        paste("Value of `slide2`:", input$slide2)
    })
}

shinyApp(ui, server)

The desired behaviour is for the value of input$slide2 to only change when the user interacts with slide2, but for the slider UI element to change when either slide1 or slide2 are interacted with.

Importantly, this needs to work for a variety of input* functions. Listening for click events won't work for inputs like selectInput (see my related issue).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2025-02-02 06:46:36

在我的最近的另一个问题的答案
我做了一个supsendfornextflush()函数以暂时停止更改
从发送到Shiny的输入。该功能可用于解决您的
问题也是。

我已经继续将功能放在实验中
shinysususpend 软件包。你可以
从github中安装它:

remotes::install_github("mikmart/shinysuspend")

然后在UI中添加USESHINYSUSPEND(),然后调用supstendfornextflush()
从服务器更新slide2时:

library(shiny)
library(shinysuspend)

ui <- fluidPage(
  useShinysuspend(),
  sliderInput("slide1", "Slider 1", min = 0, max = 10, value = 5),
  sliderInput("slide2", "Slider 2", min = 0, max = 10, value = 0),
  textOutput("slide2_val")
)

server <- function(input, output, session) {
  observe({
    suspendForNextFlush("slide2")
    updateSliderInput(session, "slide2", value = input$slide1)
  })
  
  output$slide2_val <- renderText({
    paste("Value of `slide2`:", input$slide2)
  })
}

shinyApp(ui, server)

In my recent answer to another question
I made a suspendForNextFlush() function to temporarily stop changes to an
input from being sent to Shiny. That function could be used to solve your
problem, too.

I’ve gone ahead and put the function in an experimental
shinysuspend package. You can
install it from GitHub with:

remotes::install_github("mikmart/shinysuspend")

Then include useShinysuspend() in the UI, and call suspendForNextFlush()
when updating slide2 from the server:

library(shiny)
library(shinysuspend)

ui <- fluidPage(
  useShinysuspend(),
  sliderInput("slide1", "Slider 1", min = 0, max = 10, value = 5),
  sliderInput("slide2", "Slider 2", min = 0, max = 10, value = 0),
  textOutput("slide2_val")
)

server <- function(input, output, session) {
  observe({
    suspendForNextFlush("slide2")
    updateSliderInput(session, "slide2", value = input$slide1)
  })
  
  output$slide2_val <- renderText({
    paste("Value of `slide2`:", input$slide2)
  })
}

shinyApp(ui, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文