Using the package rtweet, I have streamed some tweets and saved them in a JSON file.
When using the following: tweets_df <- parse_stream('file.json'), I get the following error during the process:
Does anyone have any idea how to fix this so that the JSON file can be read into R as a data frame?
Have you tried it this way? I don't personally use rtweet but work with json files.
#load library to read json
library(jsonlite)
json_data <- fromJSON("db.json")
It reads it as a nested list but then you can simply change it to a dataframe using
df<-rlist::list.stack(x, fill=TRUE )'
You might have to adapt it and for example use a loop if your json file contains several users.
Related
I'm new to json files in R. I got some json files for STONKS are you can see scraped online. The werid thing is each individual json file can be read perfectly using fromJSON from the jsonlite library. I have taken individual files to check on jsonlint.com to validate, and the web confirmed they are valid json files.
I have tried to use the validate () from jsonlite on single files, but there the file failed to pass the validation.
However, when I want to use lapply to read all the files R gave me error on
Error in parse_con(txt, bigint_as_char) :
lexical error: invalid char in json text.
No data available for the given
Here are my simple codes:
library(tidyr)
library(jsonlite)
ls <- list.files(pattern = "*.json")
Data.fromJson <- lapply(ls, fromJSON)
Sorry but I think the problem might be with the data so I dont want to just trim it and make dummy as it might mess things up. Instead I have uploaded a few json files to my google drive and here is the link:
https://drive.google.com/drive/folders/1zM4vj1TIseFKBSiNWe5yMY9BJPg-CIsv?usp=sharing
I'm looking to export a dataframe as a dbf file in R, and am using write.dbf but I get an error that says:
Error in write.dbf(mydata, "mydata.dbf") :
could not find function "write.dbf"
I've tried several variations of the filename portion for write.dbf (i.e no quotations, no extension, with the path included) but the all say the function can't be found.
Why is this the case?
Try this:
install.packages("foreign")
library(foreign)
write.dbf(mydata, "mydata.dbf")
As commented, you should first load the package containing this function. if you don't remember which package the function is from, you might retrieve it using ??write.dbf
I have converted a json data from a .json file to an R object with using fromJSON() of jsonlite library like this:
library(jsonlite)
jsonR<-fromJSON(txt="data.json")
If I explore the stringed values of the jsonR object I meet some strange sequences of chars.
For example if a string value of the original "data.json" was 😩 then R read it as \xf0\u009f\u0098©. And when I write this value back to file with cat() it becomes < f0>.
Can anyone suggest how one is supposed to do for keeping the correct original encoding while converting?
There must be something wrong with your requested URL. If that's not the problem, try it with the following packages:
library(RCurl) or library(RJSONIO)
I tried opening the file in excel and it is being displayed in proper format. Now how do i read it in R? I tried using read.csv function. It takes all the columns together without any separator.
You cannot directly load it to dataframe, first you have to load it as xml and then you can process it further.
Try following
require(XML)
data <- xmlParse('sample.dxl')
xml_data <- xmlToList(data)
You this list further to make your dataframe.
I have no internet connection and I just want to make a program using the library twitteR of R. For that purpose I have downloaded the file rdmTweets.RData that it is supposed to hold a collection of twitters. That file is available in: http://www.rdatamining.com/data
I have try to read that file using:
rdmTweets<-userTimeline("rdmTweets.RData",n=200)
also converting directly to a data frame:
df<-do.call("rbind",lapply("rdmTweets.RData",as.data.frame)
but, with no results at all. I mean it does not show any information of the twitters. I tried to read it like a file with:
rdm<-file("rdmTweets.RData","r")
lines<-readLines(rdm)
also with no results. It seems the only way that I can access that file is when I have:
rdmTweets<-userTimeline("rdatamining",n=200)
but that means to have an active internet connection. So the question that I have is how I can read that file in a way that I can get its contents like if I use userTimeline?
Thanks
To read a RData file, you need to use load().
Run the code below. The 1st line loads a data object rdmTweets from file, and the 2nd converts it into a data frame. You also need to have package twitteR installed.
load("rdmTweets.RData")
df <- do.call("rbind", lapply(rdmTweets, as.data.frame))