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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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会起作用。

您可以在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)}

希望能帮助到你!

以上是关于r - 空文本输入()导致传单闪亮应用程序中的错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在闪亮的 R 应用程序中使用传单添加控制输入?

R闪亮的导入传单html小部件对象

r - 在传单上叠加fileInput闪亮

R传单中的标记鼠标单击事件以实现闪亮

以编程方式触发 R 传单中的标记鼠标单击事件以实现闪亮

如何在 R 的传单地图中删除属性