首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载文件rshiny

下载文件rshiny
EN

Stack Overflow用户
提问于 2017-06-02 10:50:01
回答 1查看 159关注 0票数 0

我有一个闪亮的应用程序,我正在尝试在编辑后从该应用程序下载csv文件。我在线学习了这些示例,但下载按钮不起作用。我知道这个按钮很好,它是直接从rshiny文档复制过来的。我假设错误是服务器端的

代码语言:javascript
复制
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)
}

如有任何建议,我们将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2018-05-16 06:16:13

我稍微重新整理了一下代码,并在服务器函数的开头创建了2个reactiveValues。

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

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

代码语言:javascript
复制
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)

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44320041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档