This function is originally from the jsonlite package, file as.scalar.R Unfortunately, that function is not exported from jsonlite, but we need it for our more safe_unbox function above. The call to is.namedlist has been inlined with the actual code.
as.scalar(obj)
The object to convert to a scalar