下载文件rshiny

时间:2017-06-02 02:50:01

标签: r csv rstudio

我有一个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)
}

赞赏任何sugestions

1 个答案:

答案 0 :(得分:0)

我重新安排了你的代码,并在服务器函数的开头创建了2个reactiveValues。

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

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

output$table.output

希望它有所帮助。