我有一个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)
}
任何subestions均被赞赏
我对您的代码进行了重新排列,并在服务器函数开头创建了2个反应值。
在observeEvent
中,您要等到fileInput
-Button( Input $ gimmecsv (单击,然后读取CSV,然后将其分配给反应性值inputFile
。然后,您可以在两个renderTable
输出中直接访问此值,因为您将在示例中加载CSV。如果文件不太大,则不应该是问题,但不是必需的。
downloadHandler
进入renderTable
函数之外,并采用输入参数( input $ infile (,该函数用于命名输出文件。对于内容,使用了第二个反应值,在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)
希望它有帮助。