【问题标题】:Download file rshiny下载文件 r 闪亮
【发布时间】:2017-11-03 08:10:17
【问题描述】:

我有一个 rshiny 应用程序,我正在尝试在编辑后从该应用程序下载一个 csv 文件。我在网上按照示例进行操作,但下载按钮不起作用。我知道按钮很好,它直接从 rshiny 文档中复制。我认为错误是服务器端的

server <-function(input,output){# Combine the selected variables into a new data frame

#output$value <- renderPrint({
# str(input$GimmeCSV[2,2])

output$table.preview <- renderTable({

  inFile <- input$GimmeCSV

  preview <- read.csv(inFile$datapath, header=TRUE, sep=",")
  return(head(preview)) # undo Table can be modified 

})

output$table.output <- renderTable({

  inFile <- input$GimmeCSV

  tbl <- read.csv(inFile$datapath, header=TRUE, sep=",")
  #return(head(tbl)) # undo Table can be modified 

  ################^ preview of csv table################
  #look up how to extract the columns  with no name present(something numerical)
  #After that is figured out calculating the rest of those values will be a snap
  #tbl <- read.csv(inFile$datapath, header=TRUE, sep=",")
  #below is temporary file path only used for testing purposes, the above read.csv is the true pooba

  #STANDARDS!!!
  solar <- tbl$solar
  temp  <- tbl$temp
  ws    <- tbl$ws


  #return(head(solar))
  Ktemp <-temp + 273
  ktemp <-temp + 273 
  #lol

  if ((input$Shape) == shape_select[1]){

    Len <- (input$diacone)
    Height <- (input$hgtcone)
    Width <- 0
  } else {if((input$Shape) == shape_select[2]){
    Len <- (input$diasphere)
    Height <- 0
    Width <- 0

  } 
    else 
      Len <- (input$lenprism)
    Height <- (input$hgtprism)
    Width <- (input$widprism)

  }

  r <-Len/2

  #return(r)


  if (Len >=0.037){  # Absorptivity from Luke Miller 
    Abs <- 0.615
  } else {if (Len <= 0.02225 ){Abs <-0.689
  } else Abs <- 0.68 } 
  #works!

  Emm <-0.97

  #TOTALLY ARBITURARY SURFACE AREA VALUES BASED ON WHAT I KNOW!
  ConeA <- (pi*r*r) + (sqrt((Height*Height)+(r*r))) # area of cone

  SphereA <- (4*pi*(r*r)) #area of sphere

  PrismA <- ((2*Len*Width) + (2*Height*Len) +( 2*Height*Width))

  #return(PrismA) 
  #WORKS

  #Deciding which surface area area to calculate

  if ((input$Shape) == shape_select[1]){

    SA <-ConeA
  } else {if((input$Shape) == shape_select[2]){SA <- SphereA} 
    else SA <-PrismA}


  #WOEKS!!!!!!!

  #return(SA)

  #Temporary placeholder values for PSA
  PSA <- SA

  SB <- 5.67E-08 # Stephan Boltzman constant

  eskyclear <- 0.72 + (0.005*temp)

  CC <- 0.5 #Cloud over 0 - 1

  esky <- eskyclear + CC*(1 - eskyclear - (8/ktemp)) #IR emissivity from sky

  Aradsky <- SA/2 #surface area projected to the sky

  Aradground <- SA/2 # surface area projected to the ground

  K3 <- esky^(1/4)
  K2 <- 4 * SB * Emm * (esky)^(3/4)
  K4 <- 4 * SB * Emm
  K5 <- 0.6/(0.5*Len)

  hc <- 0.6


  com1 <- (Abs * solar) + (K2 * (Aradsky/PSA) * K3 * Ktemp^4) + (K4 * (Aradground/PSA) * Ktemp^4) + (K5 * PSA * Ktemp) + (hc*SA*Ktemp) + 2.48*0
  com2 <- (4180 * 0) + ((Ktemp^3) * K2 * (Aradsky/PSA)) + (K4 * (Aradground/PSA) *(Ktemp^3)) + (hc*SA) + (K5*PSA)

  #works!

  Sol <- com1 / com2
  modeltemp <- Sol - 273
  max <- max(modeltemp)
  min <- min(modeltemp)





  mydata <- data.frame( Daily_Temp = temp, Solar_Radiation = solar, Body_Temperature = modeltemp, stringsAsFactors = FALSE)
  return(mydata)


  output$downloadData <- downloadHandler(
    filename = function() { paste(input$inFile, '.csv', sep='') },
    content = function(file) {
      write.csv(mydata, file)
    }
  )

})

  } #app goes here lol

  shinyApp(ui, server)
}

欢迎提出建议

【问题讨论】:

  • 尝试将downloadHandler 移出renderTable 函数。 example供参考
  • 这有帮助,但现在我无法访问 mydata 对象来写入文件
  • 尝试将mydata 构建为reactive()(参见之前链接的示例),然后您可以从renderTabledownloadHandler 引用它

标签: r csv rstudio


【解决方案1】:

我稍微重新排列了您的代码,并在服务器函数的开头创建了 2 个响应值。

observeEvent 中,您等到fileInput-Button (input$GimmeCSV) 被单击,然后它会读取 csv 并将其分配给反应值 inputFile。然后,您可以直接在两个 renderTable 输出中访问此值,因为您将在示例中加载 csv 两次。如果文件不是太大,这应该不是问题,但没有必要。

downloadHandler 超出了renderTable 函数并采用输入参数 (input$inFile),该参数用于命名输出文件。对于内容,使用第二个 reactiveValue,其中数据在 output$table.output 中分配。

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("GimmeCSV", "Upload a CSV", multiple = F),
      textInput("inFile", "Name for Output"),
      downloadLink("downloadData", "Download")
    ),
    mainPanel(
      tableOutput("table.preview"),
      tableOutput("table.output")
    )
  )
)


server <-function(input,output){
  inputFile <- reactiveValues(file = NULL)
  outputFile <- reactiveValues(file=NULL)

  observeEvent(input$GimmeCSV, {
    inFile <- input$GimmeCSV
    data <- read.csv(inFile$datapath, header=TRUE, sep=";")
    inputFile$file <- data
  })

  output$table.preview <- renderTable({
    head(inputFile$file)
  })

  output$table.output <- renderTable({
    tbl <- inputFile$file
    outputFile$file <- tbl
    tbl
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$inFile, '.csv', sep='')
    },
    content = function(file) {
      write.csv(outputFile$file, file)
    }
  )
}

shinyApp(ui, server)

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2019-09-27
    • 2021-09-12
    • 2016-02-26
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多