我有一个闪亮的应用程序,可以在传单地图上绘制来自API的数据。它还需要来自用户的2个输入传递给API call
。
其中一个是文本输入,用于设置API
应该为其提取数据的区域。如果用户键入错误的区域代码或只是删除代码,闪亮的应用程序会出错,然后您必须输入正确的代码并等待应用程序重新回到正轨。它很笨拙而且看起来很糟糕。
我试图通过if(is.null(APIdata()))
使用if{}else{}
来解决这个问题(请参阅下面的代码),但这并没有改变任何内容。我认为它会起作用,而我对它的想法很好,只是弹回一个空的传单。还有什么我可以做的吗?
错误:
Error: parse error: premature EOF
(right here) ------^
和
Warning: Error in : parse error: premature EOF
(right here) ------^
Stack trace (innermost first):
96: parse_string
95: parseJSON
94: fromJSON_string
93: jsonlite::fromJSON
92: ebird_GET
91: ebirdnotable
90: <reactive:APIdata> [/Users/Guest/Desktop/eBirdRarity/app.R#41]
79: APIdata
78: func [/Users/Guest/Desktop/eBirdRarity/app.R#68]
77: origRenderFunc
76: output$myMap
1: runApp
代码:
library(shiny)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(leaflet.extras)
library(rebird)
ui <- bootstrapPage(
theme = shinytheme("superhero"),
# Setting map to full-screen
tags$style(type="text/css", "html, body {width:100%;height:100%}"),
# Initializing leaflet output
leafletOutput("myMap", width="100%", height="100%"),
# Adding title overlayed on leaflet map
absolutePanel(top = 1, left = 50, draggable = T,
titlePanel("eBird Recent Rarities Viewer")),
# Adding slider input overlayed on leaflet map
absolutePanel(bottom = 1, left = 45, draggable = T,
sliderInput("slider_in", "Days Back", min = 1, max = 30, value = 14, round = T)),
# Adding text input overlayed on leaflet map
absolutePanel(top = 1, right = 45, draggable = T,
textInput("region_in", "Region Code", value = "US-MA", placeholder = "US-MA"))
)
server <- function(input, output) {
# Rendering data frame from API with slider input
APIdata <- reactive({
# Initial fetch of data from eBird API
a <- ebirdnotable(region = as.character(input$region_in), back = as.numeric(input$slider_in))
# Changing review status from logical to numeric
cols <- sapply(a, is.logical)
a[,cols] <- lapply(a[,cols], as.numeric)
# Initializing new date column
a["date"] <- format(strptime(a$obsDt, format = "%Y-%m-%d"), "%b %d")
# Initializing new color grouping column
a["group"] <- NA
# Assigning colors by review status
idx<- (a$obsReviewed == 0) # Not reviewed
a$group[idx] <- "white"
idx<- (a$obsReviewed == 1) & (a$obsValid == 1) # Reviewed and accepted
a$group[idx] <- "green"
# Jittering lat/lon points to fix point overlap
a$lat = jitter(a$lat, factor = 3)
# print(a)
return(a)
})
# Leaflet map
output$myMap = renderLeaflet({
if(is.null(APIdata()))
{
# Rendering leaflet map
return(leaflet() %>% addTiles()) %>%
addSearchOSM(options = searchOSMOptions(zoom = 8))
}
else
{
# Splitting up by review status in order to show reviewed on top
notReviewed = APIdata()[APIdata()$group == "white",]
accepted = APIdata()[APIdata()$group == "green",]
# Rendering leaflet map
leaflet() %>% addTiles() %>%
addCircleMarkers(data = notReviewed, color = "white", opacity = 0.7, label = paste(notReviewed$comName,", ",notReviewed$date, ", ", notReviewed$locName,sep = "")) %>% # , labelOptions = labelOptions(noHide = F, direction = 'auto')) %>%
addCircleMarkers(data = accepted, color = "green", opacity = 0.7, label = paste(accepted$comName,", ",accepted$date, ", ", accepted$locName, sep = "")) %>% # , labelOptions = labelOptions(noHide = F, direction = 'auto')) %>%
addLegend(position = "bottomright",
colors = c("#FFFFFF", "#008000"),
labels = c("Not reviewed", "Accepted"),
title = "Legend: review status", opacity = 1) %>%
addSearchOSM(options = searchOSMOptions(zoom = 8))
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
您只需验证您是否获得了包含值的tibble
,或者您是否获得了空tibble
。如果tibble
为空,您可以返回NULL
。这样,您添加以验证APIdata()
是NULL
的验证是否有效。
您只需在if(length(a) == 0){return(NULL)}
之后添加a <- ebirdnotable(region = as.character(input$region_in), back = as.numeric(input$slider_in))
即可让您发挥作用。
修改强>
正如您在评论中提到的,您实际上可以在此使用try
。这意味着除了上述条件之外还要添加一个条件,代码将是这样的:
a <- try(ebirdnotable(region = as.character(input$region_in), back = as.numeric(input$slider_in)))
if(class(a) == "try-error" ||length(a) == 0){return(NULL)}
希望它有所帮助!