Skip to content
New issue

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

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

Already on GitHub? # to your account

switch statements with strings cannot be tidied #78

Closed
bioinformatist opened this issue Jun 24, 2019 · 2 comments
Closed

switch statements with strings cannot be tidied #78

bioinformatist opened this issue Jun 24, 2019 · 2 comments

Comments

@bioinformatist
Copy link

Hi Yihui,

To reproduce this matter, you can use code snippets below:

matrix.please <- function(x) {
  m<-as.matrix(x[,-1])
  rownames(m)<-x[,1]
  m
}

merge.duplicates <- function(x, method = "median") {
  switch(method,
         "median" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = median)),
         "mean" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = mean)),
         "max" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = max)),
         "min" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = min)),
         "var" = get.max.var.rows(x))
}

I got error as:

Error in base::parse(text = code, keep.source = FALSE) : 
  <text>:44:1: unexpected string constant
43: 
44: "median"
    ^
@yihui
Copy link
Owner

yihui commented Jun 24, 2019

I cannot reproduce it. It works well for me:

formatR::tidy_source(text = 'matrix.please <- function(x) {
  m<-as.matrix(x[,-1])
  rownames(m)<-x[,1]
  m
}

merge.duplicates <- function(x, method = "median") {
  switch(method,
         "median" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = median)),
         "mean" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = mean)),
         "max" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = max)),
         "min" = matrix.please(aggregate(x, by=list(row.names(x)), FUN = min)),
         "var" = get.max.var.rows(x))
}
')

Output:

matrix.please <- function(x) {
    m <- as.matrix(x[, -1])
    rownames(m) <- x[, 1]
    m
}

merge.duplicates <- function(x, method = "median") {
    switch(method, median = matrix.please(aggregate(x, by = list(row.names(x)), FUN = median)), mean = matrix.please(aggregate(x, 
        by = list(row.names(x)), FUN = mean)), max = matrix.please(aggregate(x, by = list(row.names(x)), 
        FUN = max)), min = matrix.please(aggregate(x, by = list(row.names(x)), FUN = min)), var = get.max.var.rows(x))
}

@bioinformatist
Copy link
Author

I'm so sorry that it is caused by same situation as this matter.

Sorry for wasting your time! Closed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants