0

I'm a newbie R user and couldn't find any answers which I could understand.

My objective is to retrieve information from a URL and then convert the information into a data frame for use.

install.packages("jsonlite")

library(jsonlite)

fromJSON("https://developers.onemap.sg/commonapi/search?searchVal=revenue&returnGeom=Y&getAddrDetails=Y&pageNum=1)") 
#it works

x1 <- as.character("https://developers.onemap.sg/commonapi/search?searchVal=revenue&returnGeom=Y&getAddrDetails=Y&pageNum=1)")

fromJSON(x1) #also works

fromJSON(paste("x",1,sep=""))

=> Error: lexical error: invalid char in json text.    

Where did I go wrong? I verified that paste("x",1,sep=="") is equivalent to "x1", so shouldn't it work?

In any case, I was actually planning to put a list of postal codes in a data frame to replace each searchVal= to generate a list of URLs, get a data frame for each URL then merge them all together. But given the difficult I face above, I guess I will try to get help from the above first.

Thanks a lot in advance!

ZJC
  • 1
  • 1
  • Simply build a list of urls and then use lapply for dataframes: `json_list <- lapply(urls_list, function(i) fromJSON(i)$results`. No need for separate variables. – Parfait Feb 06 '18 at 14:52

2 Answers2

1

I think you are trying to use a string as a variable, in current set up R knows x1 however 'x1' is nothing but just a string. I hope someone might be able to explain better. So you need to use. eval.

fromJSON(eval(parse(text=paste('x',1,sep=''))))
Biranjan
  • 303
  • 3
  • 12
  • thanks! that worked. I'll look forward to someone who can explain better - I don't see the reason why R is able to understand what x1 means but is unable to understand what paste('x',1,sep='') means. will have to look at what ?parse and ?eval do (then understand), but if anyone is feeling charitable, you can help to explain that too! – ZJC Feb 06 '18 at 15:00
  • It applies to other languages as well, for example, python has it own eval(). What `paste('x',1,sep='')` does is create character **'x1'** which is not equal to variable **x1**, for that reason we pass the character to eval, which tries to evaluate it as variable[someone can correct me if I am wrong]. Try reading the documentation on eval() function I hope it helps. – Biranjan Feb 06 '18 at 15:47
0

you're trying to use string as variable. these are completely different data types.

see there: https://www.statmethods.net/input/datatypes.html

string is character vector so when you do

paste("x",1,sep="")

you're getting character vector "x1"

whereas when you do (btw I corrected " and ) for you at the end)

x1 <- as.character("https://developers.onemap.sg/commonapi/search?searchVal=revenue&returnGeom=Y&getAddrDetails=Y&pageNum=1")

you're getting variable x1 that contains url in form of a string. you can use it somewhere else.

another example:

region <- "10000032"
url <- paste("https://esi.tech.ccp.is/latest/markets/", region, "/orders/?order_type=sell&page=1", sep="")

should give you variable url which contains character vector "https://esi.tech.ccp.is/latest/markets/10000032/orders/?order_type=sell&page=1"

then you could parse it from json to data frame with:

df <- fromJSON(url)
drsh1
  • 54
  • 7