If 语句导致闪亮应用程序中的 downloadhandler() 出现问题
If-statement caused problem to the downloadhandler() in shiny app
我有一个闪亮的应用程序,可以在下面下载 shapefile。在我添加 if else
条件并收到 unexpected token ','
通知之前,该应用程序运行良好。为什么会这样?如果我删除 ,
我得到:
Error in shinysession$registerDownload: argument "content" is missing, with no default
如果我离开它,应用程序根本无法运行。
require(shiny)
require(sp)
require(rgdal)
Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")
runApp(
list(
ui = bootstrapPage(
fileInput('inputdata', 'Input file',accept=c('.csv')),
selectInput("select", label = "Choose a dataset",
choices = c("Tree" , "Crowns"),
selected = "Tree"),
downloadButton('downloadShp', 'DownloadSHP')
),
server = function(input, output) {
createShp <- reactive({
myXY <- input$inputdata
if (is.null(myXY)){
return(NULL)
} else {
xyPoints <- read.table(myXY$datapath, sep=",", header=T)
SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data = xyPoints)
proj4string(SHP) <- CRS("+init=epsg:4326")
return(SHP)
}
})
output$downloadShp <- downloadHandler(
if(input$select=="Tree"){
filename = function() { paste0("shpExport.zip") }, #paste('shpExport.zip',
content = function(file) {
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
writeOGR(createShp(), dsn="shpExport.shp", layer="shpExport", driver="ESRI Shapefile")
zip(zipfile='shpExport.zip', files=Sys.glob("shpExport.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
file.copy("shpExport.zip", file)
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
}
}
else{
filename = function() { paste0("shpExport2.zip") }, #paste('shpExport.zip',
content = function(file) {
if (length(Sys.glob("shpExport2.*"))>0){
file.remove(Sys.glob("shpExport2.*"))
}
writeOGR(createShp(), dsn="shpExport2.shp", layer="shpExport2", driver="ESRI Shapefile")
zip(zipfile='shpExport2.zip', files=Sys.glob("shpExport2.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
file.copy("shpExport2.zip", file)
if (length(Sys.glob("shpExport2.*"))>0){
file.remove(Sys.glob("shpExport2.*"))
}
}
}
)
})
)
downloadHandler
是一个函数,所以它的参数需要是实参。你做不到
downloadHandler(
if (condexpr) {
filename = ...,
content = ...
} else {
filename = ...,
content = ...
}
)
这不是合法的 R 语法。
相反,像
downloadHandler(
filename = if (condexpr) func1 else func2,
content = if (condexpr) ... else ...
)
或
downloadHandler(
filename = function() if (condexpr) expr1 else expr2,
content = function(file) if (condexpr) expr1 else expr2
)
并且由于 ?downloadHandler
状态
Reactive values and functions may be used from this function
对于 filename=
和 content=
参数,您仍然可以使用 (input$select=="Tree")
作为条件。
我有一个闪亮的应用程序,可以在下面下载 shapefile。在我添加 if else
条件并收到 unexpected token ','
通知之前,该应用程序运行良好。为什么会这样?如果我删除 ,
我得到:
Error in shinysession$registerDownload: argument "content" is missing, with no default
如果我离开它,应用程序根本无法运行。
require(shiny)
require(sp)
require(rgdal)
Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")
runApp(
list(
ui = bootstrapPage(
fileInput('inputdata', 'Input file',accept=c('.csv')),
selectInput("select", label = "Choose a dataset",
choices = c("Tree" , "Crowns"),
selected = "Tree"),
downloadButton('downloadShp', 'DownloadSHP')
),
server = function(input, output) {
createShp <- reactive({
myXY <- input$inputdata
if (is.null(myXY)){
return(NULL)
} else {
xyPoints <- read.table(myXY$datapath, sep=",", header=T)
SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data = xyPoints)
proj4string(SHP) <- CRS("+init=epsg:4326")
return(SHP)
}
})
output$downloadShp <- downloadHandler(
if(input$select=="Tree"){
filename = function() { paste0("shpExport.zip") }, #paste('shpExport.zip',
content = function(file) {
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
writeOGR(createShp(), dsn="shpExport.shp", layer="shpExport", driver="ESRI Shapefile")
zip(zipfile='shpExport.zip', files=Sys.glob("shpExport.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
file.copy("shpExport.zip", file)
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
}
}
else{
filename = function() { paste0("shpExport2.zip") }, #paste('shpExport.zip',
content = function(file) {
if (length(Sys.glob("shpExport2.*"))>0){
file.remove(Sys.glob("shpExport2.*"))
}
writeOGR(createShp(), dsn="shpExport2.shp", layer="shpExport2", driver="ESRI Shapefile")
zip(zipfile='shpExport2.zip', files=Sys.glob("shpExport2.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
file.copy("shpExport2.zip", file)
if (length(Sys.glob("shpExport2.*"))>0){
file.remove(Sys.glob("shpExport2.*"))
}
}
}
)
})
)
downloadHandler
是一个函数,所以它的参数需要是实参。你做不到
downloadHandler(
if (condexpr) {
filename = ...,
content = ...
} else {
filename = ...,
content = ...
}
)
这不是合法的 R 语法。
相反,像
downloadHandler(
filename = if (condexpr) func1 else func2,
content = if (condexpr) ... else ...
)
或
downloadHandler(
filename = function() if (condexpr) expr1 else expr2,
content = function(file) if (condexpr) expr1 else expr2
)
并且由于 ?downloadHandler
状态
Reactive values and functions may be used from this function
对于 filename=
和 content=
参数,您仍然可以使用 (input$select=="Tree")
作为条件。