목록에서 반응 듣기

Oct 17 2020

reactives목록 에 추가 하고 들으려고합니다. 아래 코드에서 dbg1작동하는 동안 출력이 표시되지 않는 이유를 이해할 수 없습니다 dgb2.

유일한 차이점은 처음 버튼을 누른 후에 만 시작할 때만 l2포함 되지만 그렇지 않으면 동일하다는 것입니다.reactivel1

이것에 대한 설명이 있습니까?

library(shiny)
library(purrr)

ui <- fluidPage(
   actionButton("add1", "Add to List 1"), 
   actionButton("add2", "Add to List 2"), 
   actionButton("rnd", "Generate Random"),
   verbatimTextOutput("dbg1"),
   verbatimTextOutput("dbg2"))

server <- function(input, output, session) {
   l1 <- l2 <- list()
   
   observeEvent(input$add1, { l1 <<- c(l1, reactive({ input$rnd
         sample(100, 1)
      }))
   })
   
   observeEvent(input$add2, { l2 <<- c(l2, reactive({ input$rnd
         sample(100, 1)
      }))
   }, ignoreNULL = FALSE)
   
   output$dbg1 <- renderPrint(map(l1, ~ .x())) output$dbg2 <- renderPrint(map(l2, ~ .x())) 
}

shinyApp(ui, server)

@stefan의 답변과 @starja의 의견을 읽고 문제를 더 정확하게 렌더링하고 싶습니다.

골

동적 컨테이너를 원합니다 reactives. 즉, reactives일부 입력에 따라 작업을 수행 하는 동적 생성 양입니다 .

문제

내 코드에서 renderPrintfor dbg1는 시작할 때만 호출된다고 생각합니다. reactive컨텍스트 (실제로 나중에 만 추가됨) 가 없다는 것을 인식 하고 따라서 결코 리콜하지 않습니다. dbg1그것 의 경우 적어도 하나의 반응을보고 다시 돌아옵니다. 그래서 나는 l1스스로 반응 해야한다고 생각 합니다 (@stefan가 지적했듯이)

답변

2 stefan Oct 17 2020 at 04:01

결국 달성하려는 것이 확실하지 않습니다. 그러나이 게시물을 따라 가면 목록을 업데이트하고 다음과 reactiveVal같이 사용하여 인쇄 할 수 있습니다 .

library(shiny)
library(purrr)

ui <- fluidPage(
  actionButton("add1", "Add to List 1"), 
  actionButton("add2", "Add to List 2"), 
  actionButton("rnd", "Generate Random"),
  verbatimTextOutput("dbg1"),
  verbatimTextOutput("dbg2"))

server <- function(input, output, session) {
  l1 <- reactiveVal(value = list())
  l2 <- reactiveVal(value = list())
  
  rnd <- eventReactive(input$rnd, { sample(100, 1) }) observeEvent(input$add1, {
    old_value <- l1()
    l1(c(old_value, rnd()))
  })
  observeEvent(input$add2, { old_value <- l2() l2(c(old_value, rnd())) }) output$dbg1 <- renderPrint(l1())
  output$dbg2 <- renderPrint(l2()) 
}

shinyApp(ui, server)

thothal Oct 19 2020 at 15:43

문제는 dbg1처음 확인할 때 l1반응 컨텍스트를 볼 수 없다는 것입니다 (사실). 그러나 l1결국 일부를 포함 reactives하고 결코 "회상"하지 않는다는 사실을 깨닫지 못합니다 .

따라서 우리는 l1반응 자체를 만들어서 (@Stefan에서 영감을 얻음) 더 명확하게해야한다고 생각합니다 .

server <- function(input, output, session) {
   l1 <- l2 <- list()

   r1 <- reactiveVal(list())
   r2 <- reactiveVal(list())
   
   observeEvent(input$add1, { r1(c(r1(), reactive({ input$rnd
         sample(100, 1)
      })))
   })
   
   observeEvent(input$add2, { r2(c(r2(), reactive({ input$rnd
         sample(100, 1)
      })))
      
   }, ignoreNULL = FALSE)
   
   output$dbg1 <- renderPrint(map(r1(), ~ .x())) output$dbg2 <- renderPrint(map(r2(), ~ .x())) 
}