将在线 xml 数据加载到 R 闪亮仪表板中的滑块

Loading online xml data to slider in R shiny dashboard

提问人:ronzenith 提问时间:10/1/2021 更新时间:10/3/2021 访问量:121

问:

我和我的朋友使用下载的数据构建了一个 R 闪亮的仪表板。代码如下:

library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)



ecd <- read.csv("ecd-figures.csv")

c(
  "No of case" = "no_of_case",
  "Minor Case" = "minor_case",
  "All Non Fatal Case" = "all_non_fatal_case",
  "Fatal Case" = "fatal_case"
) -> vec

ui <- fluidPage(sidebarLayout(
  sidebarPanel
  (
    checkboxGroupInput("feature",
                       "Feature",
                       vec),
    sliderInput(
      "year",
      "Year",
      min = min(ecd$year),
      max = max(ecd$year),
      value = range(ecd$year),
      sep = "",
      step = 1
    )
  ),
  
  mainPanel(tabsetPanel(
    tabPanel("Plot", plotOutput("correlation_plot")),
    tabPanel("Table", tableOutput("ecd"))
  ))
))



server <- function(input, output) {
  yearrange <- reactive({
    ecd %>%
      subset(year %in% input$year[1]:input$year[2]) %>%
      select(c(year, input$feature))
  })
  
  
  
  output$correlation_plot <- renderPlot({
    ecdsubset <- yearrange()
    
    ecdsubset <- melt(ecdsubset, id = "year")
    validate(need(input$feature, 'Check at least one item.'))
    ggplot(ecdsubset, aes(x = year, y = value, color = variable)) + geom_line(size = 1) + scale_x_continuous(breaks =
                                                                                                               seq(input$year[1], input$year[2], by = 1))
    
  })
  output$ecd <- renderTable({
    yearrange()
  })
  
}

shinyApp(ui, server)

简单的数据文件在这里:https://drive.google.com/file/d/1pZQe89wxw14lirW2mRIgi9h29yPyc7Fs/view?usp=sharing

然后,我想让一切都在线,即调用 api 而不是下载 csv 文件。似乎可以相当简单地阅读内容,如下所示:

library(xml2)

ecd_xml <-"https://www.labour.gov.hk/datagovhk/resource/ecd/ecd-figures.xml"
read_ecd <- read_xml(ecd_xml)
xml_find_all(read_ecd, ".//year")

{xml_nodeset (5)}
[1] <year>2015</year>
[2] <year>2016</year>
[3] <year>2017</year>
[4] <year>2018</year>
[5] <year>2019</year>

问题是:如何将 xml 内容中的每一条信息解析到仪表板上?

以滑块为例。如何通过解析标签并选择最大值和最小值来显示滑块标签(即 2015 和 2019)?<year>

而且,您能为我推荐一些阅读材料来学习从 xml 到仪表板的整个过程吗?非常感谢提前。

(附言我尝试使用包来代替,因为有一些标准参数可以查找属性的最大值、最小值和平均值。但是我遇到了另一个大错误——xml

Error in function (type, msg, asError = TRUE)  : 
  error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

我该怎么办?我的 R 版本是 4.0.5 (2021-03-31)。

R XML 闪亮的 rcurl xml2

评论


答:

1赞 danlooo 10/1/2021 #1

您可以使用从 XML 树创建 tibble。 此外,您可以使用更新 UI 滑块范围:xml2::as_listshiny::updateSliderInput

library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
library(xml2)

vec <- c(
  "No of case" = "no_of_case",
  "Minor Case" = "minor_case",
  "All Non Fatal Case" = "all_non_fatal_case",
  "Fatal Case" = "fatal_case"
)

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    checkboxGroupInput(
      "feature",
      "Feature",
      vec
    ),
    sliderInput(
      "year",
      "Year",
      min = 2000,
      max = 2001,
      value = c(2000, 2001),
      sep = "",
      step = 1
    )
  ),
  mainPanel(tabsetPanel(
    tabPanel("Plot", plotOutput("correlation_plot")),
    tabPanel("Table", tableOutput("table"))
  ))
))



server <- function(input, output, session) {
  xml <-
    "https://www.labour.gov.hk/datagovhk/resource/ecd/ecd-figures.xml" %>%
    read_xml()
  
  table <-
    xml %>%
    xml_find_all(".//item") %>%
    map(as_list) %>%
    map(~ .x %>% as_tibble()) %>%
    bind_rows() %>%
    unnest(everything()) %>%
    type_convert()
  
  years <- table$year %>% unique()
  
  updateSliderInput(
    session = session,
    inputId = "year",
    min = min(years),
    max = max(years),
    value = c(min(years), max(years)) # current selected range
  )
  
  sub_table <- reactive({
    table %>%
      filter(year %in% input$year[1]:input$year[2]) %>%
      select(c(year, input$feature))
  })
  
  
  output$correlation_plot <- renderPlot({
    validate(need(input$feature, "Check at least one item."))
    
    sub_table() %>%
      pivot_longer(-year) %>%
      ggplot(aes(x = year, y = value, color = name)) +
      geom_line(size = 1) +
      scale_x_continuous(
        breaks = seq(input$year[1], input$year[2], by = 1)
      )
  })
  output$table <- renderTable({
    sub_table()
  })
}

shinyApp(ui, server)

enter image description here

评论

0赞 ronzenith 10/1/2021
谢谢。让我试试。我应该将所有标签更改为 ,例如 --> 和 into 吗?ecdxml_yearstabPanel("Table", tableOutput("ecd"))tabPanel("Table", tableOutput("xml_years"))yearrange <- reactive({ ecd %>%yearrange <- reactive({ xml_years %>%
0赞 danlooo 10/1/2021
不。任何以 结尾的函数的第一个参数是 which 不得链接到类型不同的对象(此处:数值向量)。我用它作为前缀,以免将其与 混淆。我建议重命名为 并重命名为 .然后你就不再需要副本了。OutputoutputIdcharacterxml_yearrangexml_yearsecd_yearsyearrangeecd_subsetecdsubset
0赞 ronzenith 10/1/2021
明白了。但是,如果我想不再依赖 ecd(即 csv),似乎应该替换所有 ecd$ 和 ecd%>%。
0赞 danlooo 10/1/2021
@ronzenith我修改了我的答案以使其取决于 csv 文件
1赞 danlooo 10/3/2021
是的。 将一列映射到一种美学,如 x、y、颜色等。您希望按变量名称着色。变量名称位于多列中,因此您必须从宽格式到长格式。然后我们有一个名为它的列,我们可以将其映射到颜色。ggplotsub_tablename