9

Is there a way to get the IP of the person accessing the shiny app? Hopefully there's a R/Shiny solution but I can accept a javascript solution as well. I want to reverse geocode the IP to make graphs that visualize which countries most users access the app from -- and then put that information in the app for all users to see. I only need a way to get the users IP, the other things are easily solveable.

ZN13
  • 1,048
  • 1
  • 11
  • 21
  • 2
    Have a look here: https://shiny.rstudio.com/articles/usage-metrics.html and here: https://shiny.rstudio.com/articles/google-analytics.html – Jav May 10 '17 at 10:05

3 Answers3

8

You can try this. In the folder www, put this file, say getIP.js:

$(document).ready(function(){
  $.get("http://ipinfo.io", function(response) {
    Shiny.onInputChange("getIP", response);
  }, "json");
});

In ui.R:

shinyUI(fluidPage(
  tags$head(
    tags$script(src="getIP.js")
  ),
  .......

And in server.R (the observer is just for testing):

  IP <- reactive({ input$getIP })

  observe({
    cat(capture.output(str(IP()), split=TRUE))
  })

Then you get such a list as the output of IP() (I hide my IP):

List of 8
 $ ip      : chr "xx MY IP IS HERE xx"
 $ hostname: chr "No Hostname"
 $ city    : chr "Liège"
 $ region  : chr "Wallonia"
 $ country : chr "BE"
 $ loc     : chr "50.6412,5.5718"
 $ org     : chr "AS12392 Brutele SC"
 $ postal  : chr "4020"

This is not perfect, sometimes the result is NULL.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 1
    I checked your solution, and it seems it has two limitations @Stephane. It returns only the IP address of the session from which the user is firing the shiny app. That is not terribly useful as a user knows his own IP. It is the admin that has to gather IP addresses of all users connecting to the shiny app. It returns IP only once while if we need IP along with each input from the users, how can we achieve that? With the above two clarifications your solution would become undoubtedly hugely acceptable. – Lazarus Thurston Jan 30 '19 at 09:54
  • Sorry @SanjayMehrotra, no idea. – Stéphane Laurent Jan 30 '19 at 10:02
  • @LazarusThurston in the server you could include some code that stores the IP address to some sort of a storage I guess. – Jochem Jun 20 '22 at 14:59
2

dont say so much, show code here

library(shiny)  
ui <- fluidPage(
  # get ip
  tags$script(src="http://pv.sohu.com/cityjson?ie=utf-8"),
  tags$script('$( document ).on("shiny:sessioninitialized", function(event) {Shiny.setInputValue("too",returnCitySN["cip"]);});'),         
  
  # print
  verbatimTextOutput("ip")      
)

server <- function(input, output) {
  output$ip <- renderPrint(input$too) 
}

shinyApp(ui,server)
zhang jing
  • 141
  • 9
2

dont say so much. show code again

library(shiny)  
ui <- fluidPage(
  tags$script('$(document).on("shiny:sessioninitialized",function(){$.get("https://api.ipify.org", function(response) {Shiny.setInputValue("getIP", response);});})'),
  verbatimTextOutput('ip')
)

server <- function(input, output) {
  output$ip <- reactive(input$getIP)
}

shinyApp(ui,server)
zhang jing
  • 141
  • 9