service account credentials with a shiny app hosted in a directory

Mounting a ShinyApp into a directory is causing problems whereas I am able to run that app within RStudio. I have a shiny app that reads Google Analytics data using library(googleAnalyticsR), library(googleAuthR).

The connection to Google Analytics is with a service account. I used a service acount because I understood it to be that by using aservice account I would not have to be concerned with authentication after downloading the service account json and pointing google gar_auth_service(service_account_credentials.json) to it.

Everything works fine when I click to run the app within RStudio. Here's my script:

library(shiny)
library(tidyverse)
library(googleAnalyticsR)
library(googleAuthR)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            DT::dataTableOutput("example_sessions")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    # authenticate with service ac■■■■ cedentials (uses googleAuth library)
    gar_auth_service("/home/rstudio/credentials/service_account.json")
    
    # try pulling ga sessions
    view_id <- 123456 # my real id is different of course
    
    # get data
    ## filter clauses
    filter_funnel <- dim_filter(dimension = "shoppingStage", 
                                operator = "REGEXP",
                                expressions = "^(ADD_TO_CART|CHECKOUT_[1-9]|PRODUCT_VIEW|TRANSACTION)$")
    
    ## events extract
    ga_sessions <- 
        google_analytics(
            view_id, 
            date_range = c("2020-04-01", "2020-04-02"),
            metrics = c("users", "bounces", "transactions", "transactionRevenue"),
            dimensions = c("yearMonth", "channelGrouping", "deviceCategory", "userType"),
            anti_sample = TRUE,
            samplingLevel = HIGHER_PRECISION,
            max = -1
    )
    
    
    output$example_sessions <- DT::renderDataTable(DT::datatable({
        ga_sessions %>% head
    }, 
    options = list(dom = 't', bPaginate = F, sScrollX="100%"),
    rownames = F))
    
}

# Run the application 
shinyApp(ui = ui, server = server)

Here's a screen shot of this running:

So, I am able to connect with Google Analytics using the service account json and create a shiny app with it.

The trouble comes from trying to host the app. I've hosted other apps without any issues in /srv/shiny-server/<app directory name>.

I moved this single page app.R too /srv/shiny-server/MyTestApp and then visited <my ip>:3838/

The app loads in that I can see it, but as soon as the UI is rendered I get a disconnected from server error.

I replaced the data frame resulting from the Google Analytics API call with just mtcars and that worked fine. This only happens when I try to reference Google Analytics.

How can I get my app to run with Google Analytics when it's in a hosted directory? Underlining that everything does run fine when I run the app from within RStudio, the issue only happens with hosting the app in a directory?

What the logs say? It would be helpful to try to pin point the issue.

I went to the logs directory after refreshing the app and it's right now 50 minutes past the hour. I see the log file for 'AnotherTestApp' (top row):
Screen Shot 2020-04-08 at 17.51.00

But rather annoyingly, linux will not let me open this log file as you can see in the screen when I try with the less command. Also tried using vim and more commands.

I tried again a minute later and that particular log file has disappeared :confused:
Screen Shot 2020-04-08 at 17.52.47

So, I now know where to see the log files however for this particular app it looks like I cannot access it for whatever reason.

In case it's relevant I found a reference to ensuring the preserve_logs is set to true in shiny server config. I did that and here's what it now looks like:

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

  preserve_logs true;

  # 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;
  }
}

Then the person suggesting this says:

After restart of Shiny Server, access your application again, and check the log file. Usually there are useful information in the log file that could help you debug the issue.

However, I was unable to restart shiny because of this, where I am unable to run the restart command.

Not sure if the above is relevant or not, but preserve_logs did not exist in my config till I added it.