gpt4 book ai didi

r - 如何在 R Shiny 中获取用户信息

转载 作者:行者123 更新时间:2023-12-04 01:21:39 25 4
gpt4 key购买 nike

我想获取用户的 IP、用户的窗口 ID 和用户的服务器访问时间
我可以使用以下代码获取用户的 IP,但其他内容无法获取....
获取用户的 IP 代码是
ip <- session$request$REMOTE_ADDR 在服务器.R
如何获取用户的窗口 ID 和用户的服务器访问时间????

最佳答案

长话短说,“您”(应用程序,因此您,开发人员)对连接的了解是有限度的。我已经通过 RStudio 专业支持提出了更多类似的要求,我在下面拥有的是当前的能力。我希望(没有公布的时间表)最终会有更多可用。
我有 RStudio Connect,它在技术上是不同的产品,但底层方法相似,我相信有很多共享代码。
这是一个 Shiny 的应用程序,可以帮助您“发现”来自 session 的东西.我过滤掉了大部分 environment -session 中的变量要么装满了功能,要么只是没有用处。可能我过滤的太多了,你可能想用它来看看是否还有更多。

library(shiny)

ui <- bootstrapPage(
h3("Parsed query string"),
verbatimTextOutput("queryText"),
h3("URL components"),
verbatimTextOutput("sessionText"),
h3("EnvVars"),
verbatimTextOutput("envvarText")
)

server <- function(input, output, session) {
# Parse the GET query string
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
# Return a string with key-value pairs
paste(names(query), query, sep = "=", collapse=", ")
})
# Return the components of the URL in a string:
output$sessionText <- renderText({
cls <- sapply(session, function(a) class(a)[1])
nms <- names(cls[ cls %in% c("list", "character", "numeric", "integer",
"NULL", "logical", "environment", "reactivevalues" ) ])
nms <- setdiff(nms, ".__enclos_env__")
paste(
capture.output(
str(
sapply(nms,
function(sessnm) {
if (inherits(session[[sessnm]], c("environment", "reactivevalues"))) {
sapply(names(session[[sessnm]]), function(nm) session[[sessnm]][[nm]], simplify = FALSE)
} else if (inherits(session[[sessnm]], c("character", "numeric", "integer"))) {
session[[sessnm]]
} else class(session[[sessnm]])
}, simplify = FALSE),
nchar.max = 1e5,
vec.len = 1e5
)
),
collapse = "\n"
)
})
# Dump the environment variables
output$envvarText <- renderText({
paste(
capture.output(
str(as.list(Sys.getenv()))
),
collapse = "\n"
)
})
}

