将rvest与drake一起使用:外部指针无效错误 [英] Using rvest with drake: external pointer is not valid error

查看:160
本文介绍了将rvest与drake一起使用:外部指针无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次运行下面的代码时,一切正常.但是,当我更改html_file %>%...指令中的某些内容(例如注释tolower())时,出现以下错误:

When I first run the code below, everything is ok. But when I change something in html_file %>%... comand, for example commenting tolower(), I get the following error:

Error: target title failed.
diagnose(title)error$message:
  external pointer is not valid
diagnose(title)error$calls:
   1. └─html_file %>% html_nodes("h2") %>% html_text()

代码:

library(rvest)
library(drake)

some_string <- '
  <div class="main">
      <h2>A</h2>
      <div class="route">X</div>
  </div> 
'

html_file <- read_html(some_string)
title <- html_file %>% 
  html_nodes("h2") %>% 
  html_text()

plan <- drake_plan(
  html_file = read_html(some_string),
  title = html_file %>% 
    html_nodes("h2") %>% 
    html_text() %>% 
    tolower()
)

make(plan)

我找到了两种可能的解决方案,但我对它们并不热心.
1.将drake_plan中的两个步骤合并为一个.
2.根据建议在此处使用xml2::write_html()xml2::read_html().
有更好的解决方法吗? P.S.问题已经在此处中讨论过>, github 上.

I found two possible solutions but I'm not enthusiastic about them.
1. Join both steps in drake_plan into one.
2. Use xml2::write_html() and xml2::read_html() as suggested here.
Is there a better way to solve it? P.S. Issue was already discussed here, Rstudio forum, and on github.

推荐答案

默认情况下,drake将目标另存为RDS文件(其他选项 https://github.com/tidyverse/rvest/issues/181#issuecomment您提出的-395064636 正是问题所在.我喜欢(1),因为文本与RDS兼容.概括地说,由用户选择与drake的数据存储系统兼容的良好目标.请参见 https://books.ropensci.org/drake/plans.html#how-to-choose-good-targets 进行讨论并链接到类似问题.但是,如果您想使用(2),则可以从动态文件.

By default, drake saves targets as RDS files (other options here). So https://github.com/tidyverse/rvest/issues/181#issuecomment-395064636, which you brought up, is exactly the problem. I like (1) because text is compatible with RDS. Speaking broadly, it is up to the user to choose good targets compatible with drake's data storage system. See https://books.ropensci.org/drake/plans.html#how-to-choose-good-targets for a discussion and links to similar issues. But you want to go with (2), you could return the file path to your HTML file from within a dynamic file.

这篇关于将rvest与drake一起使用:外部指针无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