projects/tweetawesome/appmultiplefiles/server.R

#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(someR)
library(shiny)
library(shinydashboard)
library(reactable)
library(reactablefmtr)

# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {

  ### GET DATA ###
  dat <- reactive({

    # get data
    con <- someR::con_sql()
    res <- dbSendQuery(con, "SELECT * FROM twitter_folketing_tl_stats")
    dat <- dbFetch(res, n = -1)
    dbClearResult(res)
    DBI::dbDisconnect(con)

    # need to join this in for later
    dat %>% dplyr::select(
      user,name,party
    ) %>% distinct(
      user,
      .keep_all = T
    ) -> dat_master

    # make table
    dat %>% dplyr::filter(
      variable %in% c(
        "activity_curweek",
        "tweets_curweek",
        "comments_curweek",
        "likes_curweek",
        "likes_mean_curweek",
        "activity_curmonth",
        "tweets_curmonth",
        "comments_curmonth",
        "likes_curmonth",
        "likes_mean_curmonth",
        "activity_curyear",
        "tweets_curyear",
        "comments_curyear",
        "likes_curyear",
        "likes_mean_curyear"
      )
    ) -> table_currentx

    table_currentx <- reshape2::dcast(
      table_currentx,
      "user ~ variable",
      value.var = "value",
      fun.aggregate = sum
    )

    ### CURRENT WEEK ###
    table_currentx %>% dplyr::select(
      user,
      activity_curweek,
      tweets_curweek,
      comments_curweek,
      likes_curweek,
      likes_mean_curweek
    ) %>% dplyr::arrange(
      desc(likes_curweek)
    ) -> table_curweek
    table_curweek[["likes_mean_curweek"]] <- round(table_curweek[["likes_mean_curweek"]],2)
    table_curweek[["placering"]] <- 1:nrow(table_curweek)

    table_curweek <- dplyr::left_join(
      table_curweek,dat_master, by = c("user"="user")
    )
    table_curweek %>% dplyr::select(
      name,
      party,
      placering,
      likes_curweek,
      likes_mean_curweek,
      activity_curweek,
      tweets_curweek,
      comments_curweek
    ) -> table_curweek

    ### CURRENT MONTH ###
    table_currentx %>% dplyr::select(
      user,
      activity_curmonth,
      tweets_curmonth,
      comments_curmonth,
      likes_curmonth,
      likes_mean_curmonth
    ) %>% dplyr::arrange(
      desc(likes_curmonth)
    ) -> table_curmonth
    table_curmonth[["likes_mean_curmonth"]] <- round(table_curmonth[["likes_mean_curmonth"]],2)
    table_curmonth[["placering"]] <- 1:nrow(table_curmonth)

    table_curmonth <- dplyr::left_join(
      table_curmonth,dat_master, by = c("user"="user")
    )
    table_curmonth %>% dplyr::select(
      name,
      party,
      placering,
      likes_curmonth,
      likes_mean_curmonth,
      activity_curmonth,
      tweets_curmonth,
      comments_curmonth
    ) -> table_curmonth

    ### CURRENT YEAR ###
    table_currentx %>% dplyr::select(
      user,
      activity_curyear,
      tweets_curyear,
      comments_curyear,
      likes_curyear,
      likes_mean_curyear
    ) %>% dplyr::arrange(
      desc(likes_curyear)
    ) -> table_curyear
    table_curyear[["likes_mean_curyear"]] <- round(table_curyear[["likes_mean_curyear"]],2)
    table_curyear[["placering"]] <- 1:nrow(table_curyear)

    table_curyear <- dplyr::left_join(
      table_curyear,dat_master, by = c("user"="user")
    )
    table_curyear %>% dplyr::select(
      name,
      party,
      placering,
      likes_curyear,
      likes_mean_curyear,
      activity_curyear,
      tweets_curyear,
      comments_curyear
    ) -> table_curyear

    # put into list
    out <- list(
      curweek = table_curweek,
      curmonth = table_curmonth,
      curyear = table_curyear
    )

    return(out)

  })

  ### CURRENT WEEK TABLE ###
  output$curweek_table <- renderReactable({

    reactable(
      theme = nytimes(),
      dat()[["curweek"]],
      resizable = TRUE,
      sortable = TRUE,
      filterable = TRUE,
      searchable = TRUE,
      pagination = TRUE,
      fullWidth = TRUE,
      defaultColDef = colDef(
        #header = function(value) gsub("_", "_", value, fixed = TRUE),
        #cell = function(value) format(value, nsmall = 1),
        align = "center",

        minWidth = 120
      ),
      columns = list(
        name = colDef(
          name = "Politiker",
          align = "left",
          minWidth = 180
        ),
        party = colDef(
          name = "Parti",
          align = "center",
          minWidth = 180
        ),
        placering = colDef(
          name = "Placering",
          align = "center",
          minWidth = 180
        ),
        likes_curweek = colDef(
          name = "Likes I Alt",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curweek, font_color = "black", highlighter = "#FFD700")
        ),
        likes_mean_curweek = colDef(
          name = "Likes Gns.",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curweek, font_color = "black", highlighter = "#FFD700")
        ),
        activity_curweek = colDef(
          name = "Aktivitet",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curweek, font_color = "black", highlighter = "#FFD700")
        ),
        tweets_curweek = colDef(
          name = "Tweets",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curweek, font_color = "black", highlighter = "#FFD700")
        ),
        comments_curweek = colDef(
          name = "Kommentarer",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curweek, font_color = "black", highlighter = "#FFD700")
        )
      )
    )

  })

  ### CURRENT MONTH TABLE ###
  output$curmonth_table <- renderReactable({

    reactable(
      theme = nytimes(),
      dat()[["curmonth"]],
      resizable = TRUE,
      sortable = TRUE,
      filterable = TRUE,
      searchable = TRUE,
      pagination = TRUE,
      fullWidth = TRUE,
      defaultColDef = colDef(
        #header = function(value) gsub("_", "_", value, fixed = TRUE),
        #cell = function(value) format(value, nsmall = 1),
        align = "center",

        minWidth = 120
      ),
      columns = list(
        name = colDef(
          name = "Politiker",
          align = "left",
          minWidth = 180
        ),
        party = colDef(
          name = "Parti",
          align = "center",
          minWidth = 180
        ),
        placering = colDef(
          name = "Placering",
          align = "center",
          minWidth = 180
        ),
        likes_curmonth = colDef(
          name = "Likes I Alt",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curmonth, font_color = "black", highlighter = "#FFD700")
        ),
        likes_mean_curmonth = colDef(
          name = "Likes Gns.",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curmonth, font_color = "black", highlighter = "#FFD700")
        ),
        activity_curmonth = colDef(
          name = "Aktivitet",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curmonth, font_color = "black", highlighter = "#FFD700")
        ),
        tweets_curmonth = colDef(
          name = "Tweets",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curmonth, font_color = "black", highlighter = "#FFD700")
        ),
        comments_curmonth = colDef(
          name = "Kommentarer",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curmonth, font_color = "black", highlighter = "#FFD700")
        )
      )
    )

  })

  ### CURRENT YEAR TABLE ###
  output$curyear_table <- renderReactable({

    reactable(
      theme = nytimes(),
      dat()[["curyear"]],
      resizable = TRUE,
      sortable = TRUE,
      filterable = TRUE,
      searchable = TRUE,
      pagination = TRUE,
      fullWidth = TRUE,
      defaultColDef = colDef(
        #header = function(value) gsub("_", "_", value, fixed = TRUE),
        #cell = function(value) format(value, nsmall = 1),
        align = "center",

        minWidth = 120
      ),
      columns = list(
        name = colDef(
          name = "Politiker",
          align = "left",
          minWidth = 180
        ),
        party = colDef(
          name = "Parti",
          align = "center",
          minWidth = 180
        ),
        placering = colDef(
          name = "Placering",
          align = "center",
          minWidth = 180
        ),
        likes_curyear = colDef(
          name = "Likes I Alt",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curyear, font_color = "black", highlighter = "#FFD700")
        ),
        likes_mean_curyear = colDef(
          name = "Likes Gns.",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curyear, font_color = "black", highlighter = "#FFD700")
        ),
        activity_curyear = colDef(
          name = "Aktivitet",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curyear, font_color = "black", highlighter = "#FFD700")
        ),
        tweets_curyear = colDef(
          name = "Tweets",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curyear, font_color = "black", highlighter = "#FFD700")
        ),
        comments_curyear = colDef(
          name = "Kommentarer",
          align = "center",
          minWidth = 120,
          style = highlight_max(table_curyear, font_color = "black", highlighter = "#FFD700")
        )
      )
    )
  })
})
kasper2619/someR documentation built on May 21, 2023, 3:53 a.m.