1

Using stackr it is possible, with the following command, to list the questions from a specific user.

library(devtools)
devtools::install_github("dgrtwo/stackr")
library(stackr)
textques <- stack_users(712603, "questions", num_pages=10, pagesize=100)

Is it possible to have also the text of every question?

There is a body option but how could I use it with the previous command?

Also, I tried body equals true but it is not working:

textques <- stack_users(712603, "questions", body = "true", num_pages=10, pagesize=100)
miken32
  • 42,008
  • 16
  • 111
  • 154
Pozmanski
  • 181
  • 11

2 Answers2

2

Since the stack_users() and stack_GET() function both accept (and pass on) the ellipsis argument, you can add any parameter onto the end of the API call URL. This includes the filter parameter, where we can use the built-in withbody filter:

library(stackr)
questions <- stack_users(9371451, "questions", num_pages=10, pagesize=100, filter="withbody")
for (i in 1:nrow(questions)) {
    qtext <- questions$body[i]
    print(qtext)
}

Output:

[1] "<p>I would like to use the Stack Exchange API with a specific user id to get the text of a user's badges.</p>\n\n<p>I found <a href=\"https://github.com/dgrtwo/stackr\" rel=\"nofollow noreferrer\">the <em>stackr</em> library for the Stack Exchange API</a>, and tried this:</p>\n\n<pre><code># install.packages(\"devtools\")\nlibrary(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\nstack_users(712603)\n</code></pre>\n\n<p>But that only gives the total number of every kind of badge. How can I take the text from each one?\nExample:</p>\n\n<pre><code>gold silver bronze\nr     r      ggplot2\n</code></pre>\n\n<p>I don't only want the total number of badges but also what the badges are.</p>\n"
[1] "<p>Using stackr it is possible with the following command to take the question from a specific user.</p>\n\n<pre><code>library(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\ntextques &lt;- stack_users(712603, \"questions\", num_pages=10, pagesize=100)\n</code></pre>\n\n<p>How is it possible to have also the text of every question?</p>\n\n<p><a href=\"https://api.stackexchange.com/docs/types/question\">There is</a> a body option but how could I use it with the previous command</p>\n\n<p>Also I tried body equals true but it is not working:</p>\n\n<pre><code>textques &lt;- stack_users(712603, \"questions\", body = \"true\", num_pages=10, pagesize=100)\n</code></pre>\n"
miken32
  • 42,008
  • 16
  • 111
  • 154
0

I am not sure if the package provides a function to extract the question asked, but here's an approach based on rvest

for(i in 1:nrow(textques)){
  link <- textques$link[i]
  textques$qtext[i] <- read_html(link) %>% html_node("#question .post-text") %>% html_text()
}

This should add another variable to your existing dataframe with the text of the question in each link.

Dhiraj
  • 1,650
  • 1
  • 18
  • 44
  • Thank you it is a nice solution but please let me search a little more if is possible to make it through api because as I can understand rvest is making directly calls and let's say it makes more traffic to site and if I use it for a list of many users there is a possibility to receive an error. – Pozmanski Feb 17 '18 at 14:18