Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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
如何使用.R源文件中的knitr render在Lappy循环中打印HTML交互式绘图图?_R_Knitr_R Markdown_Plotly_Htmlwidgets - Fatal编程技术网

如何使用.R源文件中的knitr render在Lappy循环中打印HTML交互式绘图图?

如何使用.R源文件中的knitr render在Lappy循环中打印HTML交互式绘图图?,r,knitr,r-markdown,plotly,htmlwidgets,R,Knitr,R Markdown,Plotly,Htmlwidgets,在Rstudio中,我尝试使用.R源文件中的knitr render动态地将plotly打印渲染到HTML报告中。knitr渲染plotly打印,只要它们不在lappy循环内。但一旦我尝试从lappy循环中输出它们,它们就会被注释渲染。我需要使用一个循环,因为我需要以编程的方式用不同数量的图和不同的名称构建我的HTML报告 以下是我在控制台中执行的命令,用于从我的plotlytest.R源文件创建HTML报告: render(“plotlytest.R”) 下面是我的示例源文件,名为plotly

在Rstudio中,我尝试使用.R源文件中的knitr render动态地将plotly打印渲染到HTML报告中。knitr渲染plotly打印,只要它们不在lappy循环内。但一旦我尝试从lappy循环中输出它们,它们就会被注释渲染。我需要使用一个循环,因为我需要以编程的方式用不同数量的图和不同的名称构建我的HTML报告

以下是我在控制台中执行的命令,用于从我的
plotlytest.R
源文件创建HTML报告:

render(“plotlytest.R”)

下面是我的示例源文件,名为
plotlytest.R

#'---
#'author: "pokyah"
#'output: 
#'  html_document:
#'    theme: flatly
#'    toc: true
#'    toc_depth: 6
#'    toc_float:
#'      collapsed: false
#'      smooth_scroll: true
#'title: "Plotly test"
#'date: \`r format(Sys.Date(), " %d-%m-%Y")`\
#'---
#'
#

#+ ---------------------------------
#' ## Loading libraries
library(plotly)
library(ggplot2)

#+ ---------------------------------
#' ## plotly example
#+ plotly,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE,results='asis', plotly=TRUE

mtcars.p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() +
  labs(colour = "Cylinders")


iris.p <- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) + geom_point() +
  labs(colour = "Cylinders")

myPlots.l <- list(mtcars.p, iris.p)
names(myPlots.l) <- c("mtcars", "iris")

printPlots <- function(p, plots){
  cat("\n")
  cat("###", names(plots[p]))
  cat("\n")
  ggplotly(plots[[p]]) # this is not rendered
  cat("\n")
  print (plots[[p]]) # this is rendered, but of course this is not an interactive plot
  cat("\n")
}

cat("\n")
cat("## printing out of lapply -> plotly working")

cat("\n")
ggplotly(myPlots.l$mtcars) # this is rendered

cat("\n")
ggplotly(myPlots.l$iris) # this is also rendered
cat("\n")

cat("\n")
cat("## printing inside of lapply -> plotly not working")
cat("\n")
lapply(seq_along(myPlots.l), printPlots, myPlots.l )

我认为在
knitr
中没有函数
render
。但是,如果您想生成一个混合了HTML和标准R绘图的文档,可以使用下面的RMarkdown文档。然后,您可以
knit
这将产生所需的输出

另外,关于为什么不显示情节图?您需要对它们调用
print
,以便它们显示,因为您正在函数中以绘图方式调用
gg

---
author: "pokyah"
output: 
  html_document:
    theme: flatly
    toc: true
    toc_depth: 6
    toc_float:
      collapsed: false
      smooth_scroll: true
title: "Plotly test"
date: \`r format(Sys.Date(), " %d-%m-%Y")`\
---


# ---------------------------------
#' ## Loading libraries


#+ ---------------------------------
#' ## plotly example

```{r}
library(plotly)
library(ggplot2)
mtcars.p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() +
  labs(colour = "Cylinders")


iris.p <- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) + geom_point() +
  labs(colour = "Cylinders")

myPlots.l <- list(mtcars.p, iris.p)
names(myPlots.l) <- c("mtcars", "iris")

printPlots <- function(p, plots){
  cat("\n")
  cat("###", names(plots[p]))
  cat("\n")
  print(ggplotly(plots[[p]])) # this is not rendered
  cat("\n")
  print (plots[[p]]) # this is rendered, but of course this is not an interactive plot
  cat("\n")
}

cat("\n")
cat("## printing out of lapply -> plotly working")

cat("\n")
ggplotly(myPlots.l$mtcars) # this is rendered

cat("\n")
ggplotly(myPlots.l$iris) # this is also rendered
cat("\n")

cat("\n")
cat("## printing inside of lapply -> plotly not working")
cat("\n")
lapply(seq_along(myPlots.l), printPlots, myPlots.l )
```
---
作者:“pokyah”
输出:
html_文件:
主题:平淡
toc:没错
toc_深度:6
toc_浮动:
失败:错误
平滑滚动:真
标题:“阴谋测试”
日期:\`r格式(Sys.date(),%d-%m-%Y)`\
---
# ---------------------------------
#“##加载库
#+ ---------------------------------
#"##生动的例子
```{r}
图书馆(绘本)
图书馆(GG2)

mtcars.p这里有一个解决方案。您需要在“ggplotly”对象(它是一个
htmlwidget
对象)上调用
knit\u print()

htmlwidgets
需要附加。
我唯一无法解决的问题是
ggplotly
width/height:我必须明确这些值

#'---
#'author: "pokyah"
#'output: 
#'  html_document:
#'    theme: flatly
#'    toc: true
#'    toc_depth: 6
#'    toc_float:
#'      collapsed: false
#'      smooth_scroll: true
#'title: "Plotly test"
#'date: \`r format(Sys.Date(), " %d-%m-%Y")`\
#'---
#'
#

#+ ---------------------------------
#' ## Loading libraries
library(plotly)
library(ggplot2)
library(htmlwidgets)
library(knitr)

#+ ---------------------------------
#' ## plotly example
#+ plotly,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE,results='asis', plotly=TRUE

mtcars.p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() +
  labs(colour = "Cylinders")


iris.p <- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) + geom_point() +
  labs(colour = "Cylinders")

myPlots.l <- list(mtcars.p, iris.p)
names(myPlots.l) <- c("mtcars", "iris")

printPlots <- function(p, plots){
  cat("\n")
  cat("###", names(plots[p]))
  cat("\n")
  #ggplotly(plots[[p]]) # this is not rendered. Of course, no "print" method is called.
                       # This line only returns an htmlwidget object that is lost because inside a function. 
  cat(knit_print(ggplotly(plots[[p]], width = 672, height = 480))) # This works.
  cat("\n")
  print(plots[[p]]) # this is rendered, but of course this is not an interactive plot
  cat("\n")
}

cat("\n")
cat("## printing out of lapply -> plotly working")

cat("\n")
ggplotly(myPlots.l$mtcars) # this is rendered

cat("\n")
ggplotly(myPlots.l$iris) # this is also rendered
cat("\n")

cat("\n")
cat("## printing inside of lapply -> plotly not working")
cat("\n")
lapply(seq_along(myPlots.l), printPlots, myPlots.l)
#'---
#'作者:"薄伽"
#“产出:
#'html_文档:
#主题:直截了当
#“toc:是的
#“toc_深度:6”
#“toc_浮动:
#"假",
#'平滑滚动:正确
#标题:“阴谋测试”
#'date:\`r格式(Sys.date(),%d-%m-%Y)`\
#'---
#'
#
#+ ---------------------------------
#“##加载库
图书馆(绘本)
图书馆(GG2)
库(htmlwidgets)
图书馆(knitr)
#+ ---------------------------------
#"##生动的例子
#+plotly,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE,results=asis',plotly=TRUE

mtcars.p恐怕您无法在
lappy()
中一步生成一个HTML小部件(plotly)和一个相邻的R图。您可能实现的是在一个步骤中生成所有HTML小部件,然后在另一个步骤中生成所有R图。要实现这一点,请参见,完全可以使用任何方法在同一文档中包含HTML和R图。您只需确保打印是显式打印的,而不仅仅是返回
ggplotly
不显式打印输出。
#'---
#'author: "pokyah"
#'output: 
#'  html_document:
#'    theme: flatly
#'    toc: true
#'    toc_depth: 6
#'    toc_float:
#'      collapsed: false
#'      smooth_scroll: true
#'title: "Plotly test"
#'date: \`r format(Sys.Date(), " %d-%m-%Y")`\
#'---
#'
#

#+ ---------------------------------
#' ## Loading libraries
library(plotly)
library(ggplot2)
library(htmlwidgets)
library(knitr)

#+ ---------------------------------
#' ## plotly example
#+ plotly,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE,results='asis', plotly=TRUE

mtcars.p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() +
  labs(colour = "Cylinders")


iris.p <- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) + geom_point() +
  labs(colour = "Cylinders")

myPlots.l <- list(mtcars.p, iris.p)
names(myPlots.l) <- c("mtcars", "iris")

printPlots <- function(p, plots){
  cat("\n")
  cat("###", names(plots[p]))
  cat("\n")
  #ggplotly(plots[[p]]) # this is not rendered. Of course, no "print" method is called.
                       # This line only returns an htmlwidget object that is lost because inside a function. 
  cat(knit_print(ggplotly(plots[[p]], width = 672, height = 480))) # This works.
  cat("\n")
  print(plots[[p]]) # this is rendered, but of course this is not an interactive plot
  cat("\n")
}

cat("\n")
cat("## printing out of lapply -> plotly working")

cat("\n")
ggplotly(myPlots.l$mtcars) # this is rendered

cat("\n")
ggplotly(myPlots.l$iris) # this is also rendered
cat("\n")

cat("\n")
cat("## printing inside of lapply -> plotly not working")
cat("\n")
lapply(seq_along(myPlots.l), printPlots, myPlots.l)