New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show code and get feedback #8

Open
sckott opened this Issue Jun 7, 2016 · 15 comments

Comments

Projects
None yet
7 participants
@sckott
Member

sckott commented Jun 7, 2016

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

This comment has been minimized.

Show comment
Hide comment
@jimrothstein

jimrothstein Jun 7, 2016

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.

jimrothstein commented Jun 7, 2016

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 sckott changed the title from Show and tell to Show code and get feedback Jun 7, 2016

@sckott

This comment has been minimized.

Show comment
Hide comment
@sckott

sckott Jun 8, 2016

Member

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

Member

sckott commented Jun 8, 2016

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

@ww44ss

This comment has been minimized.

Show comment
Hide comment
@ww44ss

ww44ss Jun 16, 2016

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()))

ww44ss commented Jun 16, 2016

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

This comment has been minimized.

Show comment
Hide comment
@znmeb

znmeb Jun 16, 2016

packrat? drat?

znmeb commented Jun 16, 2016

packrat? drat?

@sckott

This comment has been minimized.

Show comment
Hide comment
@sckott

sckott Jun 16, 2016

Member

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

Member

sckott commented Jun 16, 2016

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

@ww44ss

This comment has been minimized.

Show comment
Hide comment
@ww44ss

ww44ss Jun 16, 2016

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???

ww44ss commented Jun 16, 2016

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

This comment has been minimized.

Show comment
Hide comment
@sckott

sckott Jun 16, 2016

Member

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

Member

sckott commented Jun 16, 2016

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

@smithjd

This comment has been minimized.

Show comment
Hide comment
@smithjd

smithjd Jun 21, 2016

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.

smithjd commented Jun 21, 2016

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

This comment has been minimized.

Show comment
Hide comment
@ww44ss

ww44ss Jun 22, 2016

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
#8 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AG65vQ7BKEujd8_Mdv_l-gESHAKvioDBks5qOHoEgaJpZM4IwMh1
.

Sent from Gmail Mobile

ww44ss commented Jun 22, 2016

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
#8 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AG65vQ7BKEujd8_Mdv_l-gESHAKvioDBks5qOHoEgaJpZM4IwMh1
.

Sent from Gmail Mobile

@sckott

This comment has been minimized.

Show comment
Hide comment
@sckott

sckott Jun 22, 2016

Member

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

Member

sckott commented Jun 22, 2016

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

@ismayc

This comment has been minimized.

Show comment
Hide comment
@ismayc

ismayc Jun 22, 2016

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 Jun 22, 2016

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

This comment has been minimized.

Show comment
Hide comment
@ismayc

ismayc Jun 22, 2016

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.

ismayc commented Jun 22, 2016

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

This comment has been minimized.

Show comment
Hide comment
@maryannet

maryannet Jun 22, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@ismayc

ismayc Jun 24, 2016

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.

ismayc commented Jun 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@sckott

sckott Jun 24, 2016

Member

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

Member

sckott commented Jun 24, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment