Using R to connecting to MongoDB password with '#' - r

This is similar to the question here:
MongoDB password with "#" in it
However, I'm using the R package mongolite to connect so I can't apply that solution. Any recommendations on how to modify it so that it still works for the mongolite package
For example if my username is: user1, and my password is: p#ssword, how would I write:
mongodb://user1:p#ssword#myip
Thank you!

I used this structure and worked for me
newdb <- mongo(collection= "name_collection",
db = "name_db",
url = "mongodb://user:password#your_ip:port_number/name_db",
verbose = TRUE)

I just ran into this problem and solved it by escaping the # in the password by using its corresponding percent-encoding, e.g. use%40 to ecape it. So in your case:
mongodb://user1:p%40ssword#myip
should work.
Edit: I did not see you already had the answer in a comment :) The escape worked in my case so I assume you may have a mistake in either your username or password.

In your p#ssword # character is creating problem so replace it by %40 as suggested by https://stackoverflow.com/users/4455096/rapture

Related

Avoid 'Do you want to proceed? [y/N]:' in R by auto answering y for all lines

Can this be done in R? where am R script can be run without interruption by any function.
This Do you want to proceed? [y/N]: was generated from renv::restore(), but regardless, can all promotes be ignored.
Any input is helpful, Thanks.
Afaik all renv commands (restore() definitely) have the prompt argument:
prompt
Boolean; prompt the user before taking any action? For backwards compatibility, confirm is accepted as an alias for prompt.
Use prompt=FALSE (default is interactive()) to turn off the prompting.

zsh array assignment and no match errors

zsh version 5.2
I'm attempting an array assignment using filename generation like so:
files=(/some/path/*/dir/myfile)
Indeed this is the way the zshoptions manual recommends to achieve what I want.
When no matches exist I want the array to be empty. Instead it's producing
no matches found: /some/path/*/dir/file
and the script terminates.
I've tried setting NULL_GLOB, CSH_NULL_GLOB and ensured NOMATCH is not set.
When matches do exist it works as expected.
Any help is appreciated.
Thank you in advance,
Wayne
Well of course I found the solution after posting my question.
For this to work EXTENDED_GLOB needs to be set as well as NULL_GLOB. Or a glob qualifier can be used so that NULL_GLOB only effects this particular expansion.
This is how to set NULL_GLOB for a single operation:
files=(/some/path/*/dir/myfile(N))
Hope that can help someone else who encounters this.
Wayne

R:Using a character as argument in a function

I´m new here and I have not much knowledge in the use of R. I cant find a solution for my current problem: In the use of a character (Path) as an argument for my function.
Path <- "C:/...../"
foo <-function(Path){
Driver <- "Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=Path"
connect <- odbcDriverConnect(Driver)
return(connect)
}
My problem is, that Path will be replaced in the function with the quotes. At least I have the following format in my function.
...DBQ="C:/..../""
I tried to fix this problem with noquote or cat to delete the quotes, but it doesnt help.
I thank you in advance that you are helping a beginner in R :)
You can use sprintf() to insert the Path.
Path <- "C:/...../"
sprintf("Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=\"%s\"", Path)
# [1] "Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=\"C:/...../\""
So your updated function would be
foo <- function(Path) {
Driver <- "Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=\"%s\""
connect <- odbcDriverConnect(sprintf(Driver, Path))
return(connect)
}
See help(sprintf) for all its amazing uses.
Update: Since it's not clear to me whether you want the quotes around Path, I will include the way to have it without them. If you don't want the quotes in the string, remove them from the sprintf() format.
Path <- "C:/...../"
sprintf("Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=%s", Path)
# [1] "Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:/...../"

Why url.exists returns FALSE when the URL does exists using RCurl?

For example:
if(url.exists("http://www.google.com")) {
# Two ways to submit a query to google. Searching for RCurl
getURL("http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=RCurl&btnG=Search")
# Here we let getForm do the hard work of combining the names and values.
getForm("http://www.google.com/search", hl="en", lr="",ie="ISO-8859-1", q="RCurl", btnG="Search")
# And here if we already have the parameters as a list/vector.
getForm("http://www.google.com/search", .params = c(hl="en", lr="", ie="ISO-8859-1", q="RCurl", btnG="Search"))
}
This is an example from RCurl package manual. However, it does not work:
> url.exists("http://www.google.com")
[1] FALSE
I found there is an answer to this here Rcurl: url.exists returns false when url does exists. It said this is because of the default user agent is not useful. But I do not understand what user agent is and how to use it.
Also, this error happened when I worked in my company. I tried the same code at home, and it worked find. So I am guessing this is because of proxy. Or there is some other reasons that I did not realize.
I need to use RCurl to search my queries from Google, and then extract the information such as title and descriptions from the website. In this case, how to use user agent? Or, does the package httr can do this?
guys. Thanks a lot for help. I think I just figured out how to do it. The important thing is proxy. If I use:
> opts <- list(
proxy = "http://*******",
proxyusername = "*****",
proxypassword = "*****",
proxyport = 8080
)
> url.exists("http://www.google.com",.opts = opts)
[1] TRUE
Then all done! You can find your proxy under System-->proxy if you use win 10. At the same time:
> site <- getForm("http://www.google.com.au", hl="en",
lr="", q="r-project", btnG="Search",.opts = opts)
> htmlTreeParse(site)
$file
[1] "<buffer>"
.........
In getForm, opts needs to be put in as well. There are two posters here (RCurl default proxy settings and Proxy setting for R) answering the same question. I have not tried how to extract information from here.

How do I query using a variable value in mongodb from R?

I am new in rmongodb. I would like to make a query in one collection in MongoDB using the value of a variable. It goes like this:
OBS_ID<-"20150510120000"
QUERY_DATA<-mongo.find.all(MONGO, "prod.CUSTDATA", query='{"Obs_ID" :
OBS_ID}')
This code doesn't work. I can not find any related documentation in rmongodb on how to deal with this.
Looking forward for your answers! Thank you!
I think you should try dbGetQuery as below :
library('RMongo')
dbName <- mongoDbConnect('dbname')
query <- dbGetQuery(dbName,'collectionName',"{'Obs_ID' : '20150510120000'}")
I have already found the solution to this problem. I need to create a mongo.bson.buffer object.
Set up the query
OBS_ID<-"20150510120000"
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.object(buf,"Obs_ID")
mongo.bson.buffer.append(buf,"$eq",OBS_ID)
criteria <- mongo.bson.from.buffer(buf)
Query from MongoDB using the criteria
QUERY<-mongo.find.all(MONGO, "prod.CUSTDATA", query=criteria)

Resources