2017-03-03 51 views
0

我想編碼一個閃亮的應用程序,並相當新的R codin。我正在閱讀包含3列的.txt文件。一列是日期,另外兩列是數字(日期列是作爲類字符讀取的,文本文件是通過用戶選擇的名稱來選擇的)服務器比在x軸上繪製日期和在y軸。我還添加了一個縮放功能 當日期列沒有被聲明時(因此當行被刪除時,日期被繪製爲字符)時,應用程序工作正常當Date日期被以下列的日期聲明時代碼:ggplot2不知道如何處理類的數據日期

inlees$Date <- dmy(inlees$Date) #convert from character to Date 

應用程序返回的錯誤:

Error in : ggplot2 doesn't know how to deal with data of class Date

Snapshot of the text file can be found here 任何幫助我s讚賞。

我使用下面的腳本:

UI

ui <- fluidPage(selectInput(inputId = "name", label = "Selecteer analyse", choices=c("TESTOSTERON", 
         "ANDROSTEENDION", "17OHProgesteron", "P4")), 

      plotOutput("p1", dblclick = "p1_dblclick", 
       brush = brushOpts(id = "p1_brush", resetOnNew = TRUE)), 

      plotOutput("p2", dblclick = "p2_dblclick", 
         brush = brushOpts(id = "p2_brush", resetOnNew = TRUE))) 

服務器

server <- function(input, output) { 

    rangesp1 <- reactiveValues(y = NULL) 

    selectData <- reactive ({ 

    data_path <- "ANPR" # path to the data 
    inlees <- input$name 
    files <- dir(data_path, pattern = paste0(inlees,".*\\.txt")) # get file names 

    inlees <- files %>% 
    # read in all the files, appending the path before the filename 
    map(~ read_tsv(file.path(data_path, .), skip = 6, col_names = TRUE))%>% 
    reduce(rbind) 

inlees$Date <- dmy(inlees$Date) #convert from character to Date 
}) 

output$p1 <- renderPlot({ 

p1 <- ggplot(data = selectData(), aes(x = Date, y = Ratio)) + geom_point(color = "#1874CD80") + 
    ylab(expression("D0 Ratio")) + theme_bw() + xlab("Date") + 
    theme(axis.title.y = element_text(size = 12),axis.text = element_text(size = 12), 
     axis.title.x = element_text(size = 12), panel.grid.major = element_blank(), 
     axis.line = element_line(colour = "black"), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     panel.background = element_blank()) + 
coord_cartesian(ylim = rangesp1$y) 
     p1 }) 

observeEvent(input$p1_dblclick, { 
brush <- input$p1_brush 
if (!is.null(brush)) { 

    rangesp1$y <- c(brush$ymin, brush$ymax) 

} else { 

    rangesp1$y <- NULL 
} 
}) 

rangesp2 <- reactiveValues(y = NULL) 
output$p2 <- renderPlot({ 


p2 <- ggplot(data = selectData(), aes(x = Date, y = ISArea)) + geom_point(color = "#68228B80") + 
    ylab(expression("IS Area")) + theme_bw() + xlab("Date") + 
    theme(axis.title.y = element_text(size = 12),axis.text = element_text(size = 12), 
     axis.title.x = element_text(size = 12), panel.grid.major = element_blank(), 
     axis.line = element_line(colour = "black"), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     panel.background = element_blank()) + 
    coord_cartesian(ylim = rangesp2$y) 
    p2 
    }) 


    observeEvent(input$p2_dblclick, { 
    brush <- input$p2_brush 
    if (!is.null(brush)) { 

    rangesp2$y <- c(brush$ymin, brush$ymax) 

    } else {  
    rangesp2$y <- NULL 
    } 
    }) 

} 

shinyApp(ui = ui, server = server) 
+0

您正在使用的功能'dmy'它構造類Date對象從一個非標準的包,基本包中的函數是'as.Date':'Sys.setlocale(「LC_TIME」,「C」); as.Date(「23-Nov-16」,「%d-%b - %y「)' – Valentas

+0

謝謝,但即使當我使用庫(lubridate),它仍然不起作用 – Martijnvf

+1

歡迎來到SO。一個更好的帖子不會提閃亮,只包含一個樣本數據集+最小ggplot表達式再現出乎意料的行爲 – HubertL

回答

1

我明確return語句的粉絲,這可能是爲什麼使用的一個很好的例子return()是一個好習慣。

?function幫助頁說:

If the end of a function is reached without calling return, the value of the last evaluated expression is returned.

現在,在反應式的最後一條語句定義了selectData功能是:

inlees$Date <- dmy(inlees$Date) 

所以,最後計算的表達式是的類Date - 不是類data.frame - 被返回,隨後導致ggplot()中的錯誤。

請您reactive()表達添加return(inlees)作爲最後一條語句:

selectData <- reactive ({ 

    data_path <- "ANPR" # path to the data 
    inlees <- input$name 
    files <- dir(data_path, pattern = paste0(inlees,".*\\.txt")) # get file names 

    inlees <- files %>% 
    # read in all the files, appending the path before the filename 
    map(~ read_tsv(file.path(data_path, .), skip = 6, col_names = TRUE))%>% 
    reduce(rbind) 

    inlees$Date <- dmy(inlees$Date) #convert from character to Date 
    return(inlees) 
}) 
0

謝謝您的答覆。非常感謝。從我的第一個問題中學到了很多東西。我試過你的解決方案Uwe,但後來遇到了「ggplot2不知道如何處理列表」的錯誤。你的建議後,我想,我也可以聲明列日,作爲「read_tsv線內的日期,現在一切正常:):

inlees <- files %>% 
    # read in all the files, appending the path before the filename 
    map(~ read_tsv(file.path(data_path, .), skip = 6, col_names = TRUE, 
    col_types = cols(Date = col_date(format = "%d-%b-%y")))) %>% 
    reduce(rbind) 
相關問題