Ask user crendentials when running R in Terminal - r

I am running an R script from Terminal (not RStudio) and at some point I need the user to write the username and password to access an API. For this, I am using a non-safe approach with the following code:
cat('Write username: ')
username<-readLines("stdin", n=1)
cat('Write password: ')
password<-readLines("stdin", n=1)
I have seen there is a nice implementation in RStudio using the rstudioapi package and the askForPassword function but since I am running my script from Termianl, I wonder if there is any alternative or more secure way to ask for this information

getPass is your best solution.
It supports RStudio and fallsback to tcltk and other backends if missing.
password <- getPass::getPass() is your call.

Related

How to access Olap-Cubes from R on Linux

I want to access an Olap-Cube from R on Linux. Specifically I have a SQL-Server 2016 with Analysis Services and Cubes that I would like to access.
We have been accessing the cubes from R on windows using microsoft R and the olapR-package. And while there is a linux version of microsoft-R, it does not include the olapR-package.
We have installed the R-Services for our SQL-Server. And we did install the olapR package there (using the R-Server Binding), but it seems that it doesn't come with support for mrsdeploy.
What other options do I have to access the cubes from R on linux? We heard about the Revoscaler Package, but don't know how to use it on Cubes. Maybe we could post an sql-script containing r-code to the server and have the sql-server execute the r-code? I didn't find any help on this approach though.
Any help is appreciated.
In our case it does work by embedding the olapR-code within T-SQL within RODBC-Code.
library(RODBC)
my_server="<server>"
my_db="<database>"
my_username="<myusername>"
my_pwd="<mypassword>"
db <- odbcDriverConnect(paste0("DRIVER={SQL Server};
server=",my_server,";
database=",my_db,";
uid=",my_username,";
pwd=",my_pwd))
sql="exec sp_execute_external_script
#language =N'R',
#script=N'
library(olapR)
cnnstr <- \"Data Source=<server>; Provider=MSOLAP; initial catalog=<AnalysisService>; User Id=<domain>\\\\<user>; Password=<myPassword>\"
olapCnn <- OlapConnection(cnnstr)
mdx <- \" <MDX-Query> \"
OutputDataSet <- execute2D(olapCnn, mdx)';"
df <- sqlQuery(db,sql)
Please note the quadruple backslash in domain\user.
Please note that the analysis services login is not necessarily the same as the SQL-login
Please note that the SQL user must have the rights to execute external scripts:
GRANT EXECUTE ANY EXTERNAL SCRIPT TO [<user>]
It could be improved some more by using a "with result sets" statement, but it works.

How to send email from R and windows 7

I'm trying to send an email from R. I'm running windows 7 and it does not recognize the sendmailR package. Please help!!
Error in library(sendmailR) : there is no package called ‘sendmailR’
library(sendmailR)
Error in library(sendmailR) : there is no package called ‘sendmailR’
library(mail)
Error in library(mail) : there is no package called ‘mail’
Thank you for your time.
If you are off and running, great. But this might also assist. A very helpful person sent me this description for how to send emails with R. His last name was Kristjborn, from Sweden, but I can't otherwise credit him.
Steps needed, after signing up at postmarkapp.com and getting Hadley's script at https://gist.github.com/hadley/5707759
Copy your API key from postmarkapp.com (in Credentials tab under your server name)
In R, write:
Sys.setenv(POSTMARKAPP_API_KEY= your-copied-api-key-here)
Sys.setenv(POSTMARKAPP_API_KEY= “xxxx”) # with quotes
In the file from which you want to send the email, use the following code:
source('../postmarkapp.r') #or the path to your postmarkapp.r wherever you store it
source("C:/Users/R/Documents/R/R Scripts/sendgmailwithpostmarkfromHadleygist.R")
mailtext <- "Good morning, \nThis should be sending you emails from R in no time. \nBest regards, \nSender"
send_email(to = '...', from = '...', subject = '...', body = mailtext, attachments = 'path-to-file') # or skip attachments
This should work. However, if the code fails in sourcing the postmarkapp, it is probably due to dependent Libraries. The app depends on:
library(base64enc) library(RJSONIO) library(httr)
If any of these are not installed you need to do so. Note that httr is stored on github which needs to be installed using install_github from the devtools package: http://www.rstudio.com/projects/devtools/

Using sqldf & RPostgrSQL in opencpu app

I'm creating a very simple sqldf app where I want to query postgresql database and return results. Here is my R code:
getproperties <- function(name="") {
options(sqldf.RPostgreSQL.user ="user",
sqldf.RPostgreSQL.password ="password",
sqldf.RPostgreSQL.dbname ="db",
sqldf.RPostgreSQL.host ="server",
sqldf.RPostgreSQL.port =5432,
sqldf.driver = "PostgreSQL")
property <- sqldf("select name, url from aston_hotels.property")
return(property)
}
When I call this using OpenCpu
curl http://localhost/ocpu/user/kesav/library/mylib/getproperties/json --data name=kk
I get the following error
R call failed: process died.
Don't know what's I'm doing wrong. Can anyone point me to an example on how to use DBI from OpenCpu?
Thanks in advance
If the function works when running it on the same machine the terminal but not within OpenCPU, it is likely a security problem. Please check in /var/log/kern.log if there are any lines printed containing DENIED when using the application.
If this is the case, you can add the appropriate privileges the the security profile by editing
/etc/apparmor.d/opencpu.d/custom
Also see the server manual section titled Customizing the security profile on this topic.

R function to connect to wireless

I am running a code for a client. Is there a R function / command to connect the computer to the available wireless and type in the passcode automatically?
Probably not.
However, you can run any OS command with system, so if you have a shell command foo which does what you want, you can just do system("foo") in R.
Most connections to the "outside world" with R are handled through the OS version of system-level 'libcurl' package. There is a package for R called RCurl. The authentication system is described in the variaous pages of:
help(package="RCurl") # brings up the index page.
help(getURL, package="RCurl") # has examples of authentication to a server with R code.
You should also read:
?connections

Writing an R package depending on differing packages per architecture

I want to build a package that involves loading data from mysql using different packages depending on the user's system.
For a windows user it would be through an ODBC connection via package RODBC, while a linux/mac user would use the RMySQL package.
In a script, the following works very well:
if(.Platform$OS.type == "unix") {
library(RMySQL)
} else {
library(RODBC)
}
Now I would like to have these packages loaded at the loading of my package.
I would normally add it in the DESCRIPTION file, under 'Depends:', but this doesn't allow the optional clause.
What is the best way to handle this ?
I think the usual way to solve this is via the .onLoad function (see ?.onLoad or help(".onLoad")).
Section 1.6.3 of the Writing R Extension Manuals gives an overview. Perhaps someone else can point you to a good example, I haven't used it so far.

Resources