Markdown 文档中的全局闪亮变量

Global shiny variables in Markdown document

我在 R Markdown 文档中使用闪亮的小部件。以前创建闪亮的应用程序时(不在 Markdown 上下文中),我能够制作只加载一次的大型静态数据集全局变量。我现在正尝试在 Markdown 文档中做类似的事情。

我在 CSV 文件中有一个包含 5000 多个项目的列表。我打算每天随机拉一个,然后在程序中使用它来生成一些信息和情节。我希望每个人每天的随机物品都是一样的,这就是我被卡住的地方。如果它只是每个会话的新随机项目,我知道如何让它工作,但我不确定如何让它在每个会话中都相同。

---
#title: "Daily Random Thing"
#author: "Tyler Beason"
#date: "Wednesday, June 24, 2015"
output: html_document
runtime: shiny
---
```{r,echo=FALSE}
symbolList <- read.csv('symbolList.csv',stringsAsFactors=FALSE, header=TRUE) #this is what I want to be global

checkFun <- function()
  {
    Sys.time()
  }
valueFun <- function()
  {
    Sys.time()
  }
today <- reactivePoll(5000,NULL, checkFun, valueFun) #only set to 5000ms for testing FYI
valueFun2 <- function()
  {
    sample(1:nrow(symbolList),1)
  }
rand <- reactivePoll(5000,NULL,checkFun,valueFun2)

todayThing <- reactive({symbolList[rand(),1]})
renderPrint({todayThing()})
#...go on to do more things with todayThing
```
#Title and such

我怎样才能使程序工作,以便它每天访问一次符号列表并拉出一个新的随机项目,重新生成页面的其余部分(我可以做到),然后保持静态(完全一样)对所有用户直到第二天?我知道这很可能都是在服务器端完成的,但我无法将它们拼凑起来。

希望这是有道理的。

你可以这样做,使用 Sys.Date()set.seed 让它每天都一样

set.seed(floor(as.numeric(Sys.Date())))
num_rows <- 5000
n <- sample(num_rows, 1)

然后从您的 .csv 中取出第 n 个项目。

编辑:定时更新

library(shiny)
shinyApp(
    shinyUI(
        fluidPage(
            textOutput("stuff")
        )
    ),
    shinyServer(function(input, output, session) {  
        values <- reactiveValues()
        values$n <- 0
        observe({
            ## Something like: difftime(Sys.Date()+1, Sys.Date(), units="sec")*1000
            ## to update everyday
            invalidateLater(1000, session)  # update n every second
            isolate( values$n <- values$n + 1 )
        })
        output$stuff <- renderPrint({
            sprintf("Stuff is %s", values$n)
        })
    })
)