pdxrlang / aggregate

aggregate - a pdxrlang subgroup meetup
10 stars 2 forks source link

Show code and get feedback #8

Open sckott opened 8 years ago

sckott commented 8 years ago

For the next aggregate meetup http://www.meetup.com/portland-r-user-group/events/231720260/ we'll have some people show code they want feedback on and the group can give feedback and learn in the process

During this process, for sure many things will come up that we can dig into in more detail

jimrothstein commented 8 years ago

Not sure if appropriate, so just a thought for group (just a beginner here)

How Do You Remove| Manage unwanted or old projects, packages, files from your R, RStudio setup? -remove.packages("myPackage") ?

I use a chromebook (with ubuntu), so storage space can be bit limiting.

sckott commented 8 years ago

Thanks for the idea @jimrothstein - Can you have something ready to show at next meetup along these lines?

ww44ss commented 8 years ago

Here is one I stumbled over: (i did a cursory search but didn't see an answer anywhere that worked)

Background: I really like it when people add this code to their programs to manage libraries.

if(! ("tm" %in% rownames(installed.packages()) ))        {install.packages("tm")}
library(tm)

Especially when trying new stuff, it just simplifies steps. And also when you install a new version of R it makes life a little easier even running your legacy stuff, etc.

The problem is the code gets long, listy, and awkward if you require a lot of (~10) packages.

So, I thought, why not go a step further and streamline package mgt with a function?

currently.installed.packages <- rownames(installed.packages())
package.library <- function(package = "ggplot2") {
        if(! (package %in% currently.installed.packages))   {install.packages(package, character.only = TRUE)}
        library(package)
    }
required.packages <- c("tm", "dplyr", "RWeka", "SnowballC", "ggplot2", "reshape2", "stringr", "viridis", "syuzhet")
lapply(required.packages, package.library)

The problem is that this code produces an error Error in library(package) : there is no package called ‘package’

Even when I run

package.library("tm")

I get the error Error in library(package) : there is no package called ‘package’

This is a fresh problem. Not really critical path to what I am doing, but it is annoying and I would like to find a way to fix it so i can just type a fire and forget list of libraries and then lapply a function and be done.

Any thoughts?????

(note that in the documentation this example does work

pkg <- "splines"
library(pkg, character.only = TRUE)
detach(pos = match(paste("package", pkg, sep = ":"), search()))
znmeb commented 8 years ago

packrat? drat?

sckott commented 8 years ago

@ww44ss good one. Want to discuss at the meetup week? if not, I can chime in here

ww44ss commented 8 years ago

either way. maybe both? That way we have written answers and we can also use the time for some discussion in case others have examples, learnings, etc???

sckott commented 8 years ago

Okay, well maybe we can put answers here in writing during/after meetup

smithjd commented 8 years ago

I'd like comments and suggestions on this snippet of code.

First: order a factor using dplyr:

keyword_order <- respondents_key %>% count(Español) %>% ungroup() %>% arrange(desc(n)) %>% as.list()
respondents_key$Español <- factor(respondents_key$Español, levels = keyword_order$Español)
respondents_key$Español = with(respondents_key, factor(Español, levels = rev(levels(Español))))

Second, plot a bar chart using ggplot:

  p <- ggplot(respondents_key, aes(x = country)) +
  geom_bar(aes(fill = `Español`)) +
  coord_flip()

p + labs(title = "Lideres en diferentes paises indentificaron ambitos\nsimilares como los mas importantes",
        y = "Líderes que indicaron la importancia de cada ámbito\n(espacio virtual ofrecido durante mayo 2016)", 
        x = "Pais") +
    scale_fill_brewer(direction = -1,
                      name = "Ámbito indicado") +
   theme(legend.justification = c(1,0), legend.position = c(1,0),
         axis.text = element_text(size = 14),
         axis.title = element_text(size = 16),
         title = element_text(size = 16, face = "bold"),
         legend.text = element_text(size = 14)
         )

What I've struggled with is: getting all the English v Spanish stuff separated out. I don't want to have to iterate back and do step 1 (in English) to reorder the factor for producing a plot in English.

ww44ss commented 8 years ago

Do you have an example of the data?

On Tuesday, June 21, 2016, John David Smith notifications@github.com wrote:

I'd like comments and suggestions on this snippet of code.

First: order a factor using dplyr:

keyword_order <- respondents_key %>% count(Español) %>% ungroup() %>% arrange(desc(n)) %>% as.list() respondents_key$Español <- factor(respondents_key$Español, levels = keyword_order$Español) respondents_key$Español = with(respondents_key, factor(Español, levels = rev(levels(Español))))

Second, plot a bar chart using ggplot:

p <- ggplot(respondents_key, aes(x = country)) + geom_bar(aes(fill = Español)) + coord_flip()

p + labs(title = "Lideres en diferentes paises indentificaron ambitos\nsimilares como los mas importantes", y = "Líderes que indicaron la importancia de cada ámbito\n(espacio virtual ofrecido durante mayo 2016)", x = "Pais") + scale_fill_brewer(direction = -1, name = "Ámbito indicado") + theme(legend.justification = c(1,0), legend.position = c(1,0), axis.text = element_text(size = 14), axis.title = element_text(size = 16), title = element_text(size = 16, face = "bold"), legend.text = element_text(size = 14) )

What I've struggled with is: getting all the English v Spanish stuff separated out. I don't want to have to iterate back and do step 1 (in English) to reorder the factor for producing a plot in English.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pdxrlang/aggregate/issues/8#issuecomment-227606574, or mute the thread https://github.com/notifications/unsubscribe/AG65vQ7BKEujd8_Mdv_l-gESHAKvioDBks5qOHoEgaJpZM4IwMh1 .

Sent from Gmail Mobile

sckott commented 8 years ago

Thanks @smithjd , if you come tomorrow night, that'd be something good to get feedback on

ismayc commented 8 years ago

In terms of @ww44ss's question before, I usually include a chunk similar to this at the top of my R code:

pkg <- c("tidyr", "dplyr", "ggplot2", 
  "knitr", "rmarkdown", "readr", 
  "DT","devtools")

new.pkg <- pkg[!(pkg %in% installed.packages())]

if (length(new.pkg)) {
  install.packages(new.pkg, repos = "http://cran.rstudio.com")
}

lapply(pkg, library, character.only = TRUE)

I've long been thinking that this should be added as a function in a package. But then you'd have to figure out a way to make sure that package is installed...

ismayc commented 8 years ago

I use this chunk of code so frequently that I decided to create an R Markdown template that essentially just starts my analysis with that chunk of code. If you'd like to get access to this, install my reedtemplates package (devtools::install_github("ismayc/reedtemplates")) and then select Package Loader from the New R Markdown template dialog.

maryannet commented 8 years ago

This is something that I always mess up. I need to upgrade R in Rstudio and keep all my packages. It takes to long to reinstall the packages

ismayc commented 8 years ago

Does update.packages(ask = FALSE, checkBuilt = TRUE) solve this problem, @maryannet? I usually just let R download the packages again since I include that chunk I referenced above, but it does seem like there should be a more elegant way to reference the packages that are already installed.

sckott commented 8 years ago

I too just re-download eveything - wouldn't row.names(installed.packages()) work to get currently installed pkgs, then save that to an r object or copy/paste somewhere, then in new R version, install from that vector