r - 在 Shiny 中下载 RenderTable() 数据帧上的处理程序

标签 r download widget shiny

我有一个 Shiny 的应用程序,它采用如下所示的 csv 文件:

category
action

并将其输入到数据框中以仅使用“action”过滤类别。

可复制的应用程序:

用户界面:

library(shiny)
shinyUI(fluidPage(
  titlePanel("Actor Finder"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload Category List: Must have category as header"),
      selectInput("file4", "Select Type", c("A" = "A",
                                               "B" = "B",
                                               "C" = "C"), selected = "A"),
      numericInput("file5", "Choose cost", 1000000000),
      tags$hr()),    
    mainPanel(
      uiOutput("tb")
    )

  )
))

服务器:

library(shiny)
library(readr)
library(dplyr)
# use the below options code if you wish to increase the file input limit, in this example file input limit is increased from 5MB to 9MB
# options(shiny.maxRequestSize = 9*1024^2)

actor <- c('Matt Damon','George Clooney','Brad Pitt', 'Clive Owen', 'Morgan Freeman', 'Edward Norton', 'Adrian Granier')
category<-c('action', 'action', 'noir', 'action', 'thriller', 'noir', 'action')
movie <- c('Oceans Eleven', 'Oceans Twelve', 'Fight Club', 'Children of Men', 'The Shawshank Redemption', 'American History X', 'Entourage')
movies <- c(21, 23, 26, 12, 90, 14, 1)
cost <- c(210000, 2300000, 260000, 120000, 90000, 140000, 10000)
Type <- c('A','B','C', 'A', 'B', 'C', 'A')

moviedata<-data.frame(actor, category, movie, movies, cost, Type)

# This reactive function will take the inputs from UI.R and use them for read.table() to read the data from the file. It returns the dataset in the form of a dataframe.
# file$datapath -> gives the path of the file

shinyServer(function(input,output){
data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read_csv(file=file1$datapath)

})


# this reactive output contains the summary of the dataset and display the summary in table format

# this reactive output contains the summary of the dataset and display the summary in table format
output$sum <- renderTable({
  if(is.null(data())){return ()}
  test<-subset(moviedata, category %in% data())
  test1<-filter(test, `Type`==input$file4)
  test1$`BUDGET`<-input$file5
  test1$CHECKING<-ifelse(test1$`BUDGET`>test1$cost,"YES", "NO")
  filter(test1, CHECKING=="YES")
})

 # the following renderUI is used to dynamically generate the tabsets when the file is loaded. Until the file is loaded, app will not show the tabset.
output$tb <- renderUI({
  if(is.null(data()))
    h5("Powered by", tags$img(src='optimatic.png'))
  else
    tabsetPanel(tabPanel("Summary", tableOutput("sum")))
})

} 
)

我希望能够执行以下操作:

输出如下所示:

enter image description here

1) 将下载按钮添加到 renderTable 数据框中。假设我在 UI 中更改了新输出的过滤器,然后我希望能够下载该新输出。

2) 将 DT 包中的数据帧格式更改为数据表。

任何帮助都会很棒,谢谢!

最佳答案

使用 DT 非常简单:

1 - 加载包:

library(DT)

2-通过将 renderTable 替换为 renderDataTable 来渲染表格(指定 extension 参数以包含下载按钮):

output$sum <- renderTable({...}, 
  extensions = c('Buttons'), 
  options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
  ))

3-在renderUI中使用dataTableOutput而不是tableOutput输出结果:

tabsetPanel(tabPanel("Summary", dataTableOutput("sum")))

关于r - 在 Shiny 中下载 RenderTable() 数据帧上的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41939805/

相关文章:

r - 从 R 中的 Twitter 状态中提取用户

sockets - ClientAbortException : java.net.SocketException:软件导致连接中止:套接字写入错误

javascript - 通过 jQuery 动态插入 &lt;script&gt; 标签

c# - 刚开始使用 Silverlight,我可以从主 Web 应用程序页面向我的嵌入式 silverlight "widget"发送数据吗?

r - 如何对data.frame的每个obs的值进行排序?

r - 传递给 map() 的列的访问名称

r - knitr 运行时需要 Rmd 的文件名

Android Glide 下载前显示

android - 清除 android 下载列表

java - 如何滚动 android 小部件中的所有 TextView ?