我制作了一个闪亮的应用程序,删除了外围的实验文物:
df=data.frame(seq(-1000,1000), rnorm(2001)) #not real data
ui <- basicPage(
plotOutput("plot1", click = "plot_click", brush = "plot_brush"),
verbatimTextOutput("info"),
plotOutput("tab")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
plot(df[,1], df[,2])
})
data_new<-eventReactive(input$plot_brush,{
da=df
rowmin <- which(da[,1] == round(as.numeric(input$plot_brush$xmin)))
rowmax <- which(da[,1] == round(as.numeric(input$plot_brush$xmax)))
da[rowmin:rowmax,2] = mean(da[,2])
da=isolate(da)
#writeToDatabase(da)
})
#output$zoom<-renderable({plot(data_new()[,1], data_new()[,2])})
output$tab=renderPlot({plot(data_new()[,1], data_new()[,2])})
}
shinyApp(ui, server)
这很好但是我不能永久删除这些文物是不方便的,即我想知道是否有任何方法可以使非反应变量的值永久保留所做的更改而不是重新创建原始的错误数据框每一次?
我尝试使用纠正错误数据变量的函数&#39; df&#39;:
change=reactive(function(d){
rowmin <- which(d[,1] == round(as.numeric(input$plot_brush$xmin)))
rowmax <- which(d[,1] == round(as.numeric(input$plot_brush$xmax)))
d[rowmin:rowmax,2] = mean(d[,2])
return(isolate(d))
})
isolate(change(df))
但是我收到以下错误:
Listening on http://127.0.0.1:6183
Warning: Error in change: unused argument (df)
Stack trace (innermost first):
52: change
51: is.data.frame
50: write.table
43: isolate
42: server [/Users/Downloads/test.R#20]
1: runApp
Error in change(df) : unused argument (df)
这是一个初步测试,看看我是否可以动态更新变量。所有这一切都是为了能够连续查看和消除上面显示的数据中的每个尖峰错误峰值,而不是每次在同一个不可变(从闪亮的角度来看)变量上重新运行的代码??
答案 0 :(得分:1)
您可能想要使用reactiveValues
:
server <- function(input, output) {
my <- reactiveValues(df=data.frame(seq(-1000,1000), rnorm(2001))) # Initialize df
output$plot1 <- renderPlot({plot(my$df[,1], my$df[,2])})
observeEvent(input$plot_brush,{
rowmin <- which(my$df[,1] == round(as.numeric(input$plot_brush$xmin)))
rowmax <- which(my$df[,1] == round(as.numeric(input$plot_brush$xmax)))
my$df[rowmin:rowmax,2] <- mean(my$df[,2]) # Update df
})
}