ふんわり R-tips

ぜんぜんわからない、俺たちは雰囲気でRをやっている

Shinyサンプルアプリケーション「11_timer」の説明

11_timerは、コントロールなしで時刻のみをテキスト表示する例です。

「11_timer」の実行

library(shiny)
runExample("11_timer")

f:id:phmpk:20161230161902p:plain

server.R

function(input, output, session) {
  output$currentTime <- renderText({
    invalidateLater(1000, session)
    paste("The current time is", Sys.time())
  })
}

Code license:MIT

  • invalidateLater()は、reactiveな動作を無効にするための関数。1000を指定すると、このsessionの実行内では、1秒間reactiveな動作が無効化される。そのため、output$currentTimeに出力される現在時刻の文字列は、1秒毎に更新される

ui.R

fluidPage(
  textOutput("currentTime")
)

Code license:MIT

  • サーバー側でcurrentTimeに入力された時刻の文字列を、textOutput()で表示する