R Shiny dataTableOutput not displaying Brushed Points

◇◆丶佛笑我妖孽 提交于 2019-12-08 09:48:21

问题


I'm developing an R Shiny dashboard using the Iris data in the ggplot2 dataset. It has three main components, a sidebar panel to select the variables to display in the plot, a ggplot with brushed points and a datatable below which shows the data of the brushed points. Everything works perfectly except the datatable does not seem to select the data points on the table. From what I can tell it has something to do with the line output$plot_brushed_points in the server. Any help would be greatly appreciated!

library(shiny)
library(ggplot2)

useri <- shinyUI(pageWithSidebar(
headerPanel("Reactive Plot"),
sidebarPanel(
selectInput('x','X-Axis',names(iris)),
selectInput('y','Y-Axis',names(iris)),
selectInput('color','Color',c('None',names(iris[5])))),
mainPanel(uiOutput("plotui"),dataTableOutput("plot_brushed_points"))))

serveri <- shinyServer(function(input,output) {
output$plot <- renderPlot({
p <- ggplot(iris,aes_string(x=input$x, y=input$y))+geom_point()+theme_bw()
if(input$color != 'None')
  p <- p + aes_string(color=input$color)
print(p)
})
output$plotui <- renderUI(plotOutput("plot",brush = brushOpts("plot_brush")))
output$plot_brushed_points <- renderDataTable(brushedPoints(iris,input$plot_brush,input$x,input$y), options=list(searching=FALSE, paging = FALSE))
})

shinyApp(useri, serveri)

I should note that the data table displays and you can see it refresh, it just doesn't fill with any data.

EDIT

The script above has a feature where it displays all the values in the data table if and only if you select the whole area of the plot.


回答1:


I was having the exact same issue, except I couldn't get the output to render even when all points were selected:

output$dt <- renderDT(
  expr = brushedPoints(acled_selected(), input$mapbrush), 
  options = list(lengthChange = FALSE, rownames=FALSE)
  )

I found a solution here: R Shiny does not display the output data table

This seems to work for me:

  brushd <- reactive({
    user_brush <- input$mapbrush
    brushedPoints(acled_selected(), user_brush, xvar = "LONGITUDE", yvar = 
"LATITUDE")
    })

  output$dt<-DT::renderDataTable({DT::datatable(brushd())})



回答2:


I had a similar problem. I fixed it eventually (after reading this post: https://github.com/rstudio/shiny/issues/998) by NOT calling print on the plot, just returning it. So:

output$plot <- renderPlot({
p <- ggplot(iris,aes_string(x=input$x, y=input$y))+geom_point()+theme_bw()
if(input$color != 'None')
p <- p + aes_string(color=input$color)
#print (p)
p
})


来源:https://stackoverflow.com/questions/31445367/r-shiny-datatableoutput-not-displaying-brushed-points

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!