r - 空 textInput() 导致传单闪亮应用程序出错

Posted

技术标签:

【中文标题】r - 空 textInput() 导致传单闪亮应用程序出错【英文标题】:r - Empty textInput() causing error in leaflet shiny app 【发布时间】:2018-06-08 17:05:10 【问题描述】:

我有一个闪亮的应用程序,可以在传单地图上绘制来自 API 的数据。它还接受来自用户的 2 个输入,这些输入传递给API call

其中之一是文本输入,用于设置API 应为其提取数据的区域。如果用户输入了错误的区域代码或只是删除了代码,闪亮的应用程序会给出错误,然后您必须输入正确的代码并等待应用程序恢复正常。它很笨拙,看起来很糟糕。

我试图通过使用if(is.null(APIdata()))ifelse 来解决它(见下面的代码),但这实际上并没有改变任何东西。我认为它会起作用,我对它只是弹出空传单的想法很好。还有什么我可以做的吗?

错误:

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", , ),

    # 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)

【问题讨论】:

您不想在访问 api 之前验证输入吗?你为什么允许用户提交无效数据? 【参考方案1】:

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

您可以在您的响应式中添加 if(length(a) == 0)return(NULL) 之后的 a &lt;- 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)

希望对你有帮助!

【讨论】:

谢谢!好点子。我已经包含了该行,但它实际上仍然会为某些类型的输入引发错误。例如,当我输入“US-M”(删除“A”)或“US-”时,它会执行我想要的操作并弹出一个空传单。但是如果我删除回“U”,那么它会抛出与premature EOF 之前相同的错误。我刚刚签入了一个空白脚本文档,它的ebirdnotable() 正在打印带有一些输入的确切错误。但它仍然会产生一个 0x0 的小标题。 我可以在这里实现tryCatch() 吗?我以前没用过。 @Heliornis 你是对的,你可以在这里试试。我已经编辑了代码看看。

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

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

r - 在传单上叠加fileInput闪亮

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

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

R 更新 textInput 值的闪亮延迟

带有 r 闪亮 textInput 的 jQuery 掩码插件