API query using dateRangeInput in R - r

I am trying to query a range of dates from an API in R/Shiny. I use dateRangeInput to specify the range, and the API documentation specifies that the call is:
spuddate, Value should be in the RFC3339 format, Latest date that drilling commenced on the property. Example: spuddate=gt(2006-01-02T15:04:05Z) spuddate=le(2017-03).
How would I do this correctly?
I've tried several methods using anytime and rfc3339 but I keep getting various errors. I suspect it has to be something around the way I structure the input call in server.
#UI - Obviously there are more but for sake of brevity here is the subset
sidebarPanel(
fluidRow(
column(12,
selectizeInput('stateSelect','STATE', choices= paste0(state.abb, sep=''), multiple=FALSE))
),
fluidRow(
column(12,
selectizeInput('directionSelect','HOLE DIRECTION', choices= c('H','V','D','U'), multiple=FALSE))
),
fluidRow(
column(12,
dateRangeInput("dateSelect", label = h3("Spud Date range")))
),
#Server Try So Far -- Obviously there are more but for sake of brevity here is the subset
observeEvent(input$select, {
wellInfo <- GET("https://di-api.drillinginfo.com/v2/direct-access/producing-entities",
add_headers(c('X-API-KEY' = key, 'Authorization' = addToken, 'Accept' = 'text/xml')),
query=list(drilltype = dput(input$directionSelect), state = dput(input$stateSelect),
spuddate = (rfc3339(anytime(input$dateSelect[1])),rfc3339(anytime(input$dateSelect[2]))),
pagesize = 100000))
})
I just want to get the query to go off. It works without the spuddate call (ie just the state and drilltype query). Any help would be appreciated! Thanks.

Ok, figured out a brute force solution.
date1 <- paste('btw(',date(anytime(input$dateSelect[1])),
',',date(anytime(input$dateSelect[2])),')', sep='')
wellInfo <- GET("https://di-api.drillinginfo.com/v2/direct-access/producing-entities",
add_headers(c('X-API-KEY' = key, 'Authorization' = addToken, 'Accept' = 'text/xml')),
query=list(drilltype = dput(input$directionSelect), state = dput(input$stateSelect),
spuddate = dput(date1),
#spuddate <= (as.POSIXct(as.character(input$dateSelect2, format = '%Y-%m'))),
pagesize = 100000))

Related

How to add Newline in the detail message of a Shiny Progress bar?

Is there any way to write add a newline character in a Shiny Progress bar message? \n or <br> does not seem to work.
I am trying to convert one of my old codes into a shiny app. The code basically calls a number of functions one after another, all of which take some time to execute. From the app, I would like to know which functions have already executed and the amount of time each of them took. I am trying to achieve this using the progress bar feature. Do let me know if there are better ways to do this.
Below is my dummy code. \ns are where I want to add a newline character. Let me know if what I am trying to do is not clear.
library(shiny)
ui <- fluidPage(
tags$head(tags$style(
HTML(".shiny-notification {position:fixed;top: 30%;left: 0%;right: 0%;}"))),
actionButton(inputId = "go", label = "Launch long calculation")
)
server <- function(input, output, session) {
observeEvent(input$go,{
withProgress(message = "doing task 1",value = 0,{
Sys.sleep(1.5)#task 1
setProgress(0.3,message = "doing task 2",detail = "\n task 1 done")
Sys.sleep(1.5)#task 2
setProgress(0.6,message = "doing task 3",detail = "\n task 1 done \n task 2 done")
Sys.sleep(1.5)#task 3
setProgress(0.9,message = "Almost done",detail = "\n task 1 done \n task 2 done \n task 3 done")
Sys.sleep(1.5)
})
})
}
shinyApp(ui, server)
This is what I am trying to achieve
Thanks in advance!

Authentification Scope Problems with Google Analytics API in Shiny Dashboard

I am setting up a Shiny Dashboard to get unsampled reports through the Google Analytics Report API. This is my first Shiny-Projekt so maybe the solution to my problem is very simple. Unfortunately i can not find anything which would help me. So welcome to my very first question on Stackoverflow :).
I already set the authentication scope to the highest and set an client web id in an own projekt. Basicly everything which is best practiced in all found tutourials (Big Respect to Mark Edmondson!).
library(shiny) # R webapps
library(googleAuthR) # auth login
# refresh authenticiaction token and set client scope
gar_set_client(web_json = "CLIENTID-JSONFILE", scopes = ("https://www.googleapis.com/auth/analytics"))
library(googleAnalyticsR)
####################
# Shiny: USER INTERFACE
####################
# Define UI
ui <- fluidPage(
# Authorization Login-Button
googleAuth_jsUI("auth", login_text = "Log In"),
# Drop-Down Menue: Account, Property, View
column(width=12, authDropdownUI("auth_dropdown", inColumns = FALSE)), # Modul Auswahl des Views 1 von 2
dateRangeInput("datepicker", NULL, start = Sys.Date()-30),
# The dimension selector (dropdown)
selectInput("dim", label = "Please select at least one dimension",
choices = dimension_options,
selected = c("date","hour","minute"),
multiple = TRUE),
# The metric dropdown
selectInput("metric", label = "Please select at least one and maximal ten metrics",
choices = metric_options,
selected = "sessions",
multiple = TRUE),
# Download Button
downloadButton("downloadData", "Download")
)
####################
# Shiny: SERVER LOGIK
####################
# Define server logic
server <- function(input, output, session) {
# get authorizatin token
auth <- callModule(googleAuth_js,"auth")
# Accountliste:
ga_accounts <- reactive({
req(auth())
with_shiny(
ga_account_list,
shiny_access_token = auth())
})
# Views: Greift auf die Accountliste zu
view_id <- callModule(authDropdown, "auth_dropdown",
ga.table = ga_accounts)
# Daten abrufen
ga_data <- reactive({
req(view_id())
req(input$datepicker)
req(input$dim)
req(input$metric)
with_shiny(
google_analytics,
view_id(),
date_range <- input$datepicker,
metrics <- input$metric,
dimensions <- input$dim,
max = -1, #kein Sampling
shiny_access_token = auth()
)
})
# Daten downloaden
output$downloadData <- downloadHandler(
filename = function() {
paste("ViewID_",view_id(), ".csv", sep="")
},
content = function(file){
write.csv2(ga_data(), file, row.names = FALSE)
})
}
# Run the application
shinyApp(ui = ui, server = server)
The Problem is, that i get the following Error-Code despite the Code worked a few days before:
Warning: Error in : API returned: Insufficient Permission: Request had insufficient authentication scopes.
93: stop
92: checkGoogleAPIError
91: _f
89: cachedHttrRequest
88: memDoHttrRequest
87: f
86: gar_api_page
85: f
84: with_shiny
83: <reactive:ga_accounts> [S:/GA_Report_Shiny/shinyapp_v0.R]
67: ga.table
66: <reactive>
50: pList
44: <observer>
1: runApp
You need "https://www.googleapis.com/auth/analytics.edit" to list GA accounts, which is what is needed for the ga.table function above.
The very first Shiny-Project you need to understand Google Analytics Report API. See below I mention API console support two type of credentials. OAuth 2.0 and API Keys.
// Create the service.
var service = new DiscoveryService(new BaseClientService.Initializer
{
ApplicationName = "Discovery Sample",
ApiKey="[YOUR_API_KEY_HERE]",
});
// Run the request.
Console.WriteLine("Executing a list request...");
var result = await service.Apis.List().ExecuteAsync();
// Display the results.
if (result.Items != null)
{
foreach (DirectoryList.ItemsData api in result.Items)
{
Console.WriteLine(api.Id + " - " + api.Title);
}
}
You can also refer them for a more complete solution.

Re: How to reset a value of fileInput in Shiny?

I would like to revisit the below question:
How can I set the value of a "fileinput" object to NULL, after pressing a reset button?
There are similar posts (how can I update a shiny fileInput object?) and (How to reset a value of fileInput in Shiny?), but as far as I can see the matter has never been resolved.
One comment says: "
Earlier this week I submitted a PR to shiny that does some work for this. I think it will be in shiny by the end of the month. – DeanAttali Jun 8 '17 at 6:31
Is there any progress on this front?
I found this to work well. So I'm not resetting the input$fileInput to NULL but I am setting a custom output to NULL, which you can then use for conditionalPanel, toggleState, etc.
UI function
fileInput(...)
conditionalPanel(condition = "output.file_upload_condition", ...)
Server function
values <- reactiveValues(
upload_state = NULL
)
#
observeEvent(input$loan_agreement_form, {
values$upload_state <- 'uploaded'
})
#
observeEvent(input$submit_loan, {
values$upload_state <- 'reset'
})
#
output$file_upload_condition <- reactive({
if (is.null(values$upload_state)) {
return(NULL)
} else if (values$upload_state == 'uploaded') {
return(input$loan_agreement_form)
} else if (values$upload_state == 'reset') {
return(NULL)
}
})
#
outputOptions(output, "file_upload_condition", suspendWhenHidden = = FALSE)

choose.files() defaulting to last directory used

According to the documentation for the choose.files function:
choose.files(default = "", caption = "Select files",
multi = TRUE, filters = Filters,
index = nrow(Filters))
If you would like to display files in a particular directory, give a
fully qualified file mask (e.g., "c:\*.*") in the default argument.
If a directory is not given, the dialog will start in the current
directory the first time, and remember the last directory used on
subsequent invocations.
My code is as follows:
AZPic = choose.files(default = "", caption = "Select Azimuth Picture", multi = FALSE, filters = Filters[c("All","jpeg"),])
STSPic = choose.files(default = "", caption = "Select Side to Side Elevation Picture", multi = FALSE, filters = Filters[c("All","jpeg"),])
FTBPic = choose.files(default = "", caption = "Select Front to Back Elevation Picture", multi = FALSE, filters = Filters[c("All","jpeg"),])
OrientationPic = choose.files(default = "", caption = "Select Orientation Picture", filters = Filters[c("All","jpeg"),])
Now when I run this code, it defaults to my home directory for all 4 calls.
It starts there for the first call of course, but afterwards should it not remember the folder I navigate to?
I typically will hop through another 3 or 4 folders to find the pictures for the job, but all of them for each program use will be in the same folder.
Is there something I'm missing?
Thanks in advance.

Shiny - app loads but it does not read the data file

I am trying to build my first shiny app in which I want my app to read different files based on user selected inputs. I have managed to build the ui.R, server.R, and helper.R functions so that I am not getting errors but my app is not working i.e. the file does not load. Nothing happens when I select different inputs, it appears that my read file function in my Helper.R script is not working. I have been stuck on this for days and I would very much appreciate some help to get this working.
ui.R
shinyUI(fluidPage(
titlePanel(“Search RSQRM Asset Data”),
fluidRow(
column(3,
selectInput(“model”, label = h3(“Select Model”),
choices = c(“RSQRM Global”, “RSQRM Europe”,”RSQRM US”,”RSQRM Japan”,”RSQRM Asia ex-JP”), selected = ‘RSQRM Global’)),
column(3,
selectInput(“modelCurrency”, label = h3(“Select Model Currency”),
choices = c(“USD”,”EUR”,”JPY”), selected = ‘EUR’)),
column(3,
dateInput(“modelDate”,
label = h3(“Select Model Date”),
value = “2014-04-23″)),
column(3,
radioButtons(“modelVersion”, label = h3(“L or G Version”),
choices = c(“Local Currency Exposure”, “Global Currency Exposure”),selected = “Local Currency Exposure”)),
helpText(“Note: Select the correct combination of model region and base currency.”),
submitButton(“Update View”)
),
# Create a new row for the table.
fluidRow(
dataTableOutput(outputId=”assetData”)
)
))
=============
server.R
library(timeDate);library(data.table)
source("helper.R")
# Define a server for the Shiny app
shinyServer(function(input, output) {
sModelPath<-'T:/Documents/Rsquared/RSQRM/'
#Assign switch values for the input fields
dfAssetData <- reactive({
sModel <- switch(input$model, "RSQRM Global"==as.character('GlobalDev'),
"RSQRM Europe"=as.character('Europe'),
"RSQRM US"=as.character('US'),
"RSQRM Japan"=as.character('Japan'),
"RSQRM Asia ex-JP"=as.character('AsiaExJP'))
sModelCurrency <- switch(input$modelCurrency, "USD"=as.character('USD'),"EUR"=as.character('EUR'),"JPY"=as.character('JPY'))
sModelVersion <- switch( input$modelVersion, "Local Currency Exposure"="", "Global Currency Exposure"=as.character("_G"))
sModelDate <- input$modelDate
readAssetDataFile(sModelPath=sModelPath,sModel=sModel,sModelCurrency=sModelCurrency,sModelDate=sModelDate,sModelVersion=sModelVersion)
})
output$assetData <- renderDataTable(
dfAssetData,options=list(iDisplayLength = 25)
)
})
=============
helper.R
# Constructs File Path and reads File
readAssetDataFile <- function(sModelPath,sModel,sModelCurrency,sModelDate,sModelVersion)
{
#Build Model file path
if(sModel=='GlobalDev')
{
sModelFile<-paste(sModelPath,sModel,'/outputData/','FF_RSQ_RSQRM_GlobalDev_v2_19_8_',sModelCurrency,'_',format(sModelDate,"%Y%m%d"),'_AssetData.txt',sep='')
} else if(sModel=='Europe')
{
sModelFile<-paste(sModelPath,sModel,'/outputData/','FF_RSQ_RSQRM_Europe',sModelVersion,'_v2_19_9_',sModelCurrency,'_',format(sModelDate,"%Y%m%d"),'_AssetData.txt',sep='')
} else if(sModel=='US')
{
sModelFile<-paste(sModelPath,sModel,'/outputData/','FF_RSQRM_US_v2_19_7_',sModelCurrency,'_',format(sModelDate,"%Y%m%d"),'_AssetData.txt',sep='')
} else if(sModel=='Japan')
{
sModelFile<-paste(sModelPath,sModel,'/outputData/','FF_RSQ_RSQRM_Japan_v2_19_4_',sModelCurrency,'_',format(sModelDate,"%Y%m%d"),'_AssetData.txt',sep='')
} else if(sModel=='AsiaExJP')
{
sModelFile<-paste(sModelPath,sModel,'/outputData /','FF_RSQ_RSQRM_AsiaExJP_v2_19_6_',sModelCurrency,'_',format(sModelDate,"%Y%m%d"),'_AssetData.txt',sep='')
}
#Read Asset Data File
dfDataHeader<-t(scan(sModelFile,skip=2,nlines=1,what = 'character',sep='|'))
dfData<-read.csv(sModelFile,sep='|',skip=3,header=F,stringsAsFactors=F)
names(dfData)<-dfDataHeader
return(dfData)
}
=============
extract of one of the source files
Asset Data|RSQRM_GlobalDev_v2_19_8_EUR
Date|20140423
RSQID|Parent ID|Currency Of Quotation|Domicile|Exchange Country|ADR|Current Price|Local Mkt Cap|Name|Return|Specific Return|Model R-Squared|Historical Variance|Model Variance|Base Currency Mkt Cap
AED||USD|#N/A|#N/A|0|0.272242|0|United Arab Dirham|-0.00525701|-1.019239e-005|0.9999894|0.008664313|0.01005771|0
ARS||USD|#N/A|#N/A|0|0.1249497|0|Argentine Peso|-0.004397333|0|1|0.01426818|0.01750348|0
AUD||USD|#N/A|#N/A|0|0.9287573|0|Australian Dollar|0.006424189|0|1|0.01322997|0.01398572|0
BRL||USD|#N/A|#N/A|0|0.4455243|0|Brazilian Real|0.03908932|0|1|0.01858497|0.01901002|0
BWP||USD|#N/A|#N/A|0|0.1141982|0|Botswana Pula|0.01457977|0.01029778|0.8950179|0.0081137|0.008672097|0
CAD||USD|#N/A|#N/A|0|0.9057439|0|Canadian Dollar|0.01092219|0|1|0.007959329|0.008564582|0
CHF||USD|#N/A|#N/A|0|1.133649|0|Swiss Franc|0.00140202|0|1|0.003511256|0.005111289|0
CLP||USD|#N/A|#N/A|0|0.001774098|0|Chilean Peso|-0.03123808|0|1|0.01214774|0.01481514|0
CNY||USD|#N/A|#N/A|0|0.160308|0|Chinese Yuan Renminbi|-0.01323205|0|1|0.008269959|0.009692453|0
COP||USD|#N/A|#N/A|0|0.0005161191|0|COLOMBIAN PESO|0.03332853|0|1|0.01231008|0.01293377|0
EGP||USD|#N/A|#N/A|0|0.1429034|0|Egyptian Pound|-0.01327199|-0.00800544|0.9377586|0.01042045|0.01099546|0
EUR||USD|#N/A|#N/A|0|1.38284|0|EURO|0|0|0|0|0|0
GBP||USD|#N/A|#N/A|0|1.6778|0|POUNDS STERLING|0.01696134|0|1|0.005241998|0.005920209|0
HKD||USD|#N/A|#N/A|0|0.1289722|0|Hong Kong Dollar|-0.003935099|0.001270294|0.9993091|0.008515768|0.009881006|0
HUF||USD|#N/A|#N/A|0|0.004495231|0|Hungarian Forint|0.0242871|0|1|0.01060005|0.009236147|0
IDR||USD|#N/A|#N/A|0|8.598194e-005|0|Indonesian Rupiah|-0.03087831|0|1|0.01365197|0.01479055|0
ILS||USD|#N/A|#N/A|0|0.2871667|0|Israeli Shekel|-0.0003501177|0|1|0.006951562|0.006442094|0
INR||USD|#N/A|#N/A|0|0.01635841|0|Indian Rupee|-0.03235179|0|1|0.01130393|0.01236205|0
ISK||USD|#N/A|#N/A|0|0.008922095|0|Icelandic Krona|0.0171473|0.03882897|0.297565|0.04469517|0.04829158|0
JPY||USD|#N/A|#N/A|0|0.00976686|0|Japanese Yen|-0.00645864|0|1|0.01881293|0.01908195|0
KRW||USD|#N/A|#N/A|0|0.0009616939|0|South Korean Won|0.05476844|0|1|0.009969143|0.01028664|0
KWD||USD|#N/A|#N/A|0|3.555414|0|Kuwaiti Dinar|-0.002247274|0.002172351|0.9813095|0.006397537|0.007210578|0
KZT||USD|#N/A|#N/A|0|0.005493779|0|Kazakhstan Tenge|-0.004944563|0.01499879|0.807177|0.01728362|0.02166266|0
LTL||USD|#N/A|#N/A|0|0.4005443|0|Lithuanian Litas|0.000264883|0.0002661943|0.1098195|4.386074e-006|1.170735e-005|0
MXN||USD|#N/A|#N/A|0|0.07643005|0|Mexican Nuevo Peso|0.0002846718|0|1|0.01561729|0.01448273|0
MYR||USD|#N/A|#N/A|0|0.3061288|0|Malaysian Ringgit|0.01157045|0|1|0.008959501|0.009135634|0
NAD||USD|#N/A|#N/A|0|0.09418435|0|Namibia Dollar|0.005982637|-0.001955748|0.9904171|0.02083048|0.01997479|0
NOK||USD|#N/A|#N/A|0|0.1668374|0|Norwegian Krone|0.009662986|0|1|0.004682287|0.004911323|0
NZD||USD|#N/A|#N/A|0|0.8582975|0|New Zealand Dollar|-0.009660542|0|1|0.01219613|0.01205033|0
PEN||USD|#N/A|#N/A|0|0.3584124|0|Peruvian Nuevo Sol|0.009532809|0|1|0.01369225|0.01090846|0
PHP||USD|#N/A|#N/A|0|0.02233582|0|Philippine Peso|0.0008690357|0|1|0.008781091|0.009171846|0
PLN||USD|#N/A|#N/A|0|0.3299508|0|Polish New Zloty|-0.003601432|0|1|0.008027087|0.007835649|0
QAR||USD|#N/A|#N/A|0|0.2746395|0|Qatari Rial|-0.004940689|0.0003093481|0.9999509|0.008670089|0.01006616|0
RON||USD|#N/A|#N/A|0|0.3093118|0|ROMANIAN LEU (NEW)|0.0001454353|0|1|0.002584549|0.003188807|0
RSD||USD|#N/A|#N/A|0|0.01196992|0|Serbia Dinar|0.002955198|0.002888322|0.3149524|0.005209823|0.005366478|0
RSQ00100301|RSQP001003|USD|US|US|0|0|0|A.A. IMPORTING CO INC|0|0|0.2278523|0.5409994|0.6483656|0.0388042
RSQ00100401|RSQP001004|USD|US|US|0|26.61|0|AAR CORP|0.03524399|-0.0009624362|0.6713049|0.1861986|0.1837458|761.4265
RSQ00101901|RSQP001019|USD|US|US|0|275|0|AFA PROTECTIVE SYSTEMS INC|-0.005258679|-0.01361179|0.546496|0.03353066|0.04896197|38.38115
RSQ00102101|RSQP001021|USD|US|US|0|3|0|AFP IMAGING CORP|-0.005258679|0.01126492|0.2947952|1.234856|1.125692|0.07810013
RSQ00104501W|RSQP001045|MXN|US|MX|0|460|0|AMERICAN AIRLINES GROUP INC|-0.107383|-0.1256154|0.2745233|0.544104|0.552039|19222.34
RSQ00104504|RSQP001045|USD|US|US|0|36.16|0|AMERICAN AIRLINES GROUP INC|-0.03031355|-0.05283874|0.342698|0.6602146|0.4509373|12329.74
RSQ00105001|RSQP001050|USD|US|US|0|16.69|0|CECO ENVIRONMENTAL CORP|0.0177182|-0.01030165|0.4680261|0.1776531|0.1898829|309.8809
RSQ00107201|RSQP001072|USD|US|US|0|13.39|0|AVX CORP|0.04942906|0.04780984|0.7066215|0.04772019|0.05692234|1628.753
RSQ00107501|RSQP001075|USD|US|US|0|55.6|0|PINNACLE WEST CAPITAL CORP|0.03970087|-0.02854544|0.8831447|0.03975214|0.0374972|4436.935
RSQ00107601|RSQP001076|USD|US|US|0|30.13|0|AARON'S INC|-0.02922386|-0.02090919|0.5094755|0.07882757|0.07039738|1568.27
RSQ00107801|RSQP001078|USD|US|US|0|38.6|0|ABBOTT LABORATORIES|0.006312132|0.03443837|0.4539649|0.09144053|0.08155435|43072.59
RSQ00107801W|RSQP001078|USD|US|GB|0|38.62|0|ABBOTT LABORATORIES|-0.002148151|0.006967425|0.2366017|0.08802702|0.1942208|43094.9
RSQ00107802W|RSQP001078|CHF|US|CH|0|33.75|0|ABBOTT LABORATORIES|-0.02139831|-0.09285313|0.2569895|0.4222401|0.2419817|42693.91
RSQ00108401|RSQP001084|USD|US|US|0|0.157|0|WORLDS INC|0.07596767|0.06546307|0.4029619|1.849886|1.528729|10.58255
RSQ00109401|RSQP001094|USD|US|US|0|21.77|0|ACETO CORP|0.1782798|0.1492193|0.6505797|0.1856081|0.1646115|446.7853
RSQ00109601|RSQP001096|USD|CA|US|0|117.8482|0|ACKTON CORP|0.05328643|-0.01130325|1|0.07294457|0.02986959|1066.637
When you call a reactive variable, call it as you would when calling a function.
For example, in your output$assetData when you call dfAssetData, try this...
output$assetData <- renderDataTable(
dfAssetData(),options=list(iDisplayLength = 25)
)
There may be more to the problem but that is the first error I see.

Resources