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

reprex renders data.frame wrong? #77

Closed
pgensler opened this Issue Mar 31, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@pgensler

pgensler commented Mar 31, 2017

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>
@pgensler

This comment has been minimized.

pgensler commented Mar 31, 2017

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
@pgensler

This comment has been minimized.

pgensler commented Mar 31, 2017

attaching rstudio viewerpane for verification:
rstudio-viewer

@hadley

This comment has been minimized.

Member

hadley commented May 26, 2017

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.

@jennybc

This comment has been minimized.

Member

jennybc commented May 26, 2017

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.

@jennybc

This comment has been minimized.

Member

jennybc commented Sep 27, 2017

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

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