如何使用 jsonlite 将在线 JSON 数据加载到闪亮的应用程序?

How to load online JSON data to shiny app with jsonlite?

我正在尝试制作从 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 验证对等点):

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

有人能给我一个线索吗,如果有办法使用 jsonlite fromJSON 函数从 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  

不要跳过 ssl,尝试

fromJSON(content(GET("https://www.riigiteenused.ee/api/et/all"), "text"))

我试过这个解决方案,在我的电脑和闪亮的服务器上运行良好:

  library(rjson)
  library(jsonlite)
  fromJSON(url, flatten=T)