JSON rmarkdown 中的树
JSON tree in rmarkdown plotly
我想在我的 rmarkdown 结果中包含一棵 JSON 树。这是一个可重现的例子:
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() + geom_smooth()
plotly_json(p)
调用plotly_json(p)
创建的对象为"jsonedit" "htmlwidget"
.
在编织之前使用这段代码时,结果显示在 Viewer 中。这是我希望在 html 文件中显示结果的方式。
但是,将文档编织到 html 我得到了相同的结果,但在 text form 中。
从plotly_json的源代码看,好像需要手动设置jsonedit
为true,并安装listviewer
包。
jsonedit 的默认值是 interactive():
Return TRUE when R is being used interactively and FALSE otherwise.
这就解释了为什么当您直接执行代码时会显示小部件,但在编织 rmd 文件时不会显示。
试试这个:
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
# install required listviewer pkg if necessary
#install.packages("listviewer")
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() + geom_smooth()
# use jsonedit from listviewer pkg
plotly_json(p, jsonedit = TRUE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
由 reprex package (v0.2.0.9000) 创建于 2018-07-19。
我想在我的 rmarkdown 结果中包含一棵 JSON 树。这是一个可重现的例子:
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() + geom_smooth()
plotly_json(p)
调用plotly_json(p)
创建的对象为"jsonedit" "htmlwidget"
.
在编织之前使用这段代码时,结果显示在 Viewer 中。这是我希望在 html 文件中显示结果的方式。
但是,将文档编织到 html 我得到了相同的结果,但在 text form 中。
从plotly_json的源代码看,好像需要手动设置jsonedit
为true,并安装listviewer
包。
jsonedit 的默认值是 interactive():
Return TRUE when R is being used interactively and FALSE otherwise.
这就解释了为什么当您直接执行代码时会显示小部件,但在编织 rmd 文件时不会显示。
试试这个:
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
# install required listviewer pkg if necessary
#install.packages("listviewer")
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() + geom_smooth()
# use jsonedit from listviewer pkg
plotly_json(p, jsonedit = TRUE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
由 reprex package (v0.2.0.9000) 创建于 2018-07-19。