R Shiny - 如何舍入数字,转换为百分比并下载.csv-file

时间:2017-11-07 13:33:41

标签: r csv download shiny rounding

我写了一个闪亮的应用程序,用于搜索和下载一个非常大的数据集。该应用程序可以正常运行,但有些功能无法正常工作:

  • 我尝试了几种添加函数的方法,以便将所选数据下载为.csv文件。所有这些都失败了,我只能下载所有数据而不是显示的数据。
  • 我无法包含一个函数来舍入数据并将某些列显示为百分比而不是数字。 formatRound()中的datatable()函数运行良好,我想使用它,但问题是我无法将其包含在服务器函数中。由于用户应该为他或她的工作获得整数(所有数字也在逗号后面),因此数据只应在显示时四舍五入。如果我能够修复舍入,百分比问题也将得到解决,因为我会使用类似的函数formatPercentage()

我使用mtcars-data做了一个例子,并删除了所有错误或不工作的代码,以便下载和舍入问题。任何暗示我如何解决我的问题将非常感谢!提前谢谢!

EDIT3:由于@Claud H,下面的代码解决了舍入问题。下载函数导出一个名为download的空文件(无文件类型)。你知道错误在哪里吗?

EDIT4:由于@Claud H.解决了问题。我将mt_cars_filtered()[, c(input$results_columns_selected)]更改为mt_cars_filtered()[, input$indicator]。另外,我首先不知道必须打开Web浏览器才能下载数据。

      library(tidyverse)
      library(shiny)
      library(shinythemes)
      library(DT)
      library(ggthemes)

      ui <- fluidPage(

        sidebarLayout(

    sidebarPanel(width=3,
                 h3("title", align = 'center'),

                 checkboxGroupInput("cylinder", "Cylinder", choices = c(4,6), selected = c(4)),

                 checkboxGroupInput('indicator', label = 'Indicators', choices = colnames(mtcars)[1:7],
                                    selected = colnames(mtcars)[c(1:7)]),

                 fluidRow(p(class = 'text-center', downloadButton('download', label = 'Download')))),


    mainPanel(
      tabsetPanel(
        tabPanel('Table',
                 DT::dataTableOutput('results'))

      )
    )
        ))


      server <- function(input, output){

        mtcars_filtered <- reactive({
          mtcars %>%
            filter(cyl %in% input$cylinder)
        })

        # Output Table
        output$results <- DT::renderDataTable({
          columns = input$indicator
          mtcars_filtered()[, columns, drop = FALSE] %>%
      datatable(style = 'bootstrap', selection = list(target = 'column'), options = list(paging = FALSE, dom = 't')) %>%
      formatRound(input$indicator[grep('t', input$indicator)], 2)
        })

        # Download Data
        output$download <- downloadHandler(
          filename = function() { paste('filename', '.csv', sep = '') },
          content = function(file) {
            write.csv(mtcars_filtered()[,input$indicator], file, row.names = FALSE)
    })
      }

      shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

建议从?"%>%"

查看magrittr

另外,请检查SO上的thisthis个答案。

使用这种语法

你的表格应该没问题
output$results <- DT::renderDataTable({
    columns = input$indicator
    mtcars_filtered()[, columns, drop = FALSE] %>% 
      datatable() %>%
      formatCurrency( input your code here) %>%
      formatPercentage( and so on ... )
  }, style = 'bootstrap', options = list(paging = FALSE, dom = 't')) 

另外,我还没有得到关于下载的问题。如果要下载数据 FROM 服务器,请使用downloadHandler()功能。类似的东西:

output$save_data <- downloadHandler(
  filename = function() { paste("filename", '.csv', sep = '') },
  content = function(file) {
    write.csv(mydata(), file, row.names = FALSE)
  })
downloadButton("save_data", "download")

ui.R

编辑:根据您的更改,下载无效,因为您选择了错误的列:没有名为tableId的表,您需要从表中获取列叫results

write.csv(mtcars_filtered()[, c(input$results_columns_selected)], file, row.names = FALSE)

关于舍入问题,您可以使用indicator变量来查看是否选择了列input$indicator %in% c('drat', 'qsec', 'wt'),然后使用子集来仅选择TRUE列,如果有的话:{{ 1}}

<强> EDIT2 似乎我已经理解了你想做的一切。 要根据您的复选框选择formatRound(input$indicator[input$indicator %in% c('drat', 'qsec', 'wt')], 2)函数中的列,请使用downloadHandler变量对其进行过滤:

indicator

否则,如果您想通过鼠标点击从表格中选择它们,请使用mtcars_filtered()[, input$indicator] ,如下所示:

input$results_columns_selected