using session$onsessionend to disconnect rshiny app from the mysql server

给你一囗甜甜゛ 提交于 2019-12-12 03:56:41

问题


I obtained this code from R Shiny Gallery here:http://shiny.rstudio.com/gallery/reactive-poll-and-file-reader.html

function(input, output, session) {

  # Create a random name for the log file
  logfilename <- paste0('logfile',
                        floor(runif(1, 1e+05, 1e+06 - 1)),
                        '.txt')


  # ============================================================
  # This part of the code writes to the log file every second.
  # Writing to the file could be done by an external process.
  # In this example, we'll write to the file from inside the app.
  logwriter <- observe({
    # Invalidate this observer every second (1000 milliseconds)
    invalidateLater(1000, session)

    # Clear log file if more than 10 entries
    if (file.exists(logfilename) &&
        length(readLines(logfilename)) > 10) {
      unlink(logfilename)
    }

    # Add an entry to the log file
    cat(as.character(Sys.time()), '\n', file = logfilename,
        append = TRUE)
  })

  # When the client ends the session, suspend the observer and
  # remove the log file.
  session$onSessionEnded(function() {
    logwriter$suspend()
    unlink(logfilename)
  })

  # ============================================================
  # This part of the code monitors the file for changes once per
  # 0.5 second (500 milliseconds).
  fileReaderData <- reactiveFileReader(500, session,
                                       logfilename, readLines)

  output$fileReaderText <- renderText({
    # Read the text, and make it a consistent number of lines so
    # that the output box doesn't grow in height.
    text <- fileReaderData()
    length(text) <- 14
    text[is.na(text)] <- ""
    paste(text, collapse = '\n')
  })


  # ============================================================
  # This part of the code monitors the file for changes once
  # every four seconds.

  pollData <- reactivePoll(4000, session,
    # This function returns the time that the logfile was last
    # modified
    checkFunc = function() {
      if (file.exists(logfilename))
        file.info(logfilename)$mtime[1]
      else
        ""
    },
    # This function returns the content of the logfile
    valueFunc = function() {
      readLines(logfilename)
    }
  )

  output$pollText <- renderText({
    # Read the text, and make it a consistent number of lines so
    # that the output box doesn't grow in height.
    text <- pollData()
    length(text) <- 14
    text[is.na(text)] <- ""
    paste(text, collapse = '\n')
  })
}

If you look closely at the code above, you will see this segment:

# When the client ends the session, suspend the observer and
  # remove the log file.
  session$onSessionEnded(function() {
    logwriter$suspend()
    unlink(logfilename)
  })

I want to tweak that code in my shiny app to disconnect my MySQL server from my shiny app. Like this:

session$onSessionEnded(function() {
        dbDisconnect(con)
      })

I want to do this to ensure this warning message: "Unable to connect to database: Too many connections"

Would that ensure connections are freed up for other users? (I had a problem with my app because so many users were on and did not disconnect, leaving no available spot on the server.)

来源:https://stackoverflow.com/questions/41175535/using-sessiononsessionend-to-disconnect-rshiny-app-from-the-mysql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!