会话如何在闪亮的服务器中工作?

时间:2016-12-21 17:57:14

标签: r shiny shiny-server

我在理解会话如何在闪亮服务器中工作时遇到了一些麻烦。我假设当用户关闭浏览器时会话结束,但是,通过使用服务器函数中的print(session$isClosed()),我在开始时得到FALSE响应(那么没关系),然后当我关闭浏览器时什么都没发生。谁能给我一些关于闪亮服务器会话的线索?我想存储特定于会话的图,让用户只下载他们的图。

1 个答案:

答案 0 :(得分:18)

嗯,从一个闪亮的会话对象开始是一个特殊的(' R6')闪亮的数据结构,由公共和私有元素组成。它的目的是记录一个用户和闪亮之间的关系的一个实例(稍后将详细介绍)。

>str(session)
Classes 'ShinySession', 'R6' <ShinySession>
  Public:
    @uploadEnd: function (jobId, inputId) 
    @uploadieFinish: function () 
    @uploadInit: function (fileInfos) 
    allowReconnect: function (value) 
    clientData: reactivevalues
    clone: function (deep = FALSE) 
    close: function () 
    closed: FALSE
    decrementBusyCount: function () 
    defineOutput: function (name, func, label) 
    dispatch: function (msg) 
    doBookmark: function () 
    downloads: Map, R6
    exportTestValues: function (..., quoted_ = FALSE, env_ = parent.frame()) 
    files: Map, R6
    fileUrl: function (name, file, contentType = "application/octet-stream") 
    flushOutput: function () 
    freezeValue: function (x, name) 
    getBookmarkExclude: function () 
    getTestEndpointUrl: function (inputs = TRUE, outputs = TRUE, exports = TRUE, format = "rds") 
    groups: NULL
    handleRequest: function (req) 
    incrementBusyCount: function () 
    initialize: function (websocket) 
    input: reactivevalues
    isClosed: function () 
    isEnded: function () 
    makeScope: function (namespace) 
    manageHiddenOutputs: function () 
    manageInputs: function (data) 
    ns: function (id) 
    onBookmark: function (fun) 
    onBookmarked: function (fun) 
    onEnded: function (endedCallback) 
    onFlush: function (flushCallback, once = TRUE) 
    onFlushed: function (flushedCallback, once = TRUE) 
    onInputReceived: function (callback) 
    onRestore: function (fun) 
    onRestored: function (fun) 
    onSessionEnded: function (sessionEndedCallback) 
    output: shinyoutput
    outputOptions: function (name, ...) 
    progressStack: environment
    reactlog: function (logEntry) 
    registerDataObj: function (name, data, filterFunc) 
    registerDownload: function (name, filename, contentType, func) 
    reload: function () 
    request: environment
    resetBrush: function (brushId) 
    restoreContext: RestoreContext, R6
    rootScope: function () 
    saveFileUrl: function (name, data, contentType, extra = list()) 
    sendBinaryMessage: function (type, message) 
    sendCustomMessage: function (type, message) 
    sendInputMessage: function (inputId, message) 
    sendInsertUI: function (selector, multiple, where, content) 
    sendModal: function (type, message) 
    sendNotification: function (type, message) 
    sendProgress: function (type, message) 
    sendRemoveUI: function (selector, multiple) 
    session: active binding
    setBookmarkExclude: function (names) 
    setShowcase: function (value) 
    showProgress: function (id) 
    singletons: 
    token: d44d583f13b3cd4ccce43f59fe410f61
    unhandledError: function (e) 
    updateQueryString: function (queryString) 
    user: NULL
    wsClosed: function () 
  Private:
    .clientData: ReactiveValues, R6
    .input: ReactiveValues, R6
    .outputOptions: list
    .outputs: list
    bookmarkCallbacks: environment
    bookmarkedCallbacks: environment
    bookmarkExclude: 
    busyCount: 2
    closedCallbacks: environment
    createBookmarkObservers: function () 
    enableTestEndpoint: function () 
    fileUploadContext: environment
    flushCallbacks: environment
    flushedCallbacks: environment
    getOutputOption: function (outputName, propertyName, defaultValue) 
    inputMessageQueue: list
    inputReceivedCallbacks: environment
    invalidatedOutputErrors: Map, R6
    invalidatedOutputValues: Map, R6
    outputValues: list
    progressKeys: character
    registerSessionEndCallbacks: function () 
    restoreCallbacks: environment
    restoredCallbacks: environment
    sendErrorResponse: function (requestMsg, error) 
    sendMessage: function (...) 
    sendResponse: function (requestMsg, value) 
    shouldSuspend: function (name) 
    showcase: FALSE
    storeOutputValues: function (values = NULL) 
    testEndpointUrl: session/d44d583f13b3cd4ccce43f59fe410f61/dataobj/shinyte ...
    testValueExprs: list
    websocket: WebSocket
    write: function (json) 

探索会话对象的一个​​好方法是使用 shiny example in shiny gallery client-data-and-query-string。它允许see session$clientdata中包含的内容或对象的任何其他元素。

一些额外的&amp;误导性的微不足道的要点:

  • 会话什么时候开始?当用户与闪亮的应用程序连接时
  • 会话什么时候结束?当用户与闪亮的应用程序断开连接时

作为一个例子,为了说明问题实际上是如何复杂,如果我刷新浏览器,我结束当前会话并创建一个新会话。

来到session$isClosed(),这不是在会话结束时连接到特定操作的正确功能。 这实际上是闪亮回调函数的作用

onSessionEnded(fun, session = getDefaultReactiveDomain())

最小的例子如下:

library(shiny)

ui =(
  fluidPage(
    titlePanel("This is an example")
  )
)

server = function(input, output, session){
  session$onSessionEnded({
    print("Stop!")
    stopApp   
  }) 
}

runApp(list(ui = ui, server = server))

如果您尝试,刷新(或使用浏览器()分解)将打印&#34;停止&#34;并将停止该应用程序。

2017年9月26日编辑:

一般来说,如果会话的连续性很重要,我认为最好谨慎(在任何情况下,直接在session或{{1}上测试Shiny Server代码是合适的。 }})。可能最重要的用例来自Shiny Server Pro,其中任何断开连接Shiny Server Pro都会影响登录状态等。)

我也知道may小组已在最近的版本中对这些区域进行了更改。例如,似乎虽然shiny仍然有效,但它可能不再是这个用例的最佳功能。

请参阅以下代码作为示例(来自onSessionEnded参考指南),使用shiny,可以在会话结束时以及应用停止时使用。

onStop
相关问题