Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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
在闪亮的应用程序中显示来自PlumberAPI的html小部件_R_Shiny_R Highcharter_Plumber - Fatal编程技术网

在闪亮的应用程序中显示来自PlumberAPI的html小部件

在闪亮的应用程序中显示来自PlumberAPI的html小部件,r,shiny,r-highcharter,plumber,R,Shiny,R Highcharter,Plumber,我正试图显示通过管道工API请求的交互式图形,并将其显示在一个闪亮的应用程序中。 我不知道如何使用highcharter等工具使其工作。 下面是我的示例应用程序,它使用api绘制基本图和highcharter图 我有api,但有人知道如何解析htmlwidget输入以显示吗 谢谢你的帮助 示例api,开始使用 library(plumber) r <- plumb("api.R") r$run(port=8000) 图书馆(水管工) r基于这个答案的想法,我成功地在一个闪亮的应用程序中

我正试图显示通过管道工API请求的交互式图形,并将其显示在一个闪亮的应用程序中。 我不知道如何使用highcharter等工具使其工作。 下面是我的示例应用程序,它使用api绘制基本图和highcharter图

我有api,但有人知道如何解析htmlwidget输入以显示吗

谢谢你的帮助

示例api,开始使用

library(plumber)
r <- plumb("api.R") 
r$run(port=8000)
图书馆(水管工)

r基于这个答案的想法,我成功地在一个闪亮的应用程序中呈现了一个水管工制作的小部件:。所需的只是将html对象标记插入到闪亮的UI中:

tags$html(HTML('<object data="<LINK TO YOUR WIDGET HERE>" width="100%" height="500px" type="text/html"> </object>')
标记$html(html(“”)

请注意,这在RStudio viewer中不起作用。它在Chrome(v71.0.3578.98)中起作用,但在Edge或IE中不起作用。

我认为这里的主要问题是如何可视化由
get\u iris\u highchart()创建的XML文档
函数。我不确定这方面是否有任何源代码,最好看一下
httr
库文档。问题是,你不能将htmlwidget请求中的HTML内联到你闪亮的UI中。如果你看一下源代码,它会有
标记、
标记和s将需要使用
标记的补充JavaScript。这些都是很难放入现有HTML页面并使其正常工作的内容。相反,您可能希望将它们放在iframe中,这样可以将页面嵌入页面中。请尝试
作为参考,在plumber git上提出相同(或类似)的问题:
# Application
library(shiny)
library(shinyjs)
library(shinydashboard)
library(httr)
library(grid)
library(ggplot2)

ui <- dashboardPage(
  dashboardHeader(title = "Image and Widget", titleWidth = 300),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    useShinyjs(), 
    fluidRow(
      column(width = 6, 
             shinydashboard::box(width = 12, 
                                 htmlOutput("species_selector"), 
                                 actionButton(inputId = "filter_action", label = "Filter", icon("search"), 
                                              style = "color: #fff; background-color: #337ab7; border-color: #2e6da4") 
             ) 
      ) 
    ), 
    fluidRow(
      column(width = 6, 
             shinyjs::hidden( 
               div(id = "iris_chartbox",
                   shinydashboard::tabBox(width = 12, 
                                          tabPanel(title = "Iris Base Plot", width = 12, 
                                                   imageOutput("iris_base_plot")
                                          ), 
                                          tabPanel(title = "Iris highchart", width = 12, 
                                                   uiOutput("iris_highchart")
                                          )
                   )
               ) 
             ) 
      )
    )
  )
)

server <- function(input, output) {

  # Make product line selector ----
  output$species_selector <- renderUI({ 
    selectInput( 
      inputId = "species_chosen",  
      label = "Species Chosen", 
      choices = c("setosa", "virginica", "versicolor")
    )
  })

  # Observe button click ----
  observeEvent(input$filter_action, { 
    # Make iris graph ----
    output$iris_base_plot <- renderImage({

      # A temp file to save the output. It will be deleted after renderImage
      # sends it, because deleteFile=TRUE.
      outfile <- tempfile(fileext = '.png')

      # Generate a png
      png(outfile, width = 400, height = 400)
      get_iris_base_plot(spec = input$species_chosen)
      dev.off()

      # Return a list
      list(src = outfile,
           alt = "This is alternate text") 
    }, deleteFile = TRUE)

    # Make iris highcharter graph ----
    output$iris_highchart <- renderUI({

      # Get the image
      interactive_graph <- get_iris_highchart(spec = isolate(input$species_chosen))

      return(interactive_graph)
    })

    shinyjs::show("iris_chartbox")
  })
}

# Function to make base plot graph ----
get_iris_base_plot <- function(spec) {
  req <- GET(URLencode(paste0("http://127.0.0.1:8000/plot?spec=", spec)))

  # Parse the request
  img_content <- httr::content(req, type = "image/png")

  # Visualise
  grid.raster(img_content) 
}

# Function to make highchart graph ----
get_iris_highchart <- function(spec) {
  my_req <- GET(URLencode(paste0("http://127.0.0.1:8000/highchart?spec=", spec)))

  # Parse the request
  req_content <- httr::content(my_req, type = "text/html; charset=utf-8")

  # Visualise
  req_content
}

shinyApp (ui, server)
tags$html(HTML('<object data="<LINK TO YOUR WIDGET HERE>" width="100%" height="500px" type="text/html"> </object>')