R rvest connect with local host

600 views Asked by At

I am creating a way to read in SPSS labels into R. Using library(sjPlot), view_spss(df, useViewer = FALSE) I can create a local html page such as http://localhost:11773/session/file1e0c67270a5.html that shows a nice table with columns for the variable names and the labels I am looking for.

Now I want to use rvest to scrape it but when I start with a command such as page <- rvest::html("http://localhost:11773/session/file1e0c67270a5.html") R just seems to get stuck.

I've tried searching for "connect with local host" but I can't seem to find any questions or answers related to the R package.

2

There are 2 answers

0
Forrest R. Stevens On BEST ANSWER

This doesn't really answer your specific question as I think the reason is that R spins up a non-persistent process to serve that HTML view of your data. But your approach seems quite round-a-bout to just get to variable labels. This is a general way that works quite well:

library(foreign)
d <- read.spss("your_data.sav", use.value.labels=TRUE, to.data.frame=FALSE)
var_labels <- attr(d, "variable.labels")

##  To access the label of a variable named 'var_name':
var_labels[["var_name"]]

Where d results in a list of data, and var_labels is a named list of labels keyed by variable/column.

0
Daniel On

If you want to get variable and/or value label of SPSS-imported data, you can use get_val_labels and get_var_labels of the sjmisc-package.

See examples here. Both functions accept either a single variable (vector) or a data frame and return the associated variable and value labels. See also this blog post.

The sjmisc-Package supports data frames imported both with the haven- or foreign-package.