我想編碼一個閃亮的應用程序,並相當新的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)
您正在使用的功能'dmy'它構造類Date對象從一個非標準的包,基本包中的函數是'as.Date':'Sys.setlocale(「LC_TIME」,「C」); as.Date(「23-Nov-16」,「%d-%b - %y「)' – Valentas
謝謝,但即使當我使用庫(lubridate),它仍然不起作用 – Martijnvf
歡迎來到SO。一個更好的帖子不會提閃亮,只包含一個樣本數據集+最小ggplot表達式再現出乎意料的行爲 – HubertL