【问题标题】:Shiny resetting and updating reactiveValues dataframe with two different buttons闪亮的重置和更新 reactiveValues 数据框有两个不同的按钮
【发布时间】: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


    【解决方案1】:

    如果您需要在每次单击按钮时重置响应式数据框/对象,可以通过以下方式实现:

      rv=reactiveValues(m=data.frame(x=0,y=0))
    
      observe({ if (input$button == 0) 
        return()
        rv$m <- data.frame(x=0,y=0)
      })
    

    因此,每次单击绘图并单击“添加到列表”按钮时,都会为数据框提供一个新行。当点击下一步时,数据框会被重置。

    ui.R

      plotOutput("plot", click = "plot_click1")
    

    服务器.R

      click_saved1 <- reactiveValues(singleclick = NULL)
      observeEvent(eventExpr = input$plot_click1, handlerExpr = { click_saved1$singleclick <- input$plot_click1 })
    
      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))
        }
      })
    
      observe({ if (input$button == 0) 
        return()
        rv$m <- data.frame(x=0,y=0)
      })
    

    【讨论】:

    • 看起来闪亮的社区在 StackOverflow 上不是很活跃。
    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2021-09-16
    • 2017-03-07
    • 2016-05-04
    • 2021-01-26
    • 2019-06-18
    • 2021-07-12
    相关资源
    最近更新 更多