【问题标题】:R HTML Output FileR HTML 输出文件
【发布时间】:2018-05-07 17:34:07
【问题描述】:

我正在使用 R 生成 HTML 报告输出,当我获得输出 html 文件并打开它时,我可以看到我使用 ggplot 创建的图表没问题,但是如果我将 html 文件发送给其他任何人,图表就会丢失并且它们只看文字。我的代码如下,任何关于我所缺少的建议都会很棒。

另外,我的本地驱动器中的图像未显示。

---
title: "title"
 ``author: 
- "X"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: 
  html_document: 
 number_sections: yes
toc: yes
runtime: shiny    
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,message=FALSE, warning=FALSE)
```

{r libraries, include=FALSE} library(plyr) library(dplyr) library(tidyverse) library(ggplot2) library(forcats) library(reshape) library(png)

```{r, fig.width=12, fig.height=6, fig.align="center"}
# read in data file
lj= read.csv("LJ_Final.csv")  # read csv file

#rename variables to fit charts
names(lj)[names(lj) == "2015"] <- "15"

myvars <- c("HB","15")
newdatajoiner <- lj[myvars]

newdatajoiners.long<-melt(newdatajoiner,id.vars="HB")

aggdatajoiners <-aggregate(value ~ HB + variable, data = 
newdatajoiners.long, sum)

#plot the data
aggdatajoiners%>%
ggplot(aes(x=variable,y=value,fill=factor(HB)))+
 geom_bar(stat="identity",position="dodge")+
 labs(x="", y="")+
 theme_bw()+
 facet_wrap(~HB)
```
```{r,fig.align="center",out.width = "3000px"}
knitr::include_graphics('./tablefife.png')
```

【问题讨论】:

    标签: html r shiny output r-markdown


    【解决方案1】:

    当你的 HTML 是从 Markdown 文件生成的时候,一个图片文件夹是用 HTML 生成的,你的图片被 HTML 引用,不包含在 html 文件本身中,你也需要发送这个文件夹。

    您可以将图像转换为 base64 字符串并嵌入到 html 文件中,但是我不知道如何在 R 中自动执行此操作

    【讨论】:

    • 这已在另一个线程上解决:stackoverflow.com/questions/14870589/…
    • 这是否意味着无法使用 markdown 在 HTML 输出中包含图像?
    • 这里有,如果你使用命令行你需要使用上面链接中的base64参数,如果你使用RStudio,点击Knit旁边的齿轮,输出选项,高级和选中“创建独立的 HTML 文档”,图像现在应该嵌入为 base64 url​​
    • 当我运行它时,图像会为我显示,但如果我共享 html 文件,则上面代码中的图表不会显示。即使我已经创建独立打勾
    【解决方案2】:

    @user3018495 另一个线程描述了如何排除图表。要在输出 HTML 文档中包含图表,请在 Rmd 输出选项中将 self_contained 设置为 yes 而不是 no

    ---
    title: "your title goes here"
    output:
       html_document:
       self_contained: yes
       toc: yes
       toc_float: yes
    ---
    

    如果您将 RStudio 与 knitr 一起使用,则在 RStudio 选项中进行配置。首先在代码编辑器窗口中选择齿轮。

    接下来,选择Advanced 标签,然后

    1. 点击Standalone HTML
    2. 然后按“确定”。

    当您编织文档时,生成的 HTML 文件将包含图表,如我在 Github 存储库中发布的 test HTML document 所示,以便可以通过 Github HTML 预览器查看。

    问候,

    镜头

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      相关资源
      最近更新 更多