Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HugoGit39 opened this issue Apr 24, 2021 · 11 comments
Open

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

HugoGit39 opened this issue Apr 24, 2021 · 11 comments

Comments

@HugoGit39
Copy link

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
Copy link
Author

HugoGit39 commented Apr 25, 2021

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
Copy link
Author

HugoGit39 commented Apr 25, 2021

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
Copy link
Author

HugoGit39 commented Apr 25, 2021

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
Copy link
Contributor

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
Copy link
Collaborator

maelle commented Apr 26, 2021

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

@HugoGit39
Copy link
Author

HugoGit39 commented Apr 26, 2021

@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
Copy link
Collaborator

maelle commented Apr 26, 2021

@HugoGit39
Copy link
Author

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
Copy link
Collaborator

maelle commented Apr 26, 2021

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
Copy link
Contributor

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

@HugoGit39
Copy link
Author

HugoGit39 commented May 5, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants