Skip to content

reprex renders data.frame wrong? #77

Closed

Description

@pgensler

I'm not sure if this is a bug with the way I am using reprex, but when I try to copy and paste this code two odd things occur:

  1. When I copy and paste the code onto github, it seems like there are slashes that are put into my column headers, which seems like a bug:
  2. Secondly, it seems like there is a bit of a spacing issue, which makes the table not renderable on github:
    Code below:
    #Sample Dataframe
    pacman::p_load(magrittr,dplyr,formattable)
    sample = data.frame(
    beer_brewerId = c("8481", "8481", "8481"),
    rev_app = c("4","1", "2"),
    beer_name = c("John Harvards Simcoe IPA", "John Harvards Simcoe IPA", "John Harvards American Brown Ale"),
    review_taste =c("6", "7", "6"), stringsAsFactors = FALSE
    )
    glimpse(sample)
    formattable(sample, align ="c")

#Piping:
sample %>%
filter(beer_name == "John Harvards Simcoe IPA") %>%
formattable(., align = "c")

Output from reprex() function:

#Sample Dataframe
pacman::p_load(magrittr,dplyr,formattable)
sample = data.frame(
  beer_brewerId = c("8481", "8481", "8481"),
  rev_app = c("4","1", "2"),
  beer_name = c("John Harvards Simcoe IPA", "John Harvards Simcoe IPA", "John Harvards American Brown Ale"),
  review_taste =c("6", "7", "6"), stringsAsFactors = FALSE
)
glimpse(sample)
#> Observations: 3
#> Variables: 4
#> $ beer_brewerId <chr> "8481", "8481", "8481"
#> $ rev_app       <chr> "4", "1", "2"
#> $ beer_name     <chr> "John Harvards Simcoe IPA", "John Harvards Simco...
#> $ review_taste  <chr> "6", "7", "6"
formattable(sample, align ="c")
beer\_brewerId rev\_app beer\_name review\_taste
8481 4 John Harvards Simcoe IPA 6
8481 1 John Harvards Simcoe IPA 7
8481 2 John Harvards American Brown Ale 6
``` r

#Piping:
sample %>%
filter(beer_name == "John Harvards Simcoe IPA") %>%
formattable(., align = "c")


<table class="table table-condensed">
<thead>
<tr>
<th style="text-align:center;">
beer\_brewerId
</th>
<th style="text-align:center;">
rev\_app
</th>
<th style="text-align:center;">
beer\_name
</th>
<th style="text-align:center;">
review\_taste
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center;">
8481
</td>
<td style="text-align:center;">
4
</td>
<td style="text-align:center;">
John Harvards Simcoe IPA
</td>
<td style="text-align:center;">
6
</td>
</tr>
<tr>
<td style="text-align:center;">
8481
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
John Harvards Simcoe IPA
</td>
<td style="text-align:center;">
7
</td>
</tr>
</tbody>
</table>

Activity

  1. pgensler commented on Mar 31, 2017

    @pgensler
    Author

    I believe the reprex function is inserting an extra line break between the code and the HTML rendered. I have attached the screenshot with the tables having extra /'s in the HTML, and what code is throwing the error.
    needs-space
    bad-cols-headers

    The correct output should be as such::

    #Sample Dataframe
    pacman::p_load(magrittr,dplyr,formattable)
    sample = data.frame(
      beer_brewerId = c("8481", "8481", "8481"),
      rev_app = c("4","1", "2"),
      beer_name = c("John Harvards Simcoe IPA", "John Harvards Simcoe IPA", "John Harvards American Brown Ale"),
      review_taste =c("6", "7", "6"), stringsAsFactors = FALSE
    )
    glimpse(sample)
    #> Observations: 3
    #> Variables: 4
    #> $ beer_brewerId <chr> "8481", "8481", "8481"
    #> $ rev_app       <chr> "4", "1", "2"
    #> $ beer_name     <chr> "John Harvards Simcoe IPA", "John Harvards Simco...
    #> $ review_taste  <chr> "6", "7", "6"
    formattable(sample, align ="c")
    beer_brewerId rev_app beer_name review_taste
    8481 4 John Harvards Simcoe IPA 6
    8481 1 John Harvards Simcoe IPA 7
    8481 2 John Harvards American Brown Ale 6
    #Piping:
    sample %>%
      filter(beer_name == "John Harvards Simcoe IPA") %>%
      formattable(., align = "c")
    beer_brewerId rev_app beer_name review_taste
    8481 4 John Harvards Simcoe IPA 6
    8481 1 John Harvards Simcoe IPA 7
  2. pgensler commented on Mar 31, 2017

    @pgensler
    Author

    attaching rstudio viewerpane for verification:
    rstudio-viewer

  3. hadley commented on May 26, 2017

    @hadley
    Member

    Could you please construct a simpler reprex? For your case this would include loading packages in a standard way, eliminating unneeded packages (magrittr + dplyr) and simplifying the data frame.

  4. jennybc commented on May 26, 2017

    @jennybc
    Member

    An important aspect of this is the HTML output of formattable, so the title is a bit misleading. If I save this reprex as .R with this yaml:

    #' ---
    #' output: github_document
    #' ---

    And render it with rmarkdown directly, the .md has the same "features", e.g. the escaped underscores in the variable names and the lack of space between the html table and the next R chunk. Ditto if I store this code in .Rmd.

    So I think this is really about how rmarkdown::render() works when output format is github_document and a chunk creates html.

  5. jennybc commented on Sep 27, 2017

    @jennybc
    Member

    I think I have the fix. With it, the original code (modified to not use pacman) produces this output:

    #Sample Dataframe
    library(magrittr)
    suppressPackageStartupMessages(library(dplyr))
    library(formattable)
    
    sample = data.frame(
      beer_brewerId = c("8481", "8481", "8481"),
      rev_app = c("4","1", "2"),
      beer_name = c("John Harvards Simcoe IPA", "John Harvards Simcoe IPA", "John Harvards American Brown Ale"),
      review_taste =c("6", "7", "6"), stringsAsFactors = FALSE
    )
    glimpse(sample)
    #> Observations: 3
    #> Variables: 4
    #> $ beer_brewerId <chr> "8481", "8481", "8481"
    #> $ rev_app       <chr> "4", "1", "2"
    #> $ beer_name     <chr> "John Harvards Simcoe IPA", "John Harvards Simco...
    #> $ review_taste  <chr> "6", "7", "6"
    formattable(sample, align ="c")

    beer_brewerId

    rev_app

    beer_name

    review_taste

    8481

    4

    John Harvards Simcoe IPA

    6

    8481

    1

    John Harvards Simcoe IPA

    7

    8481

    2

    John Harvards American Brown Ale

    6

    #Piping:
    sample %>%
      filter(beer_name == "John Harvards Simcoe IPA") %>%
      formattable(., align = "c")

    beer_brewerId

    rev_app

    beer_name

    review_taste

    8481

    4

    John Harvards Simcoe IPA

    6

    8481

    1

    John Harvards Simcoe IPA

    7

  6. added a commit that references this issue on Sep 27, 2017
    dd62ff4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions