Skip to content
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

Wrong results surrounding commented lines #103

Closed
3 tasks done
SPan-18 opened this issue Jul 30, 2024 · 4 comments
Closed
3 tasks done

Wrong results surrounding commented lines #103

SPan-18 opened this issue Jul 30, 2024 · 4 comments

Comments

@SPan-18
Copy link

SPan-18 commented Jul 30, 2024


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('formatR'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/formatR').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@SPan-18
Copy link
Author

SPan-18 commented Jul 30, 2024

I am using the function formatR::tidy_dir() function on the following script. I have commented out some lines using the keyboard shortcut cmd+shift+C in the RStudio editor. Hence the placement of # in the script are automatic suggestions by the RStudio editor.

#' Make a surface plot
#'
#' @param tab a data-frame containing spatial co-ordinates and the variable to plot
#' @param coords_name name of the two columns that contains the co-ordinates of the points
#' @param var_name name of the column containing the variable to be plotted
#' @param h integer; controls smoothness of the spatial interpolation as appearing in the \code{mba.surf} function of the \code{MBA} package. Default is 8.
#' @param col.pal Optional; color palette, preferably divergent, use \code{colorRampPalette} function from \code{grDevices}. Deafult is 'RdYlBu'.
#' @param mark_points Logical; if \code{TRUE}, the input points are marked
#' @export
#' @importFrom MBA mba.surf
#' @importFrom ggplot2 ggplot aes_string geom_raster scale_fill_distiller geom_point scale_fill_gradientn
#' @importFrom ggplot2 theme_bw theme element_line element_blank element_text
#' @importFrom stats na.omit
surfaceplot <- function(tab, coords_name, var_name,
                        h = 8, col.pal = NULL,
                        mark_points = FALSE){

  surf <- mba.surf(tab[,c(coords_name, var_name)],
                   no.X = 250, no.Y = 250, h = h, m = 1, n = 1,
                   extend=FALSE)$xyz.est

  surf_df <- data.frame(expand.grid(surf$x, surf$y), z = as.vector(surf$z))
  surf_df <- na.omit(surf_df)
  names(surf_df) <- c("x", "y", "z")

  plot <- ggplot(surf_df, aes_string(x = 'x', y = 'y')) +
    geom_raster(aes_string(fill = 'z')) +
    # scale_fill_gradientn(colours = col.br(100)) +
    # scale_fill_gradientn(colours = col.RdBu(100)) +
    theme_bw() +
    theme(axis.ticks = element_line(linewidth = 0.25),
          panel.background = element_blank(),
          panel.grid = element_blank(),
          legend.box.just = "center",
          aspect.ratio = 1)

  if(is.null(col.pal)){
    plot <- plot + scale_fill_distiller(palette = "RdYlBu", direction = -1,
                                        label = function(x) sprintf("%.1f", x))
  }else{
    plot <- plot + scale_fill_gradientn(colours = col.pal)
  }

  if(mark_points){
    plot <- plot + geom_point(aes_string(x = coords_name[1], y = coords_name[2]), data = tab,
                              color = "black", fill = NA, shape = 21, stroke = 0.5, alpha = 0.5)
  }

  plot
}

The output, as given below, is obviously incorrect. The function runs without any error.

#' Make a surface plot
#'
#' @param tab a data-frame containing spatial co-ordinates and the variable to plot
#' @param coords_name name of the two columns that contains the co-ordinates of the points
#' @param var_name name of the column containing the variable to be plotted
#' @param h integer; controls smoothness of the spatial interpolation as appearing in the \code{mba.surf} function of the \code{MBA} package. Default is 8.
#' @param col.pal Optional; color palette, preferably divergent, use \code{colorRampPalette} function from \code{grDevices}. Deafult is 'RdYlBu'.
#' @param mark_points Logical; if \code{TRUE}, the input points are marked
#' @export
#' @importFrom MBA mba.surf
#' @importFrom ggplot2 ggplot aes_string geom_raster scale_fill_distiller geom_point scale_fill_gradientn
#' @importFrom ggplot2 theme_bw theme element_line element_blank element_text
#' @importFrom stats na.omit
surfaceplot <- function(tab, coords_name, var_name, h = 8, col.pal = NULL, mark_points = FALSE) {

    surf <- mba.surf(tab[, c(coords_name, var_name)], no.X = 250, no.Y = 250, h = h, m = 1, n = 1, extend = FALSE)$xyz.est

    surf_df <- data.frame(expand.grid(surf$x, surf$y), z = as.vector(surf$z))
    surf_df <- na.omit(surf_df)
    names(surf_df) <- c("x", "y", "z")

    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + plot
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + <-
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + ggplot(surf_df,
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + aes_string(x
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + =
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + "x",
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + y
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + =
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + "y"))
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + +
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + geom_raster(aes_string(fill
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + =
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + "z"))
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + +
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + #
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + scale_fill_gradientn(colours
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + =
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + col.br(100))
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + +
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + scale_fill_gradientn(colours
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + =
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + col.RdBu(100))
    plot <- ggplot(surf_df, aes_string(x = "x", y = "y")) + geom_raster(aes_string(fill = "z")) + # scale_fill_gradientn(colours = col.br(100)) +  scale_fill_gradientn(colours = col.RdBu(100)) + +
    theme_bw() + theme(axis.ticks = element_line(linewidth = 0.25), panel.background = element_blank(), panel.grid = element_blank(),
        legend.box.just = "center", aspect.ratio = 1)

    if (is.null(col.pal)) {
        plot <- plot + scale_fill_distiller(palette = "RdYlBu", direction = -1, label = function(x) sprintf("%.1f", x))
    } else {
        plot <- plot + scale_fill_gradientn(colours = col.pal)
    }

    if (mark_points) {
        plot <- plot + geom_point(aes_string(x = coords_name[1], y = coords_name[2]), data = tab, color = "black", fill = NA, shape = 21,
            stroke = 0.5, alpha = 0.5)
    }

    plot
}

My session information is as follows.

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.5, RStudio 2024.4.2.764

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  formatR_1.14

@yihui
Copy link
Owner

yihui commented Jul 31, 2024

https://yihui.org/formatr/#6-further-notes

@SPan-18 SPan-18 closed this as completed Aug 2, 2024
@SPan-18
Copy link
Author

SPan-18 commented Aug 2, 2024

Thanks Yihui for the clarification. As it turns out, special care needs to be taken while commenting out multiple lines of code especially for piping functions. The link above explains the rule. Hence, I am closing the issue.

@yihui
Copy link
Owner

yihui commented Aug 2, 2024

I should really throw an error or at least emit a warning in this case.

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

No branches or pull requests

2 participants