sobach

Practice_4

Nov 6th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.18 KB | None | 0 0
  1. ---
  2. output:
  3.   pdf_document
  4. ---
  5.  
  6. # This is our first autogenerated report
  7.  
  8. > Here we collect data from Kimono Labs API and produce autogenerated minireport.
  9.  
  10. Here you can see activity on publishing news on [RIA Lenta](http://ria.ru/lenta).
  11.  
  12. ```{r, include=FALSE}
  13. api.name <- '1xl232p8'
  14. api.key <- 'DTndIua4tfpz5kxITOILI6FvmGV8xw9g'
  15. Sys.setlocale(category = "LC_ALL", locale = "C")
  16. library(RCurl)
  17. library(rjson)
  18. library(ggplot2)
  19. library(RColorBrewer)
  20. ```
  21.  
  22. ```{r, echo=FALSE}
  23. json.url <- paste0('https://www.kimonolabs.com/api/', api.name, '?apikey=', api.key)
  24. json.file <- getURL(json.url, .encoding = 'UTF-8',
  25.                     .opts = list(ssl.verifypeer = FALSE))
  26. json.data <- fromJSON(json.file)
  27. df <- data.frame()
  28. for(i in 1:json.data$version){
  29.   csv.url <- paste0('https://www.kimonolabs.com/api/csv/', i, '/', api.name, '?apikey=', api.key)
  30.   csv.file <- getURL(csv.url, .encoding = 'UTF-8', .opts = list(ssl.verifypeer = FALSE))
  31.   csv.rows <- read.csv(textConnection(csv.file), skip=1)
  32.   df <- rbind(df, csv.rows)
  33. }
  34. df <- unique(df)
  35. df$timestamp <- strptime(as.character(df$timestamp), format = '%H:%M %d.%m.%Y')
  36. df$hour <- df$timestamp$hour
  37. df$day <- strftime(df$timestamp, format='%d %b')
  38. df <- aggregate(df, by = list(df$day, df$hour), FUN = length)
  39. df <- df[, 1:3]
  40. names(df) <- c('Date', 'Hour', 'Amount')
  41. specPal <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
  42. plot <- ggplot(df, aes(x = Hour, y = Date, fill = Amount))
  43. plot <- plot + geom_tile()
  44. plot <- plot + scale_fill_gradientn(guide="colourbar", colours = specPal(100), guide_legend(title="Activity"))
  45. plot <- plot + scale_x_discrete("Hour", limits=0:22, labels=0:23)
  46. plot <- plot + scale_y_discrete("Day")
  47. plot <- plot + coord_equal()
  48. plot <- plot + theme_bw()
  49. plot <- plot + theme(legend.position="bottom", legend.background = element_rect(colour = "grey"))
  50. plot <- plot + ggtitle(expression("Publication frequency"))
  51. print(plot)
  52. ```
  53.  
  54. ## Other abilities
  55.  
  56. ### Use lists:
  57.  
  58. * Item 1
  59. * Item 2
  60. * etc.
  61.  
  62. ### Or multiple headers:
  63.  
  64. #### Level-4 header
  65.  
  66. ### Math is also possible:
  67.  
  68. $EngagementRate = 100 \times \frac{likes + shares + comments}{Followers} (\%)$
  69.  
  70. ## Conclusion
  71.  
  72. Use R and have fun.
Add Comment
Please, Sign In to add comment