【发布时间】:2018-02-21 18:25:25
【问题描述】:
我有一个闪亮的应用程序,我可以在其中读取具有特定格式的文件,该应用程序绘制数据,然后我可以交互式地注释该图。我最近更新了该应用程序,以便它可以考虑列表中目录中的所有文件,然后使用“下一步”按钮我可以在文件之间滑动并一一注释:
filed=list.files()
library(shiny)
ui <- fluidPage(
actionButton("nex","next"),
......
column(6,plotOutput("plot", click = "plot_click1")),
......
column(1,actionButton("submit1","add to list")),
column(6,actionButton("write results to file","write"))
)
server <- function(input, output) {
value= reactiveVal(1)
observeEvent(input$back, {
newValue <- value() - 1 # newValue <- rv$value - 1
value(newValue) # rv$value <- newValue
})
observeEvent(input$nex, {
newValue <- value() + 1 # newValue <- rv$value + 1
value(newValue) # rv$value <- newValue
})
df <- reactive({
f=paste(filed[value()])
df=read.table(f,header=F)
df
})
click_saved1 <- reactiveValues(singleclick = NULL)
observeEvent(eventExpr = input$plot_click1, handlerExpr = { click_saved1$singleclick <- input$plot_click1 })
rv=reactive({
if(input$nex){
m=data.frame(x=0,y=0)
}else{
m=data.frame(x=0,y=0)
}
})
observeEvent(input$submit1, {
if (input$submit1 > 0) {
rv$m <- rbind(rv$m,unlist(click_saved1$singleclick))
}
})
output$plot<- renderPlot({
df=df()
rv=rv()
x<-df$distance
y<-df$frequency
s=ceemdan(y, ensemble_size = 1000)
par(mar=c(5,5,5,5))
plot(x,y)
points(rv$m$x[-1],rv$m$y[-1], pch=16, col="red", cex=1)
})
output$lin=renderPlot({
rv=rv()
x<-df$distance
y<-df$frequency
m=as.data.frame(cbind(x=seq(1,nrow(rv$m)-1),y=rv$m$x[-1]))
fit=lm(y~x, data=m)
plot(m$x,m$y)
abline(fit)
legend("topleft", bty="n", legend=paste("NRL:",round(coef(fit)[-1], digits = 2), "Error:",round(summary(fit)$coefficients[-1 , 2]), digits=2),cex=1.2)
})
observeEvent(eventExpr = input$write, handlerExpr = {
df=df()
rv=rv()
x<-df$distance
y<-df$frequency
write.table(paste(filed[value(),],round(coef(fit)[-1], digits = 2),
round(summary(fit)$coefficients[-1 , 2]),sep='\t'),'NRLs.txt',append = T,quote=F,col.names=F,row.names=F)
})
}
shinyApp(ui, server)
绘图注释涉及在我觉得有趣的点处单击绘图,并且这些点的位置被响应式地提供给数据框:
ui.R
column(1,actionButton("submit1","add to list")),
服务器.R
rv=reactiveValues(m=data.frame(x=0,y=0))
observeEvent(input$submit1, {
if (input$submit1 > 0) {
rv$m <- rbind(rv$m,unlist(click_saved1$singleclick))
}
})
我的问题: 是当我单击“下一步”按钮移动到下一个需要注释的文件时,反应性数据框不会重置为 data.frame(x=0,y=0)。这是需要的,因为每个文件都不同。因此,我试图纠正这一点:
rv=reactiveValues({
input$nex
m=data.frame(x=0,y=0)})
这会导致应用立即崩溃并返回错误:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
我也试过了:
rv=reactive({
if(input$nex){
m=data.frame(x=0,y=0)
}else{
m=data.frame(x=0,y=0)
}
})
observeEvent(input$submit1, {
if (input$submit1 > 0) {
rv$m <- rbind(rv$m,unlist(click_saved1$singleclick))
}
})
然后我在每个服务器代码部分中指定了“rv=rv()”。
在这种情况下,绘图会呈现,但是当我在绘图上选择一个点并单击“添加到列表”按钮时,应用程序崩溃并出现错误:
Warning: Error in seq.default: 'to' must be of length 1
这是一个技术错误,我使用 seq 命令在代码的一部分中创建另一个数据框 - 'm=as.data.frame(cbind(x=seq(1,nrow(rv$m)), y=rv$m$x))'
但是,当我恢复正常并且反应数据框“rv”不依赖于下一个按钮时,这段代码可以正常工作...
我认为通过“添加到列表”按钮向数据框添加点与 rv 对象对“下一步”按钮的依赖相冲突。
谁能帮忙?
【问题讨论】:
标签: shiny