Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在RShiny中呈现动态表时出现的问题_R_Shiny - Fatal编程技术网

在RShiny中呈现动态表时出现的问题

在RShiny中呈现动态表时出现的问题,r,shiny,R,Shiny,我有一个json示例,需要在RShiny应用程序中将其解析并可视化为一个表。我的json如下所示 { "line1": [ { "model": "Cooper", "year": 2018, "type": "Hatchback", "motorization": &quo

我有一个json示例,需要在RShiny应用程序中将其解析并可视化为一个表。我的json如下所示

{
    "line1": [
      {
        "model": "Cooper",
        "year": 2018,
        "type": "Hatchback",
        "motorization": "Electric",
        "colour": "Midnight Black",
        "stageID": "MGOP94810482042"
      },
      {
        "model": "BMW",
        "year": 2020,
        "type": "Hatchback",
        "motorization": "Electric",
        "colour": "Eccentric blue",
        "stageID": "M5"
      }
    ],
    "line2": [
      {
        "model": "Cooper",
        "year": 2018,
        "type": "Hatchback",
        "motorization": "Diesel",
        "colour": "Silver",
        "stageID": "MGOP9183740194"
      }
    ]
}
我使用
jsonlite
package将这些数据转换成数据帧,并动态地呈现这些表,而无需对它们进行硬编码。这是我的密码

library(shiny)
library(jsonlite)
library(DT)
library(data.table)

rawJsonData <- fromJSON("Test.json")

ui <- fluidPage(
  h1("My app"),
  br(),
  tags$div(id = "jsontables")
)

server <- function(input, output, session) {

  observe({

    for(i in 1:length(rawJsonData)){
      if(class(rawJsonData[[i]])=="data.frame"){
        tid <- paste0("dt", i)
        tabledata <- rawJsonData[[i]]
        
        insertUI(
            selector = "#jsontables",
            ui = tags$div(
                          DT::dataTableOutput(tid),
                          hr()
            )
          )
        

        output[[tid]] <- DT::renderDataTable(server = FALSE,{
          DT::datatable(tabledata)
        })
        
      }
      
    }
  })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(jsonlite)
图书馆(DT)
库(数据表)

rawJsonData这是通过使用
local
实现的。你不需要一个观察者

library(shiny)
library(DT)

rawJsonData <- list(iris[1:4,], mtcars[1:4,])

ui <- fluidPage(
  h1("My app"),
  br(),
  tags$div(id = "jsontables")
)

server <- function(input, output, session) {
  
  for(i in 1:length(rawJsonData)){
    if(class(rawJsonData[[i]])=="data.frame"){
      tid <- paste0("dt", i)
      
      local({
        
        tabledata <- rawJsonData[[i]]
        id <- tid
        
        insertUI(
          selector = "#jsontables",
          ui = tags$div(
            DTOutput(id),
            hr()
          )
        )
        
        output[[id]] <- renderDT(server = FALSE, {
          datatable(tabledata)
        })
        
      })
      
    }
    
  }
  
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(DT)

rawJsonData很棒的东西!这很有效。你能解释这里出了什么问题吗?@SiddharthArthi我不知道如何解释,但这是一个何时计算表达式的问题。请参阅中的注释。这很有帮助。谢谢!:)