簡體   English   中英

在r閃亮中顯示'反應值'

[英]displaying 'reactive values' in r shiny

基本思路是在值發生變化后向最終用戶打印值。

提供的代碼生成閃亮的儀表板,並在幾秒鍾后將值0輸出到屏幕。 為了在打印0之前打印所有中間值(4,3,2,1),我應該更改什么? 如何最好地顯示值,而不僅僅是打印?

library(shiny)
library(shinydashboard)
x = 5

ui <- dashboardPage(
  dashboardHeader(title = "test"),
  dashboardSidebar(),
  dashboardBody(textOutput(outputId = "out"))
)

server <- function(input, output){
  while(x > 0){
    x = x - 1
    Sys.sleep(1)
    output$out <- renderPrint(x)
  }
}

shinyApp(ui, server)

我希望輸出為:

4
3
2
1
0

或包含上述內容的表,但實際輸出僅為0。

這可能是一些可以幫助你的東西。 您必須在renderPrint之外定義變量。 在我的示例中,變量是定時器觸發器上的更改,但可以是任何其他輸入。 代碼並不完美,初始循環立即執行,你將從頭開始看到5和4,但應該是一個好的開始。

library(shiny)
library(shinydashboard)
x = 5

ui <- dashboardPage(
  dashboardHeader(title = "test"),
  dashboardSidebar(),
  dashboardBody(
    textOutput(outputId = "out"),
    verbatimTextOutput(outputId = "outText"),
    tags$hr(),
    actionButton("go","Change value on click"),
    verbatimTextOutput(outputId = "out2")
    )
)

server <- function(input, output){

  # define reactive variable
  outVar <- reactiveValues(dataRow=x,
                           text=paste(x),
                           value = x)
  # define time dependent trigger
  autoInvalidate <- reactiveTimer(2000) 

  # render print
  output$out <- renderPrint(outVar$dataRow)
  # render text print
  output$outText <- renderText(outVar$text)
  # render print
  output$out2 <- renderText(outVar$value)


  # time dependent change of variable
  observeEvent(autoInvalidate(),{
    # check if > 0 
    if(outVar$dataRow[length(outVar$dataRow)] > 0){
      # add
      outVar$dataRow <- c(outVar$dataRow,outVar$dataRow[length(outVar$dataRow)]-1)
      outVar$text <- paste0(outVar$text,'\n',outVar$dataRow[length(outVar$dataRow)])
    }
  })

  # observer on click button
  observeEvent(input$go,{
    # check if > 0 
    if(outVar$value > 0){
      # lower by one
      outVar$value <- outVar$value - 1
    }
  })
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM