There seems no option to prevent html_table() from converting integer-ish texts to integers. For example, consider the case below. I want to parse 001 and 002 as "001" and "002", but actually they are interpreted integer 1 and 2. I think html_table() should provide some option to control this behavior.
It will probably be straight to implement to wrap the type.covert() line with if. If this sounds good, I'm happy to contribute a pull request.
library(rvest)
library(tibble)
d <- tibble(code = c("001", "002", "101", "102"),
label = c("apple", "banana", "lemon", "orange"))
table_html <- knitr::kable(d, format = "html")
table_html <- as.character(table_html)
cat(table_html)
#> <table>
#> <thead>
#> <tr>
#> <th style="text-align:left;"> code </th>
#> <th style="text-align:left;"> label </th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style="text-align:left;"> 001 </td>
#> <td style="text-align:left;"> apple </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> 002 </td>
#> <td style="text-align:left;"> banana </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> 101 </td>
#> <td style="text-align:left;"> lemon </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> 102 </td>
#> <td style="text-align:left;"> orange </td>
#> </tr>
#> </tbody>
#> </table>
table_html %>%
read_html() %>%
html_table()
#> [[1]]
#> # A tibble: 4 x 2
#> code label
#> <int> <chr>
#> 1 1 apple
#> 2 2 banana
#> 3 101 lemon
#> 4 102 orange
Created on 2021-02-05 by the reprex package (v1.0.0)
|
utils::type.convert(out[, i], as.is = TRUE, dec = dec, na.strings = na.strings) |
There seems no option to prevent
html_table()from converting integer-ish texts to integers. For example, consider the case below. I want to parse001and002as"001"and"002", but actually they are interpreted integer1and2. I thinkhtml_table()should provide some option to control this behavior.It will probably be straight to implement to wrap the
type.covert()line withif. If this sounds good, I'm happy to contribute a pull request.Created on 2021-02-05 by the reprex package (v1.0.0)
rvest/R/table.R
Line 135 in 62cef0d