shinyApp(ui, server)
这是一些示例输出,已针对私有(private)信息进行了清理。 (此输出的上下文:我将服务器的 IP 地址更改为 111.222.333.444 ,否则应该是外部 IP 地址。此外,由于某些原因,这在 nginx 反向代理后面,所以 REMOTE_ADDR (例如)是不是很令人兴奋......但通常可能适合你。)
List of 10
$ groups : chr [1:45] "Admins" "Analytics" "Synology Desktop"
$ user : chr "r2evans"
$ userData : Named list()
$ singletons : chr(0)
$ request :List of 24
..$ HTTP_UPGRADE : chr "websocket"
..$ HTTP_CONNECTION : chr "Upgrade"
..$ HTTP_GUID : chr "_6433vf0"
..$ QUERY_STRING : chr ""
..$ httpuv.version :Classes 'package_version', 'numeric_version' hidden list of 1
.. ..$ : int [1:3] 1 5 2
..$ SERVER_NAME : chr "127.0.0.1"
..$ SCRIPT_NAME : chr ""
..$ SERVER_PORT : chr "40386"
..$ REMOTE_PORT : chr "52858"
..$ HTTP_SEC_WEBSOCKET_VERSION : chr "13"
..$ rook.input :Classes 'NullInputStream', 'R6' <NullInputStream>
Public:
close: function ()
read: function (l = -1L)
read_lines: function (n = -1L)
rewind: function ()
..$ PATH_INFO : chr "/websocket/"
..$ rook.version : chr "1.1-0"
..$ rook.errors :Classes 'ErrorStream', 'R6' <ErrorStream>
Public:
cat: function (..., sep = " ", fill = FALSE, labels = NULL)
flush: function ()
..$ HTTP_X_FORWARDED_FOR : chr "111.222.333.444:61928, ::1"
..$ HTTP_SHINY_SERVER_CREDENTIALS: chr "{\"user\":\"r2evans\",\"groups\":[\"Admins\",\"Analytics\",\"Synology Desktop\"]}"
..$ HTTP_SEC_WEBSOCKET_KEY : chr "NbavAciNEKYiS1AN/NGlVQ=="
..$ HTTP_SHINY_SHARED_SECRET : chr "65567ccb5db3d2990df150611eb022f4"
..$ REMOTE_ADDR : chr "127.0.0.1"
..$ HEADERS : Named chr [1:10] "Upgrade" "_6433vf0" "127.0.0.1:40386" "NbavAciNEKYiS1AN/NGlVQ==" "13" "{\"user\":\"r2evans\",\"groups\":[\"Admins\",\"Analytics\",\"Synology Desktop\"]}" "65567ccb5db3d2990df150611eb022f4" "websocket" "Go-http-client/1.1" "111.222.333.444!:61928, ::1"
.. ..- attr(*, "names")= chr [1:10] "connection" "guid" "host" "sec-websocket-key" "sec-websocket-version" "shiny-server-credentials" "shiny-shared-secret" "upgrade" "user-agent" "x-forwarded-for"
..$ rook.url_scheme : chr "http"
..$ REQUEST_METHOD : chr "GET"
..$ HTTP_USER_AGENT : chr "Go-http-client/1.1"
..$ HTTP_HOST : chr "127.0.0.1:40386"
$ closed : chr "logical"
$ token : chr "3a358fb6a0d42c688ea8693b67d448cb"
$ clientData :List of 13
..$ output_queryText_hidden : logi FALSE
..$ output_sessionText_hidden: logi FALSE
..$ output_envvarText_hidden : logi FALSE
..$ url_search : chr ""
..$ url_hash_initial : chr ""
..$ singletons : chr ""
..$ url_protocol : chr "https:"
..$ allowDataUriScheme : logi TRUE
..$ url_port : chr ""
..$ url_hostname : chr "server.mydomain.com"
..$ pixelratio : int 2
..$ url_hash : chr ""
..$ url_pathname : chr "/content/23/"
$ input : Named list()
$ progressStack:List of 10
..$ .__enclos_env__:<environment: 0x652f8e8>
..$ clone :function (deep = FALSE)
..$ as_list :function ()
..$ size :function ()
..$ peek :function ()
..$ pop :function ()
..$ push :function (..., .list = NULL)
..$ initialize :function (init = 20L)
..$ private :<environment: 0x652fbc0>
..$ self :<environment: 0x652f8e8>
和环境变量:
List of 42
$ EDITOR : chr "vi"
$ HOME : chr "/home"
$ LANG : chr "en_US.UTF-8"
$ LD_LIBRARY_PATH : chr "/usr/lib/R/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/jvm/default-java/jre/lib/amd64/server"
$ LN_S : chr "ln -s"
$ LOGNAME : chr "rstudio-connect"
$ MAKE : chr "make"
$ PAGER : chr "/usr/bin/pager"
$ PATH : chr "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/opt/rstudio-connect/ext/pandoc2"
$ PWD : chr "/opt/rstudio-connect/mnt/app"
$ R_ARCH : chr ""
$ R_BROWSER : chr "xdg-open"
$ R_BZIPCMD : chr "/bin/bzip2"
$ R_CONFIG_ACTIVE : chr "rsconnect"
$ R_DOC_DIR : chr "/usr/share/R/doc"
$ R_GZIPCMD : chr "/bin/gzip -n"
$ R_HOME : chr "/usr/lib/R"
$ R_INCLUDE_DIR : chr "/usr/share/R/include"
$ R_LIBS : chr "/opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.5.3:"
$ R_LIBS_SITE : chr "/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library"
$ R_LIBS_USER : chr "~/R/x86_64-pc-linux-gnu-library/3.5"
$ R_PAPERSIZE : chr "letter"
$ R_PAPERSIZE_USER : chr "letter"
$ R_PDFVIEWER : chr "/usr/bin/xdg-open"
$ R_PLATFORM : chr "x86_64-pc-linux-gnu"
$ R_PRINTCMD : chr "/usr/bin/lpr"
$ R_RD4PDF : chr "times,inconsolata,hyper"
$ R_SESSION_TMPDIR : chr "/opt/rstudio-connect/mnt/tmp/Rtmp3WkNTY"
$ R_SHARE_DIR : chr "/usr/share/R/share"
$ RSTUDIO_PANDOC : chr "/opt/rstudio-connect/ext/pandoc2"
$ R_SYSTEM_ABI : chr "linux,gcc,gxx,gfortran,?"
$ R_TEXI2DVICMD : chr "/usr/bin/texi2dvi"
$ R_UNZIPCMD : chr "/usr/bin/unzip"
$ R_ZIPCMD : chr "/usr/bin/zip"
$ SED : chr "/bin/sed"
$ SHINY_PORT : chr "40386"
$ SHINY_SERVER_VERSION: chr "1.8.2-10"
$ SHLVL : chr "0"
$ TAR : chr "/bin/tar"
$ TMPDIR : chr "/opt/rstudio-connect/mnt/tmp"
$ USER : chr "rstudio-connect"
$ USERNAME : chr "rstudio-connect"
最终,发布此应用程序,访问它,然后查看您找到的实例。

未要求,但为了完整性而包括在内:这是一个等效的 plumber API命中:
library(jsonlite)
library(plumber)
#* @get /clientdata
function(req, res){
classes <- sapply(req, function(a) head(class(a), n=1))
classes <- classes[! classes %in% c("ErrorStream", "NullInputStream") ]
classes <- classes[ ! names(classes) %in% c("args", "httpuv.version") ]
lapply(setNames(nm=names(classes)), function(nm) req[[nm]])
}
带输出:
{
"SERVER_NAME": [
"127.0.0.1"
],
"HTTP_X_FORWARDED_FOR": [
"111.222.333.555:54590, 127.0.0.1"
],
"HTTP_ACCEPT": [
"application/json"
],
"HTTP_DNT": [
"1"
],
"SERVER_PORT": [
"33632"
],
"rook.url_scheme": [
"http"
],
"REQUEST_METHOD": [
"GET"
],
"HTTP_X_ARR_LOG_ID": [
"e5bdaca6-dcd7-4ca0-87d5-0d54927244b5"
],
"rook.version": [
"1.1-0"
],
"HTTP_X_ARR_SSL": [
"..., CN=*.mydomain.com"
],
"postBody": [],
"HTTP_RSTUDIO_CONNECT_CREDENTIALS": [
"{\"user\":\"r2evans\",\"groups\":[\"Admins\",\"Analytics\",\"Synology Desktop\"]}"
],
"HTTP_USER_AGENT": [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0"
],
"SCRIPT_NAME": [
""
],
"cookies": [],
"HTTP_ACCEPT_LANGUAGE": [
"en-US,en;q=0.5"
],
"HTTP_PLUMBER_SHARED_SECRET": [
"..."
],
"HTTP_REFERER": [
"https://server.mydomain.com/content/28/__swagger__/"
],
"REMOTE_PORT": [
"48684"
],
"HTTP_ACCEPT_ENCODING": [
"gzip, deflate, br"
],
"HTTP_MAX_FORWARDS": [
"10"
],
"HTTP_X_ORIGINAL_URL": [
"/content/28/clientdata"
],
"HTTP_HOST": [
"127.0.0.1:33632"
],
"HEADERS": [
"application/json",
"gzip, deflate, br",
"en-US,en;q=0.5",
"1",
"127.0.0.1:33632",
"10",
"...",
"https://server.mydomain.com/content/28/__swagger__/",
"https://server.mydomain.com/content/28",
"{\"user\":\"r2evans\",\"groups\":[\"Admins\",\"Analytics\",\"Synology Desktop\"]}",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
"e5bdaca6-dcd7-4ca0-87d5-0d54927244b5",
"..., CN=*.mydomain.com",
"111.222.333.555:54590, 127.0.0.1",
"/content/28/clientdata",
"https://server.mydomain.com:443/content/28/clientdata"
],
"HTTP_X_RSC_REQUEST": [
"https://server.mydomain.com:443/content/28/clientdata"
],
"HTTP_RSTUDIO_CONNECT_APP_BASE_URL": [
"https://server.mydomain.com/content/28"
],
"PATH_INFO": [
"/clientdata"
],
"QUERY_STRING": [
""
],
"REMOTE_ADDR": [
"127.0.0.1"
]
}

关于r - 如何在 R Shiny 中获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62546575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com