我正在尝试编写一个小应用程序,允许用户制作散点图,在图上选择点的子集,然后输出.csv格式的表,只有那些选定的点.我想出了如何启动和运行页面以及如何使用brushedPoints选择点.出现带有所选点的表格,但是当我按下"下载"按钮时,出现错误"不允许从shinyoutput对象中读取对象".出现.我无法下载可以在屏幕上以.csv直观显示的表格吗?如果是这样,有解决方法吗?
我使用下面的虹膜数据集重新创建了这个问题.任何帮助搞清楚为什么我无法下载显示的行表将非常感激.
data(iris)
ui <- basicPage(
plotOutput("plot1", brush = "plot_brush"),
verbatimTextOutput("info"),mainPanel(downloadButton('downloadData', 'Download'))
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(iris,aes(x=Sepal.Width,y=Sepal.Length)) +
geom_point(aes(color=factor(Species))) +
theme_bw()
})
output$info <- renderPrint({
brushedPoints(iris, input$plot_brush, xvar = "Sepal.Width", yvar = "Sepal.Length")
})
output$downloadData <- downloadHandler(
filename = function() {
paste('SelectedRows', '.csv', sep='') },
content = function(file) {
write.csv(output$info, file)
}
)
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud) DT包允许您使用来获取选定行的索引input$tableID_rows_selected。这对于没有过滤数据的表非常有用。但是,如果我们有一个经过过滤的数据集,则由于行索引已关闭,因此无法使用相同的方法。
那么,对于过滤后的数据集,我们如何在数据表的选定行中获取数据?
在下面,我发布了一个基本的闪亮应用程序,其中显示了四个表:第一个是原始mtcars数据集,第二个是在第一个中获取选定的行。第三和第四做相同的事情,但是在“过滤器” sliderInput上过滤了数据集之后。
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
DT::dataTableOutput("origTable"),
DT::dataTableOutput("origTableSelected"),
sliderInput("filter", label = "Filter by cyl", min = 4, max = 8, step = 2, value = 6),
DT::dataTableOutput("filteredTable"),
DT::dataTableOutput("filteredTableSelected")
)
server <- function(input, output, session) {
output$origTable <- DT::renderDataTable({
datatable(
mtcars,
selection = list(mode = "multiple"),
caption = "Original Data"
)
})
origTable_selected <- reactive({
ids <- input$origTable_rows_selected
mtcars[ids,]
})
output$origTableSelected <- DT::renderDataTable({
datatable(
origTable_selected(),
selection = list(mode = "multiple"),
caption = "Selected Rows from …Run Code Online (Sandbox Code Playgroud)