Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

r - How to trigger a data refresh in shiny?

I have a shiny application that queries data from SQL into data frames, and then those data frames are referenced from my shinyServer() block. I've been running it only in RStudio thus far, and so whenever I needed new data I'd just restart the application and before the server loads it would grab all new data.

I'd like to transition the app to shiny server, but I'm not sure how I can induce it to get new data periodically. For the sake of the interface I'd like it to be automatic rather than have a user click a button to initiate the loading. Is there an idiomatic solution for this?

EDIT:

I think I found a solution that works for me.

shinyServer(function(input,output,session){
    sourceData <- reactive({
        invalidateLater(1000000,session)

        functionThatGetsData()
    })
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The smartest would probable be to use reactivePoll if you can make a fast query to detect if there is new data. This worked very well for me just today actually.

reactivePoll shiny

Reactive polling



Description

Used to create a reactive data source, which works by periodically polling a non-reactive data ource.

Usage

reactivePoll(intervalMillis, session, checkFunc, valueFunc)

Arguments

intervalMillis

Approximate number of milliseconds to wait between calls to checkFunc. his an be either a numeric value, or a function that returns a numeric value.

session

The user session to associate this file reader with, or NULL if none. If non-null, he reader will automatically stop when the session ends.

checkFunc A relatively cheap function whose values over time will be tested for equality; nequality indicates that the underlying value has changed and needs to be invalidated and re-ead using valueFunc. See Details. valueFunc

A function that calculates the underlying value. See Details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...