r语言 - 空文本输入()导致传单闪亮应用程序中的错误



我有一个闪亮的应用程序,可以在传单地图上绘制来自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)

您可以验证是否正在获得带有值的tibble,或者是否正在获得空tibble。如果tibble为空,您可以返回NULL。这样,您添加的用于检查APIdata()是否NULL的验证将起作用。

您可以在反应a <- ebirdnotable(region = as.character(input$region_in), back = as.numeric(input$slider_in))后添加if(length(a) == 0){return(NULL)},使其正常工作。

编辑:

正如您在评论中提到的,您实际上可以在此处使用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)}

希望对您有所帮助!

相关内容

  • 没有找到相关文章

最新更新