Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在更新几个反应性依赖项之后控制执行流_R_Shiny - Fatal编程技术网

R 在更新几个反应性依赖项之后控制执行流

R 在更新几个反应性依赖项之后控制执行流,r,shiny,R,Shiny,这是一个简单而高级的问题。以下是演练: 我有三个输入I_1、I_2、I_3,它们导致在不同功能中使用的三个无功值: I1 <- reactive({input$i_1}) I2 <- reactive({input$i_2}) I3 <- reactive({input$i_3}) 更新本身工作正常,变量的三个新值被发送到客户端 问题:然后更新值并将其逐个发送回服务器,服务器每次都会不必要地重新计算图形。我只想在更新所有值时更新图形一次。可能吗 我试图等待已刷新事件:

这是一个简单而高级的问题。以下是演练:

我有三个输入I_1、I_2、I_3,它们导致在不同功能中使用的三个无功值:

I1 <- reactive({input$i_1})
I2 <- reactive({input$i_2})
I3 <- reactive({input$i_3})
更新本身工作正常,变量的三个新值被发送到客户端

问题:然后更新值并将其逐个发送回服务器,服务器每次都会不必要地重新计算图形。我只想在更新所有值时更新图形一次。可能吗


我试图等待已刷新事件:

  values <- reactiveValues(loadingProfile = FALSE)

  session$onFlushed(function() {
    values$loadingProfile <- FALSE
    cat("##### DEBUG: loading Profile OFF ##############\n", file = stderr())
  }, once = FALSE)

  output$plot <- renderPlot {(
    if (!values$loadingProfile) {
      plot( f(I1(), I2(), I3()) )
    }
  )}

  observe {(
    input$b_Load #Load button
    values$loadingProfile <- TRUE #Toggle on the watching value
    cat("##### DEBUG: loading Profile ON ##############\n", file = stderr())
    [...]
    updateSelectInput(session,"i_1", value = 4)
    updateSelectInput(session,"i_2", value = 3)
    updateSelectInput(session,"i_3", value = 5)
    ...
  )}

values下面类似的内容很可能会解决您的问题。我没有测试过,但是

设置

删除对
会话$onFlushed
的调用,并将
观察中的分配更改为:

isolate(values$loadingProfile <- values$loadingProfile + 1)

isolate(values$loadingProfile根据我对Shiny当前状态的了解,这个问题本身没有答案。然而,使用awesome
reactive()
表达式,我能够重构值流并只更新一次图。事实上

output$plot <- renderPlot {(
  plot( f(User()) )
)}

observe {(
  input$b_Load #Load button
  updateSelectInput(session,"i_1", value = 4)
  updateSelectInput(session,"i_2", value = 3)
  updateSelectInput(session,"i_3", value = 5)
  ...
)}

User <- reactive({
  object <- list()
  object$i1 <- I1()
  object$i2 <- I2()
  object$i3 <- I3()
)}

output$plot谢谢!我有类似的想法,但我不喜欢计算一般的事件。在这里,它不起作用,因为如果更新的某个值不变,它将不会被发送回。因此,计数器可能必须是
%/%3
2
1
,具体取决于具体情况……嗨,Antoine,我是我正在尝试做一些与您的问题非常相似的事情。您最终找到了解决方案吗?或者您最终使用了什么方法?谢谢。@johnny838我最终基于reactive()值进行了重构。请看我的答案。我想这正是我要找的。谢谢。
values <- reactiveValues(loadingProfile = 0)
output$plot <- renderPlot {(
  if (values$loadingProfile %/% 4 == 0) {
    plot( f(I1(), I2(), I3()) )
  }else{
    values$loadingProfile <- values$loadingProfile + 1
  }
)}
isolate(values$loadingProfile <- values$loadingProfile + 1)
library(shiny)
runApp(
  list(
    ui = fluidPage(
      selectInput("id1", "select a number", 1:5)
      , selectInput("id2", "select a small letter", letters[1:5])
      , selectInput("id3", "select a BIG letter", LETTERS[1:5])
      , textOutput("text")
      , actionButton("myBtn", "Press me!!!")
    )
    , server = function(input, output, session){
      values <- reactiveValues(loadingProfile = 0)
      I1 <- reactive({input$id1})
      I2 <- reactive({input$id2})
      I3 <- reactive({input$id3})
      output$text <- renderText({
        if (values$loadingProfile %/% 4 == 0) {
          paste(I1(), I2(), I3())
        }else{
          values$loadingProfile <- values$loadingProfile + 1
        }
      })
      observe({
        if(input$myBtn > 0){
          isolate(values$loadingProfile <- values$loadingProfile + 1)
          updateSelectInput(session,"id1", "select a number", 1:5, selected = 5)
          updateSelectInput(session,"id2", "select a small letter", letters[1:5], selected = "e")
          updateSelectInput(session,"id3", "select a BIG letter", LETTERS[1:5], selected = "E")
        }
      })
    }
  )
)
output$plot <- renderPlot {(
  plot( f(User()) )
)}

observe {(
  input$b_Load #Load button
  updateSelectInput(session,"i_1", value = 4)
  updateSelectInput(session,"i_2", value = 3)
  updateSelectInput(session,"i_3", value = 5)
  ...
)}

User <- reactive({
  object <- list()
  object$i1 <- I1()
  object$i2 <- I2()
  object$i3 <- I3()
)}