data.frame在RStudio控制台中具有日期列输出,预览,但不在块下方

时间:2017-02-26 19:43:50

标签: rstudio

使用Rstudio 3.3.2的笔记本:

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

当尝试显示带有日期列的data.frame时,data.frame将显示在“查看器”选项卡中,但不会显示在块本身下方:

    ```{r}
    df <- data.frame(date=c("31/08/2011", "31/07/2011", "30/06/2011"),values=c(0.8378,0.8457,0.8147))               

    #no Date format ->OK, output below the chunk
    df

    df$dateformatted<-as.Date(strptime(df$date,'%d/%m/%Y'))

    #with Date format -> NOK, no output below the chunk,only in Viewer.
    df 

    ```

RStudio诊断:

26 Feb 2017 20:42:00 [rsession-x] ERROR r error 7 (Unexpected data type); OCCURRED AT: rstudio::core::Error rstudio::r::json::{anonymous}::jsonValueFromVectorElement(SEXP, int, rstudio::core::json::Value*) /home/ubuntu/rstudio/src/cpp/r/RJson.cpp:149; LOGGED FROM: void rstudio::session::modules::rmarkdown::notebook::enqueueChunkOutput(const string&, const string&, const string&, unsigned int, ChunkOutputType, const rstudio::core::FilePath&, const Value&) /home/ubuntu/rstudio/src/cpp/session/modules/rmarkdown/NotebookOutput.cpp:449

this question有关。

有谁知道我做错了什么?非常感谢。

3 个答案:

答案 0 :(得分:1)

这确实是当前版本的RStudio中的一个错误:data.frame包含Date对象在笔记本中无法正确呈现。您可以尝试安装RStudio的最新每日版本并确认问题已解决:

http://dailies.rstudio.com

答案 1 :(得分:1)

我很欣赏Rigoberta和Kevin的帖子。我遇到了同样的问题(rstudio 1.0.136)。

我害怕使用http://dailies.rstudio.com中描述的最新每日构建:“每日构建仅用于测试目的,不建议用于一般用途。对于稳定版本,请访问rstudio.com。 “

因为我从未使用过rstudio的“不稳定”版本,所以现在回滚rstudio版本似乎是一种更好的方法,但赞赏意见。

在等待决定是回到RStudio 1.0.44还是向前推进到“不稳定”版本时,我发现矩阵对象不会发生问题,暂时我正在使用print(如.matrix()):

```{r}
df <- data.frame(date = c("31/08/2011", "31/07/2011", "30/06/2011"), values = c(0.8378, 0.8457, 0.8147))
df$dateformatted <- as.Date(strptime(df$date, '%d/%m/%Y'))

print(as.matrix(df), quote = FALSE)
```
     date       values dateformatted
[1,] 31/08/2011 0.8378 2011-08-31   
[2,] 31/07/2011 0.8457 2011-07-31   
[3,] 30/06/2011 0.8147 2011-06-30  

模拟head()的行为:

print(as.matrix(df), quote = FALSE, max = length(df) * 6)

答案 2 :(得分:0)

您可以使用此功能

bf <- function(x) x %>% ungroup() %>% mutate_if(is.Date, as.character) 

使包含日期的数据框显示为预期

```{r}
data.frame(date = as.Date(Sys.time()), num = 1:3) %>% bf
```

日期编号

2017-03-18 1
2017-03-18 2
2017-03-18 3
3行

相关问题