R語言:創建web界面

1、shiny包

R語言使用shiny包創建web界面。
使用shinydashboard包和shinytheme,美化界面樣式,設置界面主題,提升界面整體水平。
Shiny界面圖庫
shinydashboard
shinytheme界面主題
shinydashboard包創建的基礎界面樣式分為三個板塊:標題,側邊欄,主界面。

shinydashboar基礎界面.png

2、支持交互式圖

shiny包支持recharts包的使用,可以創建界面的交互式圖形

recharts包交互式圖形.png

3、網頁分享shinyapps.io

shinyapps網址
Rstudio推出了shinyshinyapps兩個包,shiny可以幫助我們更快更好的開發一些app,然后shinyapps可以提供了一個免費的云服務器,供我們發布app。

4、NBA球員得分分析

以知乎用R語言分析NBA球員得分分析結果為主題,做web界面展示。

UI界面設置

側邊欄設置兩個選項:datas display和players' score,點擊不同的選項,展示對應的內容。

datas display

datas display界面展現3個數據框:NBA,Away team,Host team。通過show 10/25/50 entries 等控制頁面展示數值的條數;右上角search實現數值的搜索功能(使用DT包實現)。


datas display展現內容.png

players' score

players' score展示主隊、客隊球員得分情況。使用ggplot2包繪制客隊各個球員的得分情況


ggplot形式圖.png

使用recharts包繪制主隊各個球員的得分情況,recarts包實現圖形交互式展現。

圖片.png

library(shiny)
library(shinydashboard)
dashboardPage(
  dashboardHeader(title="NBA score analysis"),#界面標題
 #側邊欄設置 :兩個選擇欄
 dashboardSidebar(
    sidebarMenu(
      menuItem("Datas Display", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Players' Score", tabName = "widgets",icon = icon("th")))
  ),
  #主界面設置
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
              h3("Datas Display"),
              tabsetPanel(id = 'dataset',
                          tabPanel('NBA',DT::dataTableOutput('mytable1')),
                          tabPanel('Away team', DT::dataTableOutput('mytable2')),
                          tabPanel('Host team', DT::dataTableOutput('mytable3')))
      ),
      tabItem(tabName = "widgets",
              h2("Away team"),
              h3("Away team: player's score"),
              plotOutput('plot1'),
              h3("Away team: player's score analysis"),
              eChartOutput('plot2'),
              tabsetPanel(id = 'player',
                          tabPanel('Away team players',DT::dataTableOutput('mytable4')),
                          tabPanel('Host team players', DT::dataTableOutput('mytable5'))
              ),
              h2("Host team"),
              h3("Host team: player's score analysis"),
              eChartOutput('plot3')
      )
    )
  )
)

對應UI數值設置

建立完UI界面后,設置對應的數值,表格,圖形,使web界面變成動態的界面。

function(input, output) {
  output$mytable1 <- DT::renderDataTable({DT::datatable(NBA)})
  output$mytable2 <- DT::renderDataTable({DT::datatable(awayteam)})
  output$mytable3 <- DT::renderDataTable({DT::datatable(hostteam)})
  output$plot1 <- renderPlot({
    par(mar = c(5.1, 4.1, 0, 1))
    ggplot(data = Aplayer)+geom_col(aes(x=playername,y=totscore,fill=playername))+
   geom_hline(aes(yintercept=mean(totscore)),colour="red",linetype="dashed",lwd=1)+
      theme_few()
  })
  output$plot2 <- renderEChart({
    par(mar = c(5.1, 4.1, 0, 1))
    ePie(a,toolbox = FALSE,legend.orient ="vertical",
         legend.x = "right",title ="the away team player's score")
  })
  output$mytable4 <- DT::renderDataTable({DT::datatable(Aplayer)})
  output$mytable5 <- DT::renderDataTable({DT::datatable(Hplayer)})
  output$plot3 <- renderEChart({
    par(mar = c(5.1, 4.1, 0, 1))
    eBar(Hplayer,Hplayer$playername,Hplayer$totscore,
         horiz =TRUE,title ="the host team player's  score",
         ylab.name = "score", ylab.namePosition = "end",
         xlab.name = "play's name", xlab.namePosition = "end",
         ylim =c(0,12),toolbox =FALSE,legend = FALSE)
  })
}

結束語

掙扎了好久終于把界面創建成功了,shiny的初步學習先畫個句號。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容