我有一个可复制的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
})
})