我正在尝试创建一个闪亮的应用程序来获取来自此API的数据:https://www.riigiteenused.ee/api/et/all。我需要使用jsonlite :: fromJSON,因为它具有良好的展平功能。当我使用下面的代码时(最小的例子,在现实生活中我用数据做更多的东西):
library(jsonlite)
data=fromJSON("https://www.riigiteenused.ee/api/et/all")
server <- function(input, output) {
output$tekst <- renderText({
nchar(data)
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel(textOutput("tekst"))
))
shinyApp(ui = ui, server = server)
我收到以下错误消息:
Error in open.connection(con, "rb") :
Peer certificate cannot be authenticated with given CA certificates
我尝试了以下内容(绕过ssl verify peer):
library(RCurl)
raw <- getURL("https://www.riigiteenused.ee/api/et/all",
.opts = list(ssl.verifypeer = FALSE), crlf = TRUE)
data=fromJSON(raw)
它读入原始数据,但是搞乱JSON(验证(原始)显示词法错误:字符串内部无效字符\ n,导致跟随错误):
Error: lexical error: invalid character inside string.
ressile: laevaregister@vta.ee. Avaldusele soovitatavalt lis
(right here) ------^
我尝试过的一个想法是:
data=fromJSON(readLines("https://www.riigiteenused.ee/api/et/all"))
它在我的计算机上工作正常,但是当我将它上传到shinyapps.io应用程序并不起作用时,我会看到错误:
Error in file(con, "r") : https:// URLs are not supported
如果有办法从json函数使用jsonlite从https toshiny应用程序加载JSON数据,有人可以给我一个线索吗?
我的会话信息如下:
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Estonian_Estonia.1257 LC_CTYPE=Estonian_Estonia.1257
[3] LC_MONETARY=Estonian_Estonia.1257 LC_NUMERIC=C
[5] LC_TIME=Estonian_Estonia.1257
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] jsonlite_0.9.19 httr_1.0.0 RCurl_1.95-4.7 bitops_1.0-6 shiny_0.12.2
loaded via a namespace (and not attached):
[1] Rcpp_0.12.2 digest_0.6.8 mime_0.4 R6_2.1.1
[5] xtable_1.7-4 magrittr_1.5 stringi_1.0-1 curl_0.9.4
[9] tools_3.2.2 stringr_1.0.0 httpuv_1.3.3 rsconnect_0.4.1.4
[13] htmltools_0.2.6
答案 0 :(得分:1)
请勿跳过ssl,尝试
fromJSON(content(GET("https://www.riigiteenused.ee/api/et/all"), "text"))
答案 1 :(得分:0)
我尝试了这个在我的计算机和闪亮服务器中运行良好的解决方案:
library(rjson)
library(jsonlite)
fromJSON(url, flatten=T)