下载文件rshiny

下载文件rshiny,r,csv,rstudio,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])

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

server我重新排列了您的代码,并在服务器函数的开头创建了两个reactivevalue

在一个
观测事件中
,您等待直到单击
文件输入
-按钮(输入$GimmeCSV),它读取csv,然后将其分配给反应值
输入文件
。然后,您可以在两个可渲染的输出中直接访问该值,就像您在示例中加载csv两次一样。如果文件不是太大,应该不会有问题,但不是必需的

downloadHandler
超出了
renderable
函数的范围,并采用了用于命名输出文件的输入参数(input$infle)。对于内容,使用第二个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)
库(闪亮)

ui尝试将
downloadHandler
移出
renderable
函数。对于有帮助的引用,但现在我无法访问mydata对象,以将文件从try building
mydata
作为
reactive()
(参见前面链接的示例)写入,然后您可以从
renderable
downloadHandler
中引用它