Rshiny Sending an Email with mailR | jCall Error - r

Below is the mailR code I have running on the server side of an RShiny app that I am building. The aim of this app is to send an email when a certain occurrence happens in the financial markets. When I run this code on its own in a separate script (not in a RShiny app) it runs fine. When run in the RShiny app, the app crashes with the error below.
sender <- "FROM#gmail.com"
recipients <- c("TO#gmail.com")
body.string <- "words words words words words punchline"
send.mail(from = sender,
to = recipients,
subject = "***STOPLOSS ALERT***",
body = body.string,
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "FROM#gmail",
passwd = "******", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
Error Message:
Warning in if (file.exists(body)) body <- readChar(body, file.info(body)$size) :
the condition has length > 1 and only the first element will be used
Warning: Error in .jcall: java.lang.NoSuchMethodException: No suitable method for the given parameters
Stack trace (innermost first):
56: <Anonymous>
55: stop
54: .jcheck
53: .jcall
52: .jrcall
51: email$setMsg
50: send.mail
43: isolate
42: server [C:\Users\****/app.R#135]
1: runApp
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
java.lang.NoSuchMethodException: No suitable method for the given parameters
Since the code works fine on its own, outside of the Shiny app, my thinking is that RShiny is somehow causing this to bug out on the lower level Java code. Any advice/tips would be greatly appreciated.

Can you try executing a simple code like below and tell me if you are still getting any error?
library(mailR)
library(shiny)
ui <- fluidPage(
actionButton("sendMail","Send Mail")
)
server <- function(input, output) {
observeEvent(input$sendMail,
{
sender <- "FROM#gmail.com"
recipients <- c("TO#gmail.com")
body.string <- "words words words words words punchline"
send.mail(from = sender,
to = recipients,
subject = "***STOPLOSS ALERT***",
body = body.string,
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "FROM#gmail",
passwd = "******", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
}
)
}
shinyApp(ui = ui, server = server)

Related

How can I reduce the error of SQL query in R?

Here is my function
getSQL <- function(server="server name", database="database name", Uid="
user name", Pwd="password", Query){
conlink <- paste('driver={SQL Server};server=', server,';database=',database,';Uid=', Uid,
';Pwd=', Pwd,';Encrypt=True;TrustServerCertificate=False', sep="")
conn <- odbcDriverConnect(conlink)
dat <- sqlQuery(channel= conn, Query, stringsAsFactors = F)
odbcCloseAll()
return(dat)
}
When I call the function using
query.cut = "SELECT [measurename]
,[OrgType]
,[year_session]
,[Star]
,[cutvalue]
,[Date]
,[File]
FROM [database name].[dbo].[DST_Merged_Cutpoint]
ORDER BY [year_session] DESC
"
getSQL(Query=query.cut)
I get this error:
Error in sqlQuery(conn, Query, stringsAsFactors = F) :
first argument is not an open RODBC channel
In addition: Warning messages:
1: In odbcDriverConnect(conlink) :
[RODBC] ERROR: state 28000, code 18456, message [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user ' insightm8'.
2: In odbcDriverConnect(conlink) :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
3: In odbcDriverConnect(conlink) :
Error in sqlQuery(conn, Query, stringsAsFactors = F) :
first argument is not an open RODBC channel
How can I fix these errors? Thanks in advance
Take care not to add spaces to UID:
Server]Login failed for user ' insightm8'.
Reproducing this on an SQL Server connection creates the same error.
Try using paste0 instead of paste :
conlink <- paste0('driver={SQL Server};server=', server,';database=',database,';Uid=', Uid,
';Pwd=', Pwd,';Encrypt=True;TrustServerCertificate=False', sep="")

Airflow connect to sql server select results to a data frame

Airflow-pandas-read-sql-query to dataframe
i am trying to connect to SQL server local to get data from a table and process the data using pandas operations but i m failing to figure out how to pass the select query results to a data frame
the below works to clear data in the table
``` sql_command = """ DELETE FROM [TestDB].[dbo].[PythonTestData] """
t3 = MsSqlOperator( task_id = 'run_test_proc',
mssql_conn_id = 'mssql_local',
sql = sql_command,
dag = dag,
database = 'TestDB',
autocommit = True) ```
the intended pandas is
query = 'SELECT * FROM [ClientData] '#where product_name='''+i+''''''
df = pd.read_sql(query, conn)
pn_list = df['ClientID'].tolist()
#print("The original pn_list is : " + str(pn_list))
for i in pn_list:
varw= str(i)
queryw = 'SELECT * FROM [ClientData] where [ClientID]='''+varw+''
dfw = pd.read_sql(queryw, conn)
dfw = dfw.applymap(str)
cols=['product_id','product_name','brand_id']
x=dfw.values.tolist()
x=x[0]
ClientID=x[0]
Name=x[1]
Org=x[2]
Email=x[3]
#print('Name :'+Name+' ,'+'Org :'+Org+' ,'+'Email :'+Email+' ,'+'ClientID :'+ClientID)
salesData_qry= 'SELECT * FROM [TestDB].[dbo].[SalesData] where [ClientID]='''+ClientID+''
salesData_df= pd.read_sql(salesData_qry, conn)
salesData_df['year1'] = salesData_df['Order Date'].dt.strftime('%Y')
salesData_df['OrderMonth'] = salesData_df['Order Date'].dt.strftime('%b')
filename ='Daily_Campaign_Report_'+Name+'_'+Org+'_'+datetime.now().strftime("%Y%m%d_%H%M%S")
p = Path('C:/Users/user/Documents/WorkingData/')
salesData_df.to_csv(Path(p, filename + '.csv'))```
Please point me to correct approach as i m new to airflow
I'm not so clear on how you generate the query code but in order to get dataframe from MsSQL you need to use MsSqlHook:
from airflow.providers.microsoft.mssql.hooks.mssql import MsSqlHook
def mssql_func(**kwargs):
hook = MsSqlHook(conn_id='mssql_local')
df = hook.get_pandas_df(sql="YOUR_QUERY")
#do whatever you need on the df
run_this = PythonOperator(
task_id='mssql_task',
python_callable=mssql_func,
dag=dag
)
this is the code i am using for the dag
def mssql_func(**kwargs):
conn = MsSqlHook.get_connection(conn_id="mssql_local")
hook = conn.get_hook()
df = hook.get_pandas_df(sql="SELECT * FROM [TestDB].[dbo].[ClientData]")
#do whatever you need on the df
print(df)
run_this = PythonOperator(
task_id='mssql_task',
python_callable=mssql_func,
dag=dag
)
Error Log
[2021-01-12 16:07:15,114] {providers_manager.py:159} WARNING - The provider for package 'apache-airflow-providers-imap' could not be registered from because providers for that package name have already been registered
[2021-01-12 16:07:15,618] {base.py:65} INFO - Using connection to: id: mssql_local. Host: localhost, Port: 1433, Schema: dbo, Login: sa, Password: XXXXXXXX, extra: None
[2021-01-12 16:07:15,626] {taskinstance.py:1396} ERROR - (18456, b"Login failed for user 'sa'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\n")
Traceback (most recent call last):
File "src/pymssql.pyx", line 636, in pymssql.connect
File "src/_mssql.pyx", line 1964, in _mssql.connect
File "src/_mssql.pyx", line 682, in _mssql.MSSQLConnection.__init__
File "src/_mssql.pyx", line 1690, in _mssql.maybe_raise_MSSQLDatabaseException
_mssql.MSSQLDatabaseException: (18456, b"Login failed for user 'sa'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\n")

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.

Error running R Instagram example from R-Bloggers?

I'm getting an error running the R Instagram query example here: https://www.r-bloggers.com/analyze-instagram-with-r/
I'm guessing it has something to do with my Instagram client (status is "Sandbox Mode"), but not sure what to do. Here's the R code and output:
Code:
require(RCurl)
require(httr)
full_url <- oauth_callback()
full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\1")
print(full_url)
app_name <- "teamusainrio"
client_id <- "a36424058cdf424c8e8b2d5cc2af1b15"
client_secret <- "398863caad6a4171ad10eb201870065b"
scope = "basic"
instagram <- oauth_endpoint(
authorize = "https://api.instagram.com/oauth/authorize",
access = "https://api.instagram.com/oauth/access_token")
myapp <- oauth_app(app_name, client_id, client_secret)
ig_oauth <- oauth2.0_token(instagram, myapp,scope="basic", type = "application/x-www-form-urlencoded",cache=FALSE)
Output:
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Input:
tmp <- strsplit(toString(names(ig_oauth$credentials)), '"')
token <- tmp[[1]][4]
username <- "therock"
user_info <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/search?q=',username,'&access_token=',token,sep="")),unexpected.escape = "keep")
received_profile <- user_info$data[[1]]
Output/error:
Error in user_info$data[[1]] : subscript out of bounds
If I run the query from the above code directly into my browser,
https://api.instagram.com/v1/users/search?q=therock&access_token=511932783.a364240.562161d569354bf78b043c98cf938235
I receive the following:
{"meta": {"code": 200}, "data": []}

RODBC connectivity to Oracle without tnsnames.ora

I am trying to connect to Oracle from R using RODBC without using tnsnanes.ora.
I have tried following strings, but none of them are working.
> con.text <- paste0("Driver={OracleODBC-11g};Dbq=//oracle.server:1527/database.pdw.prod;Uid=user;Pwd=pswd;")
> con.text <- paste0("Driver={OracleODBC-11g}; ",
"CONNECTSTRING=(DESCRIPTION=(ADDRESS= (PROTOCOL = TCP)(HOST = oracle.server)(PORT = 1527))(CONNECT_DATA=(SERVICE_NAME = database.pdw.prod))); uid=user;pwd=pswd;")
> con.text <- paste0("Driver=", "OracleODBC-11g"
, ";Server=", "oracle.server"
, ";Database=", "database.pdw.prod"
, ";Uid=", "user"
, ";Pwd=", "pwd", ";")
> con.text <- paste0("Driver=", "OracleODBC-11g"
, ";Server=", "oracle.server"
, ";CONNECTSTRING=" , "(DESCRIPTION=(ADDRESS= (PROTOCOL = TCP)(HOST = oracle.server)(PORT = 1527))(CONNECT_DATA=(SERVICE_NAME = database.pdw.prod)))"
, ";Database=", "database.pdw.prod"
, ";Uid=", "user"
, ";Pwd=", "pswd", ";")
> con1 <- odbcDriverConnect(connection = con.text)
But for all these strings I am getting following error:
Warning messages:
1: In odbcDriverConnect(connection = con.text) :
[RODBC] ERROR: state HY000, code 12162, message [unixODBC][Oracle][ODBC][Ora]ORA-12162: TNS:net service name is incorrectly specified
2: In odbcDriverConnect(connection = con.text) : ODBC connection failed
OR
1: In odbcDriverConnect(connection = con.text) :
[RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified
The correct sysntaxis you are looking for is
Conex <- odbcDriverConnect("DRIVER=Oracle en OraClient11g_home2;UID=USERNAME;PWD=PASSWORD;DBQ=//HOSTNAME:PORT/ORACLE_SID;",
believeNRows = FALSE)
Ex
Conex <- odbcDriverConnect("DRIVER=Oracle en OraClient11g_home2;UID=John;PWD=Deere;DBQ=//fcoracleserver.youdomain:1521/TestEnvironment;",
believeNRows = FALSE)
The hard part is to find the name of the Driver, as you can see mine is on spanish.
What I did is I create first a ODBC Conection using the C:\Windows\System32\odbcad32.exe, there you can check the right name of your Oracle or SQL Server driver.
Once you create the conection, you can use
odbcDataSources() on R, to see that conection and to find out the driver. Thats really the hard part.
Hope it helps !

Resources