【问题标题】:How to load online JSON data to shiny app with jsonlite?如何使用 jsonlite 将在线 JSON 数据加载到闪亮的应用程序?
【发布时间】:2015-12-01 16:41:09
【问题描述】:

我正在尝试制作从该 api 获取数据的闪亮应用程序:https://www.riigiteenused.ee/api/et/all。我需要使用 jsonlite::fromJSON,因为它有很好的 flatten 功能。当我使用以下代码时(最小的例子,在现实生活中我用数据做更多的事情):

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(validate(raw) 显示词法错误:字符串中的无效字符 \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  

【问题讨论】:

    标签: json r shiny jsonlite


    【解决方案1】:

    不要跳过ssl,试试

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

    【讨论】:

    • 仍然给我同样的错误:curl::curl_fetch_memory(url, handle = handle) 中的错误:对等证书无法使用给定的 CA 证书进行身份验证
    • 嗯,我不知道如何提供帮助。这个电话对我有用。想知道在闪亮的应用程序中是否进行了糟糕的交互
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-19
      • 2017-01-15
      • 1970-01-01
      • 2018-07-28
      • 2017-07-27
      • 2016-07-06
      • 2015-06-15
      • 2019-05-28
      • 2016-11-01
      相关资源
      最近更新 更多