【问题标题】:Shiny not displaying table with HTML/JSON error message闪亮的不显示带有 HTML/JSON 错误消息的表格
【发布时间】:2022-01-12 17:31:57
【问题描述】:

我正在尝试构建一个简单的闪亮应用程序,它将发送搜索请求、返回数据框并将其显示在 UI 中。当我运行该应用程序时,起初一切似乎都正常运行,但当我运行查询时,我得到一个 html/json 错误。

代码如下:

    ui <- fluidPage(

    # Application title
    titlePanel("My App"),


    sidebarLayout(
        sidebarPanel(
            textInput('dataset_name',
                      'Dataset:',
                       placeholder = 'Name')
        ,
        br(),
        actionButton("button", "Search"),
        ),
      
        mainPanel(
           tableOutput('userTable')
        ),
        position = c("left"),
        fluid=FALSE
    )
)

    server <- function(input, output) {

      ut.df <- eventReactive(input$button, {
      
                  ds <- dataSearch(input$datset_name)
                  return(ds)             
      })
  
       output$userTable <- renderTable({ut.df()})
   
}

dataSearch 是我创建的函数,用于将input$dataset_name 值发送到 api 调用并返回结果的数据帧。我已经测试了该函数,它解析响应 JSON 并返回数据帧没有问题。

当我运行闪亮的应用程序时,页面加载没有问题,但是当我提交查询时,我得到的不是将数据框呈现为表格:

Warning: Error in : lexical error: invalid char in json text.
                                   <!DOCTYPE HTML PUBLIC "-//W3C//
                 (right here) ------^

谁能解释为什么表格没有被渲染以及为什么闪亮似乎认为 html 代码是一个 json 文件?

会话信息:

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DT_0.20        jsonlite_1.7.2 httr_1.4.2     shiny_1.7.1   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        jquerylib_0.1.4   bslib_0.3.1       compiler_4.1.2   
 [5] pillar_1.6.4      later_1.3.0       neo4r_0.1.1       tools_4.1.2      
 [9] digest_0.6.28     lattice_0.20-45   lifecycle_1.0.1   tibble_3.1.6     
[13] png_0.1-7         pkgconfig_2.0.3   rlang_0.4.12      Matrix_1.3-4     
[17] cli_3.1.0         rstudioapi_0.13   crosstalk_1.2.0   yaml_2.2.1       
[21] curl_4.3.2        fastmap_1.1.0     withr_2.4.2       dplyr_1.0.7      
[25] htmlwidgets_1.5.4 sass_0.4.0        rappdirs_0.3.3    generics_0.1.1   
[29] vctrs_0.3.8       rprojroot_2.0.2   grid_4.1.2        attempt_0.3.1    
[33] tidyselect_1.1.1  fontawesome_0.2.2 here_1.0.1        reticulate_1.22  
[37] glue_1.5.0        data.table_1.14.2 R6_2.5.1          fansi_0.5.0      
[41] purrr_0.3.4       tidyr_1.1.4       magrittr_2.0.1    promises_1.2.0.1 
[45] ellipsis_0.3.2    htmltools_0.5.2   mime_0.12         xtable_1.8-4     
[49] httpuv_1.6.3      utf8_1.2.2        cachem_1.0.6      crayon_1.4.2 

【问题讨论】:

    标签: html r json shiny


    【解决方案1】:

    此错误表示您尝试使用 {jsonlite} 读取的文档不是 JSON 文件,而是 HTML 文件。

    例如,您可以通过以下方式重现此错误:

    > jsonlite::read_json("https://google.com")
    Error in parse_con(txt, bigint_as_char) : 
      lexical error: invalid char in json text.
                                           <!DOCTYPE html><html lang="fr" 
                         (right here) ------^
    

    所以你需要确保你读取的 JSON 是正确的。

    科林

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      相关资源
      最近更新 更多