baranovskypd / goodpress

WIP!! :warning: R Package to Write Posts from R Markdown to Wordpress :warning:
https://maelle.github.io/goodpress/
Other
34 stars 5 forks source link

Error: Result 1 must be a single string, not NULL of length 0 #31

Open HugoGit39 opened 3 years ago

HugoGit39 commented 3 years ago

Hi Im running at the following issue constantly wile producing a post with a html plot made via plotly

Even if I have a simple Rmarkdown file with just 1 plotly html plt attached via knitr::include_graphics(file.path("figs", "test.html"))

Error:

wp_post(path_post, wordpress_url) Error: Result 1 must be a single string, not NULL of length 0 Run rlang::last_error() to see where the error occurred. rlang::last_error() <error/purrr_error_bad_element_vector> Result 1 must be a single string, not NULL of length 0 Backtrace:

  1. goodpress::wp_post(path_post, wordpress_url)
  2. purrr:::stop_bad_element_vector(...)
  3. purrr:::stop_bad_vector(...)
  4. purrr:::stop_bad_type(...) Run rlang::last_trace() to see the full context. rlang::last_trace() <error/purrr_error_bad_element_vector> Result 1 must be a single string, not NULL of length 0 Backtrace: x
  5. +-goodpress::wp_post(path_post, wordpress_url)
  6. | -goodpress:::.wp_media_post(...)
  7. | -purrr::map_chr(...)
  8. -purrr:::stop_bad_element_vector(...)
  9. -purrr:::stop_bad_vector(...)
  10. -purrr:::stop_bad_type(...)
HugoGit39 commented 3 years ago

Here is my code (I call the index.Rmd file via render in a normal R script):

Normal R-script:

`

index.Rmd script:

`--- title: "Test" date: "2020-06-25T00:00:00" output: hugodown::md_document status: "draft" slug: "plots-post-draft" categories: testpost tags: test


HugoGit39 commented 3 years ago

Btw the plotly plot is compiled to the /figs folder in html via:

library(plotly) library(htmlwidgets)

fig <- plot_ly( x = c("giraffes", "orangutans", "monkeys"), y = c(20, 14, 23), name = "SF Zoo", type = "bar" )

withr::with_dir('C:/Users/Me/Documents/WordPress/UploadWP/figs', saveWidget(fig, file="test.html"))

HugoGit39 commented 3 years ago

Btw I did see the following when I previewed the post which is different between when a .png vs an .html is added:

When right click on the .png file and opened in a new window, the .png image loaction is at:

https://www.myWPwebsite.nl/wp-content/uploads/2021/04/test.png....which is correct

But the .html is at (which is btw not visisble in the post but it there appears as an "unknown image icon":

https://www.myWPwebsite.nl/fig/test.html...so this is clearly a wrong location

However the real test.html btw is uploaded to the media library at https://www.myWPwebsite.nl/wp-content/uploads/2021/04/test.html

henrywangnl commented 3 years ago

Btw I did see the following when I previewed the post which is different between when a .png vs an .html is added:

When right click on the .png file and opened in a new window, the .png image loaction is at:

https://www.myWPwebsite.nl/wp-content/uploads/2021/04/test.png....which is correct

But the .html is at (which is btw not visisble in the post but it there appears as an "unknown image icon":

https://www.myWPwebsite.nl/fig/test.html...so this is clearly a wrong location

However the real test.html btw is uploaded to the media library at https://www.myWPwebsite.nl/wp-content/uploads/2021/04/test.html

Thanks @HugoGit39!

I didn't manage to look into it yet but I guess it's because of the logic here: https://github.com/maelle/goodpress/blob/main/R/post.R#L155

In short, I suppose @maelle only wanted to upload static images not interactive ones like generated from plotly.

@HugoGit39 do you want to improve the logic and make a PR?

maelle commented 3 years ago

yes I didn't have images that are not in an image tag in mind, indeed. A PR would be interesting @HugoGit39

HugoGit39 commented 3 years ago

@maelle so you mean only media files which wordpress recognize as images (which are .jpg .jpeg .png .gif) work in the current goodpress package?

maelle commented 3 years ago

yes the img tag

https://github.com/maelle/goodpress/blob/76b675d7f0f7e714a5ed17ce1e70e09c775fe5ff/R/post.R#L157

HugoGit39 commented 3 years ago

Honestly Im a bit fed up with the combination of R and WordPress....moreover your code @maelle is pretty difficult for me to comprehend so I will possibly try but probably not unfortunatly

maelle commented 3 years ago

I understand! It's an unfortunate situation as I do not have the bandwitdth to work on goodpress right now. I hope you can find a workaround, good luck!

henrywangnl commented 3 years ago

this seems to be the right direction: https://bhaskarvk.github.io/widgetframe/

HugoGit39 commented 3 years ago

I might have found a solution though Im stuck at 1 thing:

If I add the plotly plot in Rmarkdown its embedded in the html output perfectly....so in my view, if I add this html file output in the content-variable 'content', it should upload it as a post (right?)....and when I have the post id I can just re-run Rmarkdown so the html ouptut gets overwritten as a new, updated post

However how do I add the html output from Rmarkdown as the conten variable?

Ive dismantled the code a bit, see:

So first run Rmarkdown with a plotly plot:

` ` ` {r}
library(plotly)
` ` `
` ` ` {r}
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
p
` ` `

Than put in body tags:

date <- format(Sys.time(), '%Y-%m-%dT%H:%M:%S')

post_list <- list( 'date' =  date,
                   'title' = "testest",
                   'slug' = "testslug",
                   'comment_status' =  "closed",
                   'ping_status' =  "closed",
                   'status' = 'draft',
                   'content' = Rmarkdown_html_output (so what to fill in here??),
                   'excerpt' =  NULL,
                   'format' = 'standard',
                   'template' =  NULL,
                   'categories' = "test",
                   'tags' = "plotlyplot"

post <- jsonlite::toJSON(
  post_list,
  auto_unbox = TRUE, , force=T
)

Than call the .wp_post function with wp_call_api included with wordpress_url = www.yoursite.com en post_id = the id of the 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
  )
}