goodpress icon indicating copy to clipboard operation
goodpress copied to clipboard

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

Open HugoGit39 opened this issue 3 years ago • 11 comments

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

  1. +-goodpress::wp_post(path_post, wordpress_url)
  2. | -goodpress:::.wp_media_post(...)
  3. | -purrr::map_chr(...)
  4. -purrr:::stop_bad_element_vector(...)
  5. -purrr:::stop_bad_vector(...)
  6. \-purrr:::stop_bad_type(...)
    

HugoGit39 avatar Apr 24 '21 19:04 HugoGit39

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

Normal R-script:

`

  • 1 preparation -----------------------------------------

  • 1.1 clear & set environement ------------------------------------- rm(list = ls(all.names = TRUE))

  • 1.1 set wd of WP post folder ------------------------------------- setwd("C:/Users/Me/Documents/WordPress/UploadWP")

  • 1.2 Update packages ----------------------------------------------- update.packages(.libPaths()[1], ask = FALSE, Ncpus = 4)

  • 2 Render Rmarkdown --------------------------------------------- render("index.Rmd")

  • 3 Upload post WP -----------------------------------------------

    wordpress_url <- "https://myWPwebsite.nl"

    path_post <- "C:/Users/Me/Documents/WordPress/UploadWP"

    wp_post(path_post, wordpress_url) `

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


  • 1 Include html file----------------------------------------------- knitr::include_graphics(file.path("figs", "test.html"))
`



HugoGit39 avatar Apr 25 '21 07:04 HugoGit39

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 avatar Apr 25 '21 08:04 HugoGit39

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

HugoGit39 avatar Apr 25 '21 15:04 HugoGit39

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?

henrywangnl avatar Apr 26 '21 10:04 henrywangnl

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

maelle avatar Apr 26 '21 12:04 maelle

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

HugoGit39 avatar Apr 26 '21 12:04 HugoGit39

yes the img tag

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

maelle avatar Apr 26 '21 14:04 maelle

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

HugoGit39 avatar Apr 26 '21 15:04 HugoGit39

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!

maelle avatar Apr 26 '21 16:04 maelle

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

henrywangnl avatar Apr 27 '21 21:04 henrywangnl

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
  )
}

HugoGit39 avatar May 05 '21 18:05 HugoGit39