评论

收藏

[R语言] R语言shiny可视化

编程语言 编程语言 发布于:2021-07-01 23:55 | 阅读数:497 | 评论:0

DSC0000.png
程序代码:
library(shiny)
library(dplyr)
library(plotly)
library(readxl)
library(DT)
# we read and prepare the data in the same way as in a normal script
dataFromBO <-
  read_xlsx(
  path = "Book2.xlsx",
  sheet = "Sheet1"
  )
result <- 
  dataFromBO %>% # filtering step
  filter(
  Basis == "Gross", Maturity == "CUY"
  ) %>%
  group_by(
  Year,
  Maturity,
  Rein,
  CoverType,
  LOB,
  Combined
  ) %>%
  summarise(
  Premium = sum(`Premium`),
  Profit = sum(`MEAN Result`),
  SA_TVAR_99 = -sum(`SA_TVAR_99`),
  DIV_TVAR_99 = -sum(`DIV_TVAR_99`),
  Div_B = 1-sum(`DIV_TVAR_99`)/sum(`SA_TVAR_99`)
  )
EL <-
  read_xlsx(
  path = "Book3.xlsx",
  sheet = "EL"
  )
# user-interface is defined here
# we basically say where each graphic should go
ui <- fluidPage(
  fluidRow(
  column(
    width = 6,
    
    plotlyOutput("bar_chart")
  ),
  
  column(
    width = 6,
    
    plotlyOutput("bubble_chart")
  )
  ),
  
  fluidRow(
  column(
    width = 12,
    
    dataTableOutput("table")
  )
  )
)
# we define the graphics here
server <- function(input, output, session) {
  # this means that output with id "table" will produce a datatable with results
  # it's included in the UI part through dataTableOutput("table")
  output$table <- renderDataTable({
  datatable(result)
  })
  
  output$bar_chart <- renderPlotly({
  EL2 <- 
    EL %>%
    group_by(Return) %>%
    summarise(
    Total = sum(TVaR)
    )
  
  EL3 <- inner_join(EL, EL2, by = "Return")
  
  EL3 %>%
    mutate(
    Contribution = TVaR / Total
    ) %>%
    plot_ly(
    x= ~Return,
    y = ~Contribution,
    name = ~LOB_NAME
    ) %>%
    add_bars() %>%
    layout(
    title = 'Line of Business Marginal Contribution by Return Period',
    
    xaxis =
      list(
      title = "Return Period",
      showgrid = FALSE,
      categoryarray= sort(unique(EL3$Sort_Return)),
      categoryorder = "array"
      ),
    
    yaxis = 
      list(
      title = "Marginal Contribution",
      showgrid = FALSE
      ),
    
    barmode= "stack"
    )
  })
  
  output$bubble_chart <- renderPlotly({
  plot_ly(
    result,
    x = ~DIV_TVAR_99,
    y = ~Div_B,
    text = ~Combined,
    type = 'scatter',
    mode = 'markers', 
    size = ~Premium,
    color = ~Year, 
    colors = "Paired",
    marker = 
    list(
      opacity = 0.5, 
      symbol= "circle",
      sizemode = 'diameter',
      line = list(width = 2, color = "#FFFFFF")
    )
  ) %>%
    layout(
    title = 'SCOR US: 2017 VS 2018 Risk Profile evolution',
    
    xaxis = 
      list(
      title = 'x',
      gridcolor = 'rgb(255, 255, 255)',
      range = c(0, 900000000),
      zerolinewidth = 1,
      ticklen = 10,
      gridwidth = 2
      ),
    
    yaxis = 
      list(
      title = 'Div_Benefit',
      gridcolor = 'rgb(255, 255, 255)',
      range = c(0, 1.2),
      zerolinewidth = 1,
      ticklen = 5,
      gridwith = 2
      ),
    
    showlegend = TRUE
    )
  })
}
# start the app
shinyApp(ui, server)
参考官网资料:https://shiny.rstudio.com/help/

  
关注下面的标签,发现更多相似文章