1

I tried to run this code in a rmd file but still I'm getting an error and I don't know how to resolve this. The Code is: pizza3 <- fromJSON('http://www.jaredlander.com/data/PizzaFavorites.json')

And I'm getting an error as:

Error in fromJSON("http://www.jaredlander.com/data/PizzaFavorites.json") :
unexpected character 'h' Calls: <Anonymous> ... withCallingHandlers ->
withVisible -> eval -> eval -> fromJSON```

Francis Barton
  • 129
  • 2
  • 16

1 Answers1

0

@Rohit this works for me, maybe you haven't loaded jsonlite as Stéphane has suggested.

library(jsonlite)
pizza3 <- jsonlite::fromJSON("http://www.jaredlander.com/data/PizzaFavorites.json")
pizza3
#>                     Name                                Details
#> 1          Di Fara Pizza     1424 Avenue J, Brooklyn, NY, 11230
#> 2          Fiore's Pizza   165 Bleecker St, New York, NY, 10012
#> 3              Juliana's  19 Old Fulton St, Brooklyn, NY, 11201
#> 4     Keste Pizza & Vino   271 Bleecker St, New York, NY, 10014
#> 5  L & B Spumoni Gardens      2725 86th St, Brooklyn, NY, 11223
#> 6 New York Pizza Suprema       413 8th Ave, New York, NY, 10001
#> 7           Paulie Gee's 60 Greenpoint Ave, Brooklyn, NY, 11222
#> 8                Ribalta      48 E 12th St, New York, NY, 10003
#> 9              Totonno's  1524 Neptune Ave, Brooklyn, NY, 11224

Created on 2020-02-28 by the reprex package (v0.3.0)

Francis Barton
  • 129
  • 2
  • 16
  • Yeah, it worked.But can't I pass library(jsonlite) in another chunk in R Markdown. – Rohit Jhunjhunwala Feb 28 '20 at 13:45
  • Sorry @RohitJhunjhunwala but I don't understand your sentence here. Try adding your `library(jsonlite)` call in a chunk at the top of your Rmarkdown document and when you render or knit or run the chunk it will attach the package and your code should work. Have you managed to attach other packages but not `jsonlite`? The `library` line does not have to be in the same chunk as the pizza line. Or it can be. It just needs to be done before you try to use `fromJSON`. – Francis Barton Feb 28 '20 at 15:26
  • I tried to attach library(jsonlite) in a separate chunk and pass the code of pizza3 in separate one but it shows the above error still. – Rohit Jhunjhunwala Feb 29 '20 at 16:52
  • I knit the code with library(jsonlite) in another chunk which is before that pizza3 code's chunk and it is giving the above error. – Rohit Jhunjhunwala Feb 29 '20 at 16:54
  • OK you're going to have to provide a full (but minimal, please!) reprex of what you are doing so we can have a look. Please remove all the code which is not relevant to the problem. You might solve it yourself in the process! Also maybe try uninstalling and reinstalling the jsonlite package in case it has got corrupted or something. Do you get any error message when you attach it, or when you install it? – Francis Barton Feb 29 '20 at 19:00
  • It may be that there is another package (`rjson`?)providing the function `fromJSON` - try specifying `jsonlite::fromJSON` in your code. Try starting a completely new R script file with a fresh R environment (start new R session) and building up bit by bit until you reproduce the error. – Francis Barton Feb 29 '20 at 19:05
  • First of all, I passed all the libraries that I need in one chunk (and it does not contain rjson) and in the next chunk i passed above pizza3 code.At that position, it shows error.If I use jsonlite:: in pizza3 code chunk than it works perfectly.And I tried to work in new R environment, but still it gives the same error. – Rohit Jhunjhunwala Mar 01 '20 at 13:49
  • I just made a minimal example of an Rmd file for myself with the `library(jsonlite)` call in a separate chunk to the rest of the code and it still worked fine. You're going to have to strip your code down to absolute basics and build it up again until you locate where and why the error is occurring. I also suggest uninstalling the jsonlite package and reinstalling it. I'm sorry I can't help you any more with this. – Francis Barton Mar 02 '20 at 12:46