【问题标题】:R: Error in as.vector(x, "character"): cannot coerce type 'externalptr' to vector of type 'character'R:as.vector(x,“character”)中的错误:无法将类型“externalptr”强制转换为“character”类型的向量
【发布时间】:2021-01-20 12:34:37
【问题描述】:

我正在使用 R 编程语言。我正在尝试将 HTML 文件和 JPG 图像文件组合在一起。

我的代码如下所示:

library(plotly)
library(shiny)
library(magick)

#create widget_1
widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)

#upload some jpg image from your computer into R
my_image = image_read("my_image.jpg")

doc <- htmltools::tagList(
  div(widget_1, style = "float:left;width:50%;"),
  div(my_image,style = "float:left;width:50%;")
  
)

htmltools::save_html(html = doc, file = "C://Users//Me//Desktop//combined_file.html")

但是,此代码会产生以下错误:

Error in as.vector(x, "character"): cannot coerce type 'externalptr' to vector of type 'character'

是否可以在 R 中同时保存 html 和 jpg 文件?或者这根本不可能? 有没有人尝试过这样做?

谢谢

【问题讨论】:

    标签: html r image widget imagemagick


    【解决方案1】:

    也许您可以将其放入 R Markdown 并将其编织为 HTML 以在一个 HTML 文件中获取输出。

    ---
    title: "temp"
    output: html_document
    ---
    
    
    ```{r, echo=FALSE, warning=FALSE, message=FALSE}
    library(plotly)
    library(shiny)
    library(magick)
    
    
    widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)
    
    #upload some jpg image from your computer into R
    my_image = image_read("try.png")
    ```
    
    ```{r, echo=FALSE, warning=FALSE, message=FALSE, fig.height=3}
    widget_1
    ```
    
    ```{r, echo=FALSE, warning=FALSE, message=FALSE, out.width = "600px"}
    my_image
    ```
    

    这会生成 HTML 文件:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-29
      • 2011-01-21
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多