2016-12-14 63 views
1

我对交互式切换闪亮应用程序的数据选择有疑问。我想从selectInput中选择数据,但错误说:操作不允许没有活动的反应环境。 (你试图做一些只能从反应式表达或观察者内部完成的事情。)Shiny 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) 

回答

1

最后,我通过保留Vals的交互部分,从reactiveValues()的对象中删除交互部分来解决此问题。

请注意,从reactiveValues对象获取的值是反应性的,但是reactiveValues对象本身不是。

这里是我的应用程序:

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),])) 
}) 

} 
1

我猜你应该首先要,是改变valsvals <- reactive({...}),然后提到它的时候,加括号,例如vals()$keeprows。这应该解决反应性问题。

+0

嗨,@Maria,非常感谢你的回复。但是我得到错误说'$运算符对原子向量无效'。我不确定是否可以将'reactiveValues()'更改为'reactive()'。 – Joanna