我正在尝试编写一个闪亮的应用程序,并且对R codin来说相当新。我正在阅读包含3列的.txt文件。一列是日期,另外两列是数字(日期列被读作类字符。文本文件由用户选择的名称选择。服务器比在x轴上绘制日期和数字值在y轴。我还添加了缩放功能。 当未声明日期列时,应用程序正常工作(因此删除行时,日期将绘制为字符)。当Date列被以下代码声明为日期时:
inlees$Date <- dmy(inlees$Date) #convert from character to Date
该应用程序返回错误:
错误:ggplot2不知道如何处理类Date的数据
Snapshot of the text file can be found here 任何帮助表示赞赏。
我使用以下脚本:
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 :(得分:1)
我是明确的return
语句的粉丝,这可能是为什么使用return()
是一个好习惯的好例子。
?function
的帮助页面说:
如果在没有调用return的情况下到达函数的结尾,则返回最后一个计算表达式的值。
现在,定义selectData
函数的反应式表达式中的最后一个语句是:
inlees$Date <- dmy(inlees$Date)
因此,返回最后一个类Date
- 而不是类data.frame
- 的评估表达式,随后导致ggplot()
中的错误。
请在return(inlees)
表达式中添加reactive()
作为最后一条语句:
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)
})
答案 1 :(得分:0)
感谢您的回复。非常感谢。从我的第一个问题中学到了很多东西。我尝试了你的解决方案Uwe,但后来我遇到了“ggplot2不知道如何处理列表”的错误。在您提出建议之后,我想,我也可以将列Date声明为“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)