{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/shiny/issues/742","labels_url":"https://api.github.com/repos/rstudio/shiny/issues/742/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/shiny/issues/742/comments","events_url":"https://api.github.com/repos/rstudio/shiny/issues/742/events","html_url":"https://github.com/rstudio/shiny/issues/742","id":58697376,"number":742,"title":"Error in server.r -> decodeMessage -> fromJSON (hard to track down)","user":{"login":"skranz","id":1413022,"avatar_url":"https://avatars.githubusercontent.com/u/1413022?v=3","gravatar_id":"","url":"https://api.github.com/users/skranz","html_url":"https://github.com/skranz","followers_url":"https://api.github.com/users/skranz/followers","following_url":"https://api.github.com/users/skranz/following{/other_user}","gists_url":"https://api.github.com/users/skranz/gists{/gist_id}","starred_url":"https://api.github.com/users/skranz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skranz/subscriptions","organizations_url":"https://api.github.com/users/skranz/orgs","repos_url":"https://api.github.com/users/skranz/repos","events_url":"https://api.github.com/users/skranz/events{/privacy}","received_events_url":"https://api.github.com/users/skranz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-24T06:03:16Z","updated_at":"2015-02-24T06:39:53Z","closed_at":null,"body":"When using and developing my shiny based package [RTutor](https://github.com/skranz/RTutor), I encountered the following error message that greys out my shiny app:\r\n```\r\n   Error in fromJSON(content, handler, default.size, depth, allowComments,  : \r\ninvalid JSON input\r\n```\r\nAfter quite some search, I believe the error originates in the function `decodeMessage` in shiny's `server.r` in lines  260-262, see https://github.com/rstudio/shiny/blob/master/R/server.R#L260-262.\r\n\r\n```\r\n  # use native encoding for the message\r\n  nativeData <- iconv(rawToChar(data), 'UTF-8')\r\n  return(fromJSON(nativeData, asText=TRUE, simplify=FALSE))\r\n```\r\n\r\nWhile not really understanding when this code is called or what it does, I found that the following bugfix worked:\r\n\r\n```\r\n    # use native encoding for the message\r\n    charData = rawToChar(data)\r\n    nativeData <- iconv(charData, 'UTF-8')\r\n    # if conversion to 'UTF-8' fails, use original encoding \r\n    if (is.na(nativeData)) nativeData = charData\r\n    return(fromJSON(nativeData, asText=TRUE, simplify=FALSE))\r\n```\r\nSince RTutor does not run stable with this error, it would be really great if you could include this bug fix (or an alternative bug fix) into shiny.\r\n\r\nThe problem emerged when at some point the function is called with some output from R's `summary` function that makes the return from rawToChar(data) look as follows:\r\n\r\n```\r\nrawToChar(data)\r\n\r\n[1] \"{\\\"method\\\":\\\"update\\\",\\\"data\\\":{\\\"chunk_1__console\\\":\\\"You successfully solved the chunk!\\\\n> T = 10\\\\n> x = 1:T\\\\n> y = x + rnorm(T)\\\\n> summary(lm(y ~ x))\\\\n\\\\nCall:\\\\nlm(formula = y ~ x)\\\\n\\\\nResiduals:\\\\n    Min      1Q  Median      3Q     Max \\\\n-1.6760 -0.8871  0.2210  0.6868  1.3790 \\\\n\\\\nCoefficients:\\\\n            Estimate Std. Error t value Pr(>|t|)    \\\\n(Intercept)  -0.2604     0.7549  -0.345    0.739    \\\\nx             1.0848     0.1217   8.917 1.98e-05 ***\\\\n---\\\\nSignif. codes:  0 Â‘***Â’ 0.001 Â‘**Â’ 0.01 Â‘*Â’ 0.05 Â‘.Â’ 0.1 Â‘ Â’ 1\\\\n\\\\nResidual standard error: 1.105 on 8 degrees of freedom\\\\nMultiple R-squared:  0.9086,\\\\tAdjusted R-squared:  0.8972 \\\\nF-statistic: 79.51 on 1 and 8 DF,  p-value: 1.984e-05\\\\n\\\\n\\\"}}\"\r\n```\r\nThe special characters `Â‘.Â’` (around the signifcance stars in the summary output) cause `iconv` to return `NA`, which then throws an error in the subsequent `fromJSON` call. My new line\r\n```\r\n if (is.na(nativeData)) nativeData = charData\r\n```\r\nsays that if conversion to `UTF-8` fails, the string shall be kept in original format, which then makes fromJSON run without error (and seems to work fine for me).\r\n\r\nI tried to reproduce the error with a simple shiny app that does not use RTutor, but unfortunately, was not able to. More complicated code and instructions to reproduce the error, are explained in this gist:\r\n\r\nhttps://gist.github.com/skranz/5f200040b548eef9e889\r\n\r\nI did not understand when exactly the error occurs, but here is a traceback that perhaps might be helpful for you:\r\n```\r\n21: runApp(list(ui = app$ui, server = app$server), ...)\r\n20: shinyCallingHandlers(while (!.globals$stopped) {\r\n        serviceApp()\r\n        Sys.sleep(0.001)\r\n    })\r\n19: withCallingHandlers(expr, error = function(e) {\r\n        handle <- getOption(\"shiny.error\")\r\n        if (is.function(handle)) \r\n            handle()\r\n       ...\r\n\r\n18: serviceApp()\r\n17: service(timeout)\r\n16: run(timeoutMs)\r\n15: tryCatch(evalq((structure(function (handle, binary, message) \r\n    {\r\n        for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) {\r\n            result <- try(handler(binary, message))\r\n       ...\r\n\r\n14: tryCatchList(expr, classes, parentenv, handlers)\r\n13: tryCatchOne(expr, names, parentenv, handlers[[1L]])\r\n12: doTryCatch(return(expr), name, parentenv, handler)\r\n11: evalq((structure(function (handle, binary, message) \r\n    {\r\n        for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) {\r\n            result <- try(handler(binary, message))\r\n       ...\r\n\r\n10: eval(substitute(expr), envir, enclos)\r\n9: (structure(function (handle, binary, message) \r\n   {\r\n       for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) {\r\n           result <- try(handler(binary, message))\r\n      ...\r\n\r\n8: try(handler(binary, message))\r\n7: tryCatch(expr, error = function(e) {\r\n       call <- conditionCall(e)\r\n       if (!is.null(call)) {\r\n           if (identical(call[[1L]], quote(doTryCatch))) \r\n      ...\r\n\r\n6: tryCatchList(expr, classes, parentenv, handlers)\r\n5: tryCatchOne(expr, names, parentenv, handlers[[1L]])\r\n4: doTryCatch(return(expr), name, parentenv, handler)\r\n3: handler(binary, message)\r\n2: decodeMessage(msg)\r\n```"},"comment":{"url":"https://api.github.com/repos/rstudio/shiny/issues/comments/75707257","html_url":"https://github.com/rstudio/shiny/issues/742#issuecomment-75707257","issue_url":"https://api.github.com/repos/rstudio/shiny/issues/742","id":75707257,"user":{"login":"wch","id":86978,"avatar_url":"https://avatars.githubusercontent.com/u/86978?v=3","gravatar_id":"","url":"https://api.github.com/users/wch","html_url":"https://github.com/wch","followers_url":"https://api.github.com/users/wch/followers","following_url":"https://api.github.com/users/wch/following{/other_user}","gists_url":"https://api.github.com/users/wch/gists{/gist_id}","starred_url":"https://api.github.com/users/wch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wch/subscriptions","organizations_url":"https://api.github.com/users/wch/orgs","repos_url":"https://api.github.com/users/wch/repos","events_url":"https://api.github.com/users/wch/events{/privacy}","received_events_url":"https://api.github.com/users/wch/received_events","type":"User","site_admin":false},"created_at":"2015-02-24T06:39:53Z","updated_at":"2015-02-24T06:39:53Z","body":"Could you please do the following? This should help us to debug the issue.\r\n\r\n* Get to the debugger prompt right before it assigns the value to `nativeData`.\r\n* Run `dput(data)`.\r\n* Copy and paste the output in a new comment.\r\n"}}