在反应上下文中使用反应表达式中的(逻辑)向量/尝试应用非函数错误

时间:2018-10-26 19:38:06

标签: r shiny reactive-programming

我正在尝试使用反应式表达式中的逻辑向量。当我尝试在另一个反应式表达式中对此向量执行逻辑运算时,这会在函数xor()中生成错误。我想生成一个反应式表达式(逻辑向量),然后在另一个反应式函数中使用它。下面是一个玩具示例。单击绘图上的点时会出现错误。

在原始的here中,keeprows()不具有响应性,但是我希望使其结构如下图所示(来自Shiny网站)。输入第一个对象用于反应性表达式,然后使用第二个(反应性)对象(用户自定义表)进行点选择等。分叉后的元素是具有保留点和排除点的表。我在使最后一个子集正常工作时遇到问题。

enter image description here

有人可以向我解释这个问题的根源吗?

library(ggplot2)
library(shiny)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    column(width = 6,
           plotOutput("plot1", height = 350,
                      click = "plot1_click",
                      brush = brushOpts(
                        id = "plot1_brush"
                      )
           ),
           actionButton("exclude_toggle", "Toggle points"),
           sliderInput(inputId = "efficiency", value = 20, label = "MPG", min = min(mtcars$mpg), max = max(mtcars$mpg))
    )
  )
)

server <- function(input, output) {
  # For storing which rows have been excluded

  mt_subset <- reactive(mtcars %>% filter(mpg > input$efficiency))

  vals <- reactiveValues()
  vals$keeprows <- reactive(rep(TRUE, nrow(mt_subset()), label = "TuProblem", quoted = FALSE))

  output$plot1 <- renderPlot({
    # Plot the kept and excluded points as two separate data sets
    keep    <- mt_subset()[ vals$keeprows(), , drop = FALSE]
    exclude <- mt_subset()[!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(mt_subset(), input$plot1_click, allRows = TRUE)

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

  # Toggle points that are brushed, when button is clicked
  observeEvent(input$exclude_toggle, {
    res <- brushedPoints(mt_subset(), input$plot1_brush, allRows = TRUE)

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

}

shinyApp(ui, server)

2 个答案:

答案 0 :(得分:0)

我不确定这是否是您要查找的输出,但是此代码将读取本地文件,然后执行刷点选择,在单击“切换点”后将刷点变灰,并进行调整相关性。

*.txt*

答案 1 :(得分:0)

已解决:

library(ggplot2)
library(shiny)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    column(width = 6,
           plotOutput("plot1", height = 350,
                      click = "plot1_click",
                      brush = brushOpts(
                        id = "plot1_brush"
                      )
           ),
           actionButton("exclude_toggle", "Toggle points"),
           sliderInput(inputId = "efficiency", value = 20, label = "MPG", min = min(mtcars$mpg), max = max(mtcars$mpg))
    )
  )
)

server <- function(input, output) {
  mt_subset <- reactive(mtcars %>% filter(mpg > input$efficiency))
  vals <- reactiveValues()
  observeEvent(mt_subset(), {
  vals$keeprows <- rep(TRUE, nrow(mt_subset()), label = "TuProblem", quoted = FALSE)
  })
  output$plot1 <- renderPlot({
    # Plot the kept and excluded points as two separate data sets
    keep    <- mt_subset()[ vals$keeprows, , drop = FALSE]
    exclude <- mt_subset()[!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))
  })
  observeEvent(input$plot1_click, {
    res <- nearPoints(mt_subset(), input$plot1_click, allRows = TRUE)
    vals$keeprows <- xor(vals$keeprows, res$selected_)
  })

  observeEvent(input$exclude_toggle, {
    res <- brushedPoints(mt_subset(), input$plot1_brush, allRows = TRUE)

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

}
shinyApp(ui, server)