我對交互式切換閃亮應用程序的數據選擇有疑問。我想從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)
嗨,@Maria,非常感謝你的回覆。但是我得到錯誤說'$運算符對原子向量無效'。我不確定是否可以將'reactiveValues()'更改爲'reactive()'。 – Joanna