我有一个可复制的shiny ,我曾try 为情节实现brushedPoints,然而,我只是得到了错误.有没有办法解决这个问题,这样当我用画笔在绘图上单击或 Select 一个点时,连接到它的数据就会显示在表格中?

错误:brushedPoints: 'xvar' ('x') not in names of input

library(shiny)
library(plotly)

dataset <- mtcars

ui <- shinyUI(pageWithSidebar(
  headerPanel("Mtcars"),
  sidebarPanel(sliderInput('sampleSize', 'Sample Size', min=10, max=nrow(dataset),
                           value=min(10, nrow(dataset)), step=5, round=0),
               selectInput('x', 'X', names(dataset)),
               selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
               selectInput('color', 'Color', c('None', names(dataset))),
               checkboxInput('smooth', 'Smooth'),
               selectInput('facet_row', 'Facet Row', c(None='.', names(dataset))),
               selectInput('facet_col', 'Facet Column', c(None='.', names(dataset))),
               hr(),
               checkboxInput("plotly1", "Interactive plot!",value = FALSE, width=140),
               actionButton("plot", "Plot!")
  ),
  mainPanel(
    plotOutput('plot', click = "plot_click",
               hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
               brush = brushOpts(id = "plot_brush")),
    tableOutput("plot_brushedpoints")

  )
))

server<- shinyServer(function(input, output) { 
  dataset <- reactive( { mtcars[sample(nrow(mtcars), input$sampleSize),] }) 
  gragh <- reactive({
    p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
    if (input$color != 'None')
      p <- p + aes_string(color=input$color)
    facets <- paste(input$facet_row, '~', input$facet_col)
    if (facets != '. ~ .')
      p <- p + facet_grid(facets)
    
    if (input$smooth)
      p <- p + geom_smooth()
    
    p
  }) |> bindEvent(input$plot)
  
  output$plot <- renderPlot({
    gragh()
  })
  
  output$plot_brushedpoints <- renderTable({
    df<- mtcars  
    res <- brushedPoints(df, input$plot_brush, "x", "y",allRows = TRUE)
    if (nrow(res) == 0|is.null(res))
      return(NULL)
    res
  })
  
  
  
  
})

推荐答案

不知道你想用"x"和"y"做什么.删除它们可以使其正常工作:

res <- brushedPoints(df, input$plot_brush, allRows = TRUE)

您正在将"x"和"y"放入xvar和yvar中,但这些必须是df中的列名.

brushedPoints(
  df,
  brush,
  xvar = NULL,
  yvar = NULL,
  panelvar1 = NULL,
  panelvar2 = NULL,
  allRows = FALSE
)


xvar, yvar
A string giving the name of the variable on the x or y axis. 
These are only required for base graphics, and must be the name of a column in df.

R相关问答推荐

图片中令人惊讶的行为

R数据帧中的布尔加法会产生布尔值而不是整值

用单个表达匹配多个替代模式

R的GG平行坐标图中的排序变量

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

基于shiny 应用程序中的日期范围子集xts索引

在R中查找每个组不同时间段的总天数

ggplot2中的X轴显示数值,单位为百,而不是十

二维样条,严格以一个参数递增

如果第一个列表中的元素等于第二个列表的元素,则替换为第三个列表的元素

R函数‘paste`正在颠倒其参数的顺序

线性模型斜率在减少原始数据时提供NA

计算两列中满足特定条件连续行之间的平均值

更改STAT_VALLES/STAT_PEAKS中的箭头线宽/大小

您是否可以将组添加到堆叠的柱状图

在多页PDF中以特定布局排列的绘图列表不起作用

汇总数据帧中的复制列,保持行的唯一性

解析嵌套程度极高的地理数据

减少雨云面之间的间距并绘制所有统计数据点

防止正则表达式覆盖以前的语句