1

I think I have exhausted the entire internet looking for an example / answer to my query regarding implementing a h2o mojo model to predict within RShiny. We have created a bunch of models, and wish to predict scores in a RShiny front end where users enter values. However, with the following code to implement the prediction we get an error of

Warning: Error in checkForRemoteErrors: 6 nodes produced errors; first error: No method asJSON S3 class: H2OFrame

dataInput <- dfName
dataInput <- toJSON(dataInput)

rawPred <- as.data.frame(h2o.predict_json(model= "folder/mojo_model.zip",  json = dataInput, genmodelpath = "folder/h2o-genmodel.jar"))

Can anyone help with some pointers? Thanks, Siobhan

Siobhan
  • 163
  • 11
  • Have you tried running the model in the console first? This looks like an h20 issue rather than a shiny issue, but you need to post a sample of your data and any other code you're using so that it's reproducible before anyone will really be able to give you a comprehensive answer. Thanks :) – mysteRious Mar 21 '18 at 16:44
  • Thanks, ive been trying to create a clean version with no company data in to load up. Will complete tomorrow. We can get the model to run fine with binary version, but console or not mojo cant find its mojo! – Siobhan Mar 21 '18 at 17:05

2 Answers2

2

This is not a Shiny issue. The error indicates that you're trying to use toJSON() on an H2OFrame (instead of an R data.frame), which will not work because the jsonlite library does not support that.

Instead you can convert the H2OFrame to a data.frame using:

dataInput <- toJSON(as.data.frame(dataInput))

I can't guarantee that toJSON() will generate the correct input for h2o.predict_json() since I have not tried that, so you will have to try it out yourself. Note that the only way this may work is if this is a 1-row data.frame because the h2o.predict_json() function expects a single row of data, encoded as JSON. If you're trying to score multiple records, you'd have to loop over the rows. If for some reason toJSON() doesn't give you the right format, then you can use a function I wrote in this post here to create the JSON string from a data.frame manually.

There is a ticket open to create a better version of h2o.predict_json() that will support making predictions from a MOJO on data frames (with multiple rows) without having to convert to JSON first. This will make it so you can avoid dealing with JSON altogether.

An alternative is to use a H2O binary model instead of a MOJO, along with the standard predict() function. The only requirement here is that the model must be loaded into H2O cluster memory.

Erin LeDell
  • 8,704
  • 1
  • 19
  • 35
  • Thanks SO much Erin. We will give that a go, as it looks like this may be the way forward. We had been using binary models to date but as we are migrating from NTT to AWS the preference is for mojo so we can get around build vs deployment version issues. Thanks and ill update you as to our progress. – Siobhan Mar 22 '18 at 09:15
  • Hi Erin, we appear to have made some progress, Ie the json gets created fine, but we are seeing the following error message now...."Could not find or load main class water.util.H2OPredictor " – Siobhan Mar 26 '18 at 11:13
  • Hi. We managed to get this error to disappear and have a prediction coming out, which is amazing thanks! The key was to bin toJSON and use your code from https://stackoverflow.com/a/47784930/5451344. However, we now seem to not be able to include factors in the model where the values contain a space or ampersand. We have tried all ways to escpate them but to no avail...any ideas? thanks, – Siobhan Mar 27 '18 at 09:49
  • From the code df<- data.frame (a=0,b=0,c=0,d=1,e=0,f=0,g="METAL",h = "LONDON & SE") dfstr <- sapply(1:ncol(df), function(i) paste(paste0('\"', names(df)[i], '\"'), df[1,i], sep = ':')) json <- paste0('{', paste0(dfstr, collapse = ','), '}') dataPredict <- as.data.frame(h2o.predict_json(model = "D:\\GBM_grid.zip", json = json, genmodelpath = "D:\\h2o-genmodel.jar")) this is the error "error: Factor w/ 1 level "com.google.gson.JsonSyntaxException: Malformed JSON\r\n\tat water.util.H2OPredictor.jsonToRowData(H2OPredictor."| __truncated__: 1" – Siobhan Mar 27 '18 at 16:33
  • We seem to have fixed this issue by introducing '' around out strings with spaces in them so "LONDON & SE" becomes "'LONDON & SE'". SO far this has worked! – Siobhan Apr 03 '18 at 11:05
0

The following works now using the json formatting from first two lines and the single quote around var with spaces.

df<- data.frameV1=1,V2=1,CMPNY_EL_IND=1,UW_REGION_NAME = "'LONDON & SE'" )
    dfstr <- sapply(1:ncol(df), function(i) paste(paste0('\"', names(df)[i], '\"'), df[1,i], sep = ':'))
    json <- paste0('{', paste0(dfstr, collapse = ','), '}')
    dataPredict <- as.data.frame(h2o.predict_json(model = "D:\\GBM_model_0_CMP.zip", json = json, genmodelpath = "D:\\h2o-genmodel.jar", labels = TRUE))
Siobhan
  • 163
  • 11