2017-03-03 9 views
0

私は光沢のあるアプリをコーディングしようとしており、かなり新しいコードをコーディングしようとしています。私は3つの列を含む.txtファイルを読み込んでいます。 1つの列は日付、2つは数値です(日付列はクラス文字として読み込まれます)。テキストファイルはユーザーが選択した名前で選択されます。サーバーはx軸に日付をプロットし、また、ズーム機能を追加しました 日付列が宣言されていないときにアプリケーションが正常に動作します(行が削除されたときに日付が文字としてプロットされます)。コード: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 任意の助けIを感謝しています。

私は次のスクリプト使用しています:

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

を非標準パッケージです。基本パッケージの関数は 'as.Date'です:' Sys.setlocale( "LC_TIME"、 "C"); as.Date( "23-Nov-16"、 "%d-%b - %y ")' – Valentas

+0

ありがとうございますが、ライブラリ(lubridate)を使用しても、それでも動作しません – Martijnvf

+1

ようこそ、SOLへようこそよろしくお願いします。サンプルデータセット+最小限の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ライン内側の日として、列の日付を宣言することができますし、今ではすべてが動作します:あなたがからクラスDateのオブジェクトを構築dmy`機能を `使用している

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) 
関連する問題