通过R在Wordpress中以绘图方式嵌入(即在Wordpress post的正文中定义内容和变量)
所以我的问题有两个方面:通过R在Wordpress中以绘图方式嵌入(即在Wordpress post的正文中定义内容和变量),r,wordpress,plotly,restapi,R,Wordpress,Plotly,Restapi,所以我的问题有两个方面: 如何将html交互式绘图一般添加到Wordpress帖子或网站中 如何通过R中的自动脚本将html交互式绘图添加到帖子中,这似乎可行(给出200个响应),但不知何故无法添加绘图 这是我从中使用的代码。仅供参考,要使用它,您首先需要安装,生成用户(您)的密码,并将此密码和用户名设置为环境变量,ar用于调用REST API发布 首先,我在RMarkdown中生成一个带有html输出的plotly plot,如下所示: ` ` ` {r} library(plotly)
` ` ` {r}
library(plotly)
` ` `
` ` ` {r}
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
p
` ` `
要发布的功能:
.wp_post <- function(post, post_id,
wordpress_url) {
api_url <- paste0(wordpress_url, "/wp-json/wp/v2/posts/", post_id)
wp_call_api(
VERB = "POST",
api_url = api_url,
body = post
)
}
.wp\u post
.wp_post(post, post_id,
wordpress_url)
.wp_post <- function(post, post_id,
wordpress_url) {
api_url <- paste0(wordpress_url, "/wp-json/wp/v2/posts/", post_id)
wp_call_api(
VERB = "POST",
api_url = api_url,
body = post
)
}
wp_call_api <- function(VERB, api_url, body = NULL,
filename = NULL) {
token <- paste("Basic",
jsonlite::base64_enc(
glue::glue(
'{Sys.getenv("WP_USER")}:{Sys.getenv("WP_PWD")}'
)
)
)
api_response <- httr::VERB(verb = VERB,
url = api_url,
httr::add_headers(
Authorization = token),
body = body)
httr::stop_for_status(api_response)
httr::content(api_response)
}