R语言shiny可视化

33 篇文章 2 订阅
15 篇文章 10 订阅

在这里插入图片描述
程序代码:

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/

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值