繁体   English   中英

在R中下载2个子集文件-Shiny

[英]downloading 2 subset files in R - Shiny

我正在为我们的合作伙伴创建一个应用程序,使他们可以上传自己的csv联系人列表,从该文件中随机采样一个组,然后让他们为每个采样组和其余组分别下载csv。 一切似乎都运行良好,并且没有收到任何错误代码,但是当我尝试下载已解析的数据帧时,仅获得完整的原始列表。 我假设这与file参数有关,可能引用了在文件上载期间创建的文件路径,但是我不知道足以验证和/或修改此过程以进行测试。

代码不是很长,并且认为对您来说,复制应用程序可能是最好的选择,因此下面是整个示例(或多或少)

ui.r

library(shiny)
source('server.R')

shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose a CSV file:",
                accept = c('text/csv',
                           'text/comma-separated-values',
                           'text/plain',
                           '.csv')),
      tags$hr(),
      checkboxInput("header", "This file has headers.", FALSE),
      radioButtons("sep", "What kind of separators does this file use?",
                   c(Comma = ',',
                     Semicolon = ';',
                     Tab = '\t'),
                   ','),
      radioButtons('quote', 
                   "Are any of the values in this table surrounded by
                   quotation marks?  i.e. 'Adam Smith'",
                   c("None" = '',
                     "Double Quotes (\" \")" = '"',
                     "Single Quotes (\' \')" = "'"),
                   ''), 

      h3("Sample creation"),

      numericInput("sampleSize", 
                   "How many entries would you like for your sample?",
                   value = 0,
                   step = 1),
      conditionalPanel(
        condition = "output.recommend !== NULL",
        textOutput("recommend"))
    ), 

    mainPanel(
      tabsetPanel(
        tabPanel("Original Table", tableOutput("contents")),
        tabPanel("Sample Group", downloadButton("sampleDL", 
                 "Download this table"), 
                 tableOutput("sampled")),
        tabPanel("Remaining Group", downloadButton("remainDL", 
                 "Download this table"),
                 tableOutput("remains"))
        )
      )
    )
  )
)

server.R

library(shiny)

shinyServer(function(input, output) {

  dataset <- reactive({
    if(is.null(input$file1)){
      return(NULL)
    } else {
      info <- input$file1
      data <- read.csv(info$datapath, header=input$header, 
              sep=input$sep, quote=input$quote)
      entID <- 1:(as.integer(nrow(data)))
      dataset <- data.frame(entID, data)
      cbind(dataset)
      dataset[sample(1:nrow(dataset)),]
      return(dataset)
    }
  })

  sugSample <- function(){
    dataset <- dataset()
    if(is.null(dataset)){
      return(NULL)
    } else {
      size <- nrow(dataset)
      if(size <= 3){
        return(NULL)
      }else {
        sSize <- size * 0.167
        return(as.integer(sSize))
      }
    }
  }

  output$recommend <- renderText({
    sugSample <- sugSample() 
    if(is.null(sugSample)){
      return("There is nothing from which to sample at this time.")
    } else {
      return(paste0("Based on the size of your dataset, 
                    I recommend choosing at least ", 
                    sugSample, 
                    " entries for your sample size."))
    }
 })

  sampleGroup <- reactive({
  sSize <- input$sampleSize  
  if(sSize == 0){
      x <- "there is nothing to display"
      y <- "there is nothing to display"
      z <- "there is nothing to display"
      blank <- data.frame(x,y,z)
      return(blank)
    } else {
      dataset <- dataset()
      oSize <- as.integer(nrow(dataset))
      sampleGroup <- dataset[(sample(1:oSize, sSize, replace = FALSE)),]
      return(data.frame(sampleGroup))
    }
  })

  remainGroup <- reactive({
    if(input$sampleSize == 0){
      x <- "there is nothing to display"
      y <- "there is nothing to display"
      z <- "there is nothing to display"
      blank <- data.frame(x,y,z)
      return(blank)
    } else {
    dataset <- dataset()
    sampleGroup <- sampleGroup()
    remainGroup <- dataset[which(!(dataset$entID %in% sampleGroup$entID)),]
    return(data.frame(remainGroup))
    }
  })

  output$contents <- renderTable({
    dataset <- dataset()
    if(is.null(dataset)){
      x <- 'there is nothing to display'
      y <- 'there is nothing to display'
      z <- 'there is nothing to display'
      blank <- data.frame(x,y,z)
      return(blank)
    } else {
      return(dataset)
    }
    })

  output$sampled <- renderTable({
    sampleGroup <- sampleGroup()
    return(sampleGroup)
  })

  output$sampleDL <- downloadHandler(
    filename = 'sampleGroup.csv',
    content = function(file){
      write.csv(sampleGroup(), file)
    })

  output$remains <- renderTable({
    remainGroup <- remainGroup()
    return(remainGroup)
  })

  output$remainDL <- downloadHandler(
    filename = 'remainingGroup.csv',
    content = function(file){
      write.csv(remainGroup(), file)
    })
})

谢谢!

downloadHandler()无法在RStudio中正常运行,因为进程需要Flash,而RStudio没有。 浏览器中启动了应用程序,并按预期下载了文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM