Cauchy
Cauchy

Reputation: 1747

R shiny server: How to count number of users of my application

I am using R shiny-server open source version. I want to count the number of users of my application. This is my configuration file:

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 80
server {
  listen 80;

  server_name some_name;

  # Define a location at the base URL
  location / {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server/;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
  }
}

When I use my application and I look in the log files, there is nothing that tells me of the IPs/users that used my application. Is there a way to get this information?

Upvotes: 5

Views: 3352

Answers (2)

David Muñoz Tord
David Muñoz Tord

Reputation: 365

I have managed a 'simpler' way to do this on my own server:

library(pacman)

pacman::p_load(shiny, this.path, dplyr)

dashboard = 'MyApp' #put whatever name here

traffic = read.csv(file.path(path, "traffic.csv")) #you just need to create an empty file before with whatever name you want

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message { window.location = message;});"

ui <- fluidPage(

uiOutput("msg"),
tags$head(tags$script(jscode))
)

server <- function(input, output, session) {
line = c( as.character(Sys.Date()), as.character(format(Sys.time(), "%T")), dashboard, user$response$displayName)

    traffic[nrow(traffic) + 1,] = line 

    write.csv(traffic,file.path(path, "traffic.csv"), row.names = FALSE)

    url <- "http://analytics.my.network/MyApp" # your real url redirect
    session$sendCustomMessage("mymessage", url) # you can send a message here if you want or just blank

}
shinyApp(ui,server)

The idea is to basically create a simple app that only redirects to your app and appends whatever you want to a file that you can then use to monitor your shiny server. (You can do a lot more with this basic idea (basic login time stamps, redirect to different apps depending on some function, etc..)

Upvotes: 0

OganM
OganM

Reputation: 2663

You can use the javascript add ons listed here. It allows you to extract IPs and a unique fingerprint (a hash) composed of misc information such as browser, display size etc. For most users in shinyapps.io, IP information is unavailable but you should be able to get a rough count. Note that if same people use different browsers you'll get different hashes for them

The critical parts are adding

inputIp("ipid"),
inputUserid("fingerprint")

to somewhere in the sidebar layout. These are hidden elements required to collect the information you want.

After that they can be accesed in an observer as

observe({
    fingerprint <- input$fingerprint
    ipid <- input$ipid
})

And of course you need to copy the .js files to www/js folder

source: https://groups.google.com/forum/#!msg/shiny-discuss/EGQhEyoEk3E/ur0mpif11x4J

To get an actual count though, you need persistent file storage. This is not available in shinyapps.io, but you can use cloud storage options such as dropbox to log your users. A tutorial on this is available here http://shiny.rstudio.com/articles/persistent-data-storage.html. I personally use rdrop2 to edit a file in my dropbox every time someone uses my app.

Upvotes: 7

Related Questions