问题
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