简体   繁体   English

闪亮的R:具有“输入”数据选择的交互式切换输出

[英]Shiny R: Interactive toggle output with `input` data selection

I have a question about the data selection of interactive toggle shiny app. 我对交互式切换闪亮应用程序的数据选择有疑问。 I would like to make the data selected from selectInput but the error say: Operation not allowed without an active reactive context. 我想从selectInput选择数据,但错误提示:如果没有活动的反应上下文,则不允许进行操作。 (You tried to do something that can only be done from inside a reactive expression or observer.) (您试图做只能从反应式表达式或观察器内部完成的操作。)

Is there any way to make the data interactive with the input? 有什么方法可以使数据与输入交互?

Thank you! 谢谢!

Here is my app: 这是我的应用程序:

app.r: app.r:

ui <- fluidPage(
fluidRow(
column(width = 6,
       selectInput("vsselection", "Choose a vs:", 
                   choices = names(table(data.frame(mtcars$vs))),selected=0),
       plotOutput("plot1", height = 350,
                  click = "plot1_click",
                  brush = brushOpts(
                    id = "plot1_brush"
                  )
       ),
       actionButton("exclude_toggle", "Toggle points"),
       actionButton("exclude_reset", "Reset")
  )
 )
)

server <- function(input, output) {
# For storing which rows have been excluded
vals <- reactiveValues(
keeprows = rep(TRUE, nrow(mtcars[which(mtcars$vs==input$vsselection),]))
)

output$plot1 <- renderPlot({
# Plot the kept and excluded points as two separate data sets
keep    <- mtcars[which(mtcars$vs==input$vsselection),][ vals$keeprows, ,  drop = FALSE]
exclude <- mtcars[which(mtcars$vs==input$vsselection),][!vals$keeprows, , drop = FALSE]

 ggplot(keep, aes(wt, mpg)) + geom_point() +
  geom_smooth(method = lm, fullrange = TRUE, color = "black") +
  geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) +
  coord_cartesian(xlim = c(1.5, 5.5), ylim = c(5,35))
 })

# Toggle points that are clicked
observeEvent(input$plot1_click, {
res <- nearPoints(mtcars[which(mtcars$vs==input$vsselection),],  input$plot1_click, allRows = TRUE)

vals$keeprows <- xor(vals$keeprows, res$selected_)
})

# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(mtcars[which(mtcars$vs==input$vsselection),],  input$plot1_brush, allRows = TRUE)

vals$keeprows <- xor(vals$keeprows, res$selected_)
})

# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE,  nrow(mtcars[which(mtcars$vs==input$vsselection),]))
})

}

shinyApp(ui, server)

I guess the first thing you should to, is to change vals to vals <- reactive({...}) and then when referring to it, add parenthesis, eg vals()$keeprows . 我想您应该做的第一件事是将vals更改为vals <- reactive({...}) vals()$keeprows vals <- reactive({...}) ,然后在引用它时添加括号,例如vals()$keeprows This should solve the reactivity problem. 这样可以解决反应性问题。

Finally, I solved this issue by removing the interactive part from the object of reactiveValues() by keep the interactive part of Vals . 最后,我通过保留Vals的交互部分,通过从reactiveValues()对象中删除了交互部分来解决了这个问题。

Note that values taken from the reactiveValues object are reactive, but the reactiveValues object itself is not. 请注意,从reactiveValues对象获取的值是反应性的,但reactiveValues对象本身不是。

Here is my app: 这是我的应用程序:

app.r: app.r:

ui <- fluidPage(
fluidRow(
column(width = 6,
       selectInput("vsselection", "Choose a vs:", 
                   choices =   names(table(data.frame(mtcars$vs))),selected=0),
       plotOutput("plot1", height = 350,
                  click = "plot1_click",
                  brush = brushOpts(
                    id = "plot1_brush"
                  )
       ),
       actionButton("exclude_toggle", "Toggle points"),
       actionButton("exclude_reset", "Reset")
  )
)
)

server <- function(input, output) {
# For storing which rows have been excluded
vals <- reactiveValues(
keeprows = rep(TRUE, nrow(mtcars))
)

output$plot1 <- renderPlot({
# Plot the kept and excluded points as two separate data sets
keep    <- mtcars[which(mtcars$vs==input$vsselection),][ vals$keeprows, ,   drop = FALSE]
exclude <- mtcars[which(mtcars$vs==input$vsselection),][!vals$keeprows, , drop = FALSE]

 ggplot(keep, aes(wt, mpg)) + geom_point(color = "blue") +
  geom_smooth(method = lm, fullrange = TRUE, color = "black") +
  geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha =  0.25) 
})

# Toggle points that are clicked
observeEvent(input$plot1_click, {
res <- nearPoints(mtcars[which(mtcars$vs==input$vsselection),],  input$plot1_click, allRows = TRUE)

vals$keeprows <- xor(vals$keeprows, res$selected_)
})

# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(mtcars[which(mtcars$vs==input$vsselection),],  input$plot1_brush, allRows = TRUE)

vals$keeprows <- xor(vals$keeprows, res$selected_)
})

# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE,   nrow(mtcars[which(mtcars$vs==input$vsselection),]))
})

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM