我正在尝试执行一个函数,该函数绘制一些数据,然后在RStudio中播放一些音频(对应于该数据)。然而,音频播放似乎阻止了绘图。为什么会这样呢?
plot.prod.df <- function(df, row) {
# given a row number and a df, plot the prod df for that trial
# create data
print(paste0("plotting production data. trial: ", row))
if(is.null(df$prod[[row]])){
stop("Invalid trial")
}
else {
# get relevant production data
prod.df <- data.frame("onset" = df$prod[[row]]$onset * 1000,
"pitch" = df$prod[[row]]$pitch,
"error" = df$prod[[row]]$error_boolean)
prod.df$error <- as.factor(prod.df$error)
# get target notes
target.notes <- str.mel.to.vector(df$stimuli.pitch[[row]], "-")
# check if there were playbacktimes
print(df$playback.times[[row]])
if (any(is.na(df$playback.times[[row]]))) {
print("no playback times on this trial")
# plot
ggplot(prod.df, aes(x=onset, y=pitch, color = error)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
geom_hline(yintercept = target.notes, color = magma.colors[4], size = 2, alpha = 0.7) +
geom_point() +
scale_color_manual(values=c(magma.colors[1], magma.colors[2]))
}
else {
playback.times <- fromJSON(df$playback.times[[row]])[2:length(fromJSON(df$playback.times[[row]]))] # remove the default original playback
print(playback.times)
# plot
ggplot(prod.df, aes(x=onset, y=pitch, color = error)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
geom_hline(yintercept = target.notes, color = magma.colors[4], size = 2, alpha = 0.7) +
geom_vline(xintercept = playback.times, color = magma.colors[5], size = 2, alpha = 0.7) +
geom_point() +
scale_color_manual(values=c(magma.colors[1], magma.colors[2]))
}
}
}
# set up WavPlayer
sound::setWavPlayer("./sox/play")
play.seq <- function(midi_notes, length_of_each_note) {
#print("playing sequence user heard")
freqs <- midi_to_freq(midi_notes)
sines <- lapply(freqs, sound::Sine, length_of_each_note)
for (i in seq_along(1:length(sines))) {
Sys.sleep(length_of_each_note)
sound::play(cutSampleEnds(sines[[i]])) # cutSampleEnds removes cracks
}
}
play.and.plot.trial <- function(df, row = NULL) {
# if a row isn't specified, randomly select a row
if (is.null(row)) {
sampled_trial <- sample(1:nrow(df), 1)
}
else {
sampled_trial <- row
}
print(paste0("no. trial to plot: ", sampled_trial))
plot.prod.df(df, sampled_trial)
play.seq(str.mel.to.vector(df$stimuli.pitch[[sampled_trial]], "-"), 0.25)
}
play.and.plot.trial(dat)我可以确认,如果我注释掉了下面的一行并运行play.and.plot.trial(dat),它就会显示:
play.seq(str.mel.to.vector(df$stimuli.pitch[[sampled_trial]], "-"), 0.25)我不知道为什么这会阻止阴谋的发生,我想,它只会打印到控制台。
我还尝试在音频播放之前添加Sys.sleep(5),但这没有帮助。
发布于 2020-08-05 17:34:59
TL;DR
print the grob:
play.and.plot.trial <- function(df, row = NULL) {
# ...
print(plot.prod.df(df, sampled_trial))
play.seq(str.mel.to.vector(df$stimuli.pitch[[sampled_trial]], "-"), 0.25)
}基本原理
对ggplot(...) + geom_*(...)的调用实际上不会呈现到图形设备中。呈现是由ggplot2:::print.ggplot完成的。试试这个:
gg <- ggplot(mtcars, aes(mpg, disp)) + geom_path()注意它是如何不绘制到查看窗格的?现在,如果您在控制台上键入gg,它将呈现。这是因为R会试图找出最合适的方法来“打印”这种类型的对象。使用R中的S3方法分派,它使用class(gg)查找print.gg (未找到)或print.ggplot (在ggplot2包中找到)。因为它找到了后者,所以它使用了后者。
(如果两者都没有找到,它将使用print.default。)
这也适用于for循环,例如,您想要制作多个图。
无论如何,答案是显式地呈现它。由于S3方法分派,print(gg)仍然会调用ggplot2:::print.ggplot,因为S3是如何工作的。
(如果您有一个需要呈现的gg对象,但是您没有使用library(ggplot2)加载ggplot2名称空间,那么可以显式地使用ggplot2:::print.ggplot(gg)。或者,您可以显式地使用它,仅仅因为您希望代码是“声明性的”和明确的,因为不知道S3分派的工作方式可能会使print(gg)看起来很神奇。)
https://stackoverflow.com/questions/63270280
复制相似问题