4

I am getting frequencies from a few rasters and am doing so in R-Markdown. I am using lapply to get the frequencies from the rasters in a list. When I store those frequencies in a list of data.frames, the chunk output displays some unexpected non-numeric characters.

Example rasters:

```{r}
require(raster)

r1 <- setValues(raster(nrows = 10, ncols = 10), sample(1:10, 100, replace = TRUE))
r2 <- setValues(raster(nrows = 10, ncols = 10), sample(1:10, 100, replace = TRUE))

rList <- list(r1, r2)
```

Getting the frequencies:

```{r}
lapply(rList, function(ras) {
  data.frame(freq(ras))
})
```

Output from the above chunk:

enter image description here

If I display only the data frame itself, those characters are not displayed:

```{r}
lapply(rList, function(ras) {
  data.frame(freq(ras))
})[[2]]
```

enter image description here

The correct values are also shown if do not use data.frame:

```{r}
lapply(rList, function(ras) {
  freq(ras)
})
```

enter image description here

I've tried saving the Rmd with UTF-8 encoding and am on RStudio 1.2.5019. Any ideas on how to get the list of data frames to display properly would be appreciated.

Edit: Just a note that the characters do not display in any scenario in the generated html file, only in the specific chunk in the R Notebook file itself.

Edit 2:

The full code and YAML header for the notebook that generates the strange characters is below:

---
title: "R Notebook"
output: html_notebook
---

```{r}
require(raster)

r1 <- setValues(raster(nrows = 10, ncols = 10), sample(1:10, 100, replace = TRUE))
r2 <- setValues(raster(nrows = 10, ncols = 10), sample(1:10, 100, replace = TRUE))

rList <- list(r1, r2)
```

```{r}
lapply(rList, function(ras) {
  data.frame(freq(ras))
})
```


```{r}
lapply(rList, function(ras) {
  data.frame(freq(ras))
})[[2]]
```
Luke C
  • 10,081
  • 1
  • 14
  • 21
  • You've got some fancy table formatting going on there. If I make a plain .Rmd file with those chunks I get plain R code output, not zebra-stripe tables with column types and underlines. Unless that's the default RStudio styling (I'm testing from R console) can you supply a complete Rmd with headers etc? – Spacedman Nov 09 '19 at 19:22
  • @Spacedman - Sorry for the delay, thanks for the response. As far as I know, that is the default output for a Notebook in R Studio, I have made no changes to the yaml header or to the chunk settings. I have included the full Notebook code that generates the strange characters for me. – Luke C Nov 12 '19 at 06:38
  • I can't replicate this issue. Tried on Win 10 and Ubuntu Linux. I get numbers instead of the characters shown above. – JBGruber Nov 18 '19 at 14:20
  • @JBGruber - Thank you for trying, I still haven't figured out why this happens. – Luke C Nov 19 '19 at 05:59
  • A short and dirty solution would be to coerce the contents to strings instead. I would try that, for now. Also, it would help if you could add the output of `sessionInfo()`. – gvegayon Nov 20 '19 at 19:28
  • Came here to say the bit about the `sessionInfo()`. I think that if we can figure out how to reproduce it it would be easy to solve. – JBGruber Nov 21 '19 at 11:28
  • 1
    A similar issue has been listed here https://github.com/rstudio/rstudio/issues/3163 – Timsy Suri Nov 23 '19 at 15:42
  • 1
    Report your issue there, if you haven't found the solution yet. – Timsy Suri Nov 23 '19 at 16:10

2 Answers2

2

Check out this code is properly showing the output. You can use print enter image description here

On previewing the html notebook output is not showing any UTF characters

enter image description here

If you want to use the chunk output than you can use

as.data.frame()

enter image description here

Timsy Suri
  • 458
  • 3
  • 9
  • @Bill Chen - Are you using Linux on your system? – Timsy Suri Nov 16 '19 at 04:05
  • Thank you, but I'm hoping to use the chunk output with scrollable tables so `print` won't work. – Luke C Nov 16 '19 at 06:19
  • Yes that is one way around it! I'm really trying to sort out the *why* of what's going on here- what is going on with my installations on two different systems that I'm getting the strange formatting in the first place. However, if no one else can replicate my error and help me fix it properly (rather than a workaround, of which there are a few options) I will mark this answer as correct- just going to give it a few days. Thanks for the help! – Luke C Nov 19 '19 at 06:02
  • 2
    Just like with posting questions, when you post answers it's *far* more helpful for you to post the code as text that other people can work with, not pictures of it. – camille Nov 23 '19 at 01:36
0

I've had this experience before with that same "ÿ" character, but haven't thought about it a lot (Mac RStudio). I have been able to re-run my code and haven't been able to reproduce it on the same chunk, even in the same session.

I do use the formattable package often, and since folks are mentioning the formatting aspect, perhaps that's at play? Although it's hard for me to envision exactly how.

I did find these related items about Pandoc rendering Unicode artifacts on certain character inputs, and about Pandoc converting to UTF-8. Since the workflow involves knitr feeding Pandoc, maybe that's a piece?

Can't quite connect the dots, but hope it helps someone else put it together.

I don't remember it happening recently, so potentially one avenue is to update pandoc and knitr (or even RStudio altogether -- I'm on 1.2.5019)?

ravic_
  • 1,731
  • 9
  • 13