inst/example_app/www/Helper_functions.R

# Funktionen für die News Applikation -------------------------------------

#Titel aus der Liste ziehen

# mehrere mit api key
testdaten <- lapply(c('google-news','spiegel-online','bild','abc-news-au',
                      'al-jazeera-english', 'ars-technica','der-tagesspiegel',
                      'die-zeit', 'focus', 'gruenderszene','handelsblatt',
                      't3n','wired-de','wirtschafts-woche'), get_news, apikey = Sys.getenv("NEWSAPIKEY"))

#testdaten # Liste der Datensätze

# TEST
# #titel
# testdaten21 <- testdaten[[1]]$articles[2]
# testdaten22 <- testdaten[[2]]$articles[2]
# testdaten23 <- testdaten[[3]]$articles[2]
# 
# #quelle
# n <- dim(testdaten[[1]]$articles)[1]
# quelle21 <- testdaten[[1]]$source
# quelle21 <- as.data.frame(rep(quelle21,n)) #überschreiben
# names(quelle21) <- "source"
# 
# n <- dim(testdaten[[2]]$articles)[1]
# quelle22 <- testdaten[[2]]$source
# quelle22 <- as.data.frame(rep(quelle22,n)) #überschreiben
# names(quelle22) <- "source"
# 
# n <- dim(testdaten[[3]]$articles)[1]
# quelle23 <- testdaten[[3]]$source
# quelle23 <- as.data.frame(rep(quelle23,n)) #überschreiben
# names(quelle23) <- "source"
# 
# prep_dta <- as.data.frame(rbind(testdaten21,testdaten22,testdaten23))
# prep_dta$source <- rbind(quelle21,quelle22,quelle23)
# # läuft! --> funktion schreiben

dta_prep <- function(dataset){
  # initialise empty datasets
  title_dta <- data.frame(title=character(), 
                   stringsAsFactors=FALSE)
  sources_dta <- data.frame(source=character(), 
                          stringsAsFactors=FALSE) 
  
  for(i in 1:length(dataset)){
    #get all titles
    titles <- as.data.frame(dataset[[i]]$articles[2])
    title_dta <- rbind(title_dta,titles) # append data
    #get the sources
    n <- dim(dataset[[i]]$articles)[1]
    source_string <- dataset[[i]]$source
    sources <- as.data.frame(rep(source_string,n)) #überschreiben
    names(sources) <- "source"
    sources_dta <- rbind(sources_dta,sources) #append data
  }
  prep_dta <- title_dta
  prep_dta$sources <- sources_dta
  return(prep_dta)
}

# test_daten_satz <- dta_prep(testdaten)


# Eugens Idee, klappt nicht

# test_daten_satz2 <- do.call(rbind, testdaten)

# Eugen 2

tt<- testdaten %>% purrr::map(~ data.frame(.)) %>% do.call(plyr::rbind.fill,.)
data-atelier/newsExploreR documentation built on May 22, 2019, 11:51 p.m.