R----Shiny包紹介学習

12392 ワード

どうしてソニーを使うの
Shinyはデータアナリストに分析と可視化コードを書き終えさせた後、あと数十分で分析コードをエンジニアリングし、分析成果をインタラクティブなWebページに迅速に変換して他の人に共有することができます.したがって、Rを使用するデータアナリストであれば、Shinyを選択するのは賢明です.新しいスキルを必要とせず、開発が速すぎるからです.それは通常私たちが知っている他のフレームワークとは違います.他のフレームワークは一般的に前後端が分離され、後端はjsonを提供し、フロントエンドはjson図面に基づいて表を描き、いくつかのプログラマーが協力して開発を完了する必要があります.しかし、このような可視化されたツールは、研究開発リソースのサポートを受けることができず、本データアナリスト一人で前後のパッケージを操作するしかありません.
#########################

#########################
library(shiny)
library(shinyjs)
library(DT)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
library(scales)
library(plotly)

run.sql function(sql, debug=FALSE) {
  if(debug==FALSE){ df #       ,        ,  SQL   } else{ #      group_id 1, nrow(economics)) dt as.character(economics$date), "00:00:00") df return(df) } ui "           "), #     :SQL       div(id="download", fluidRow( column(12, textOutput(outputId="download_info") ) ), fluidRow( column(12, HTML( paste('') ) ) ), fluidRow( column(12, actionButton(inputId="refresh_button", label="    ", icon=icon("submit") ) ) ) ), shinyjs::hidden( div(id="table", #     :SQL         hr(), dataTableOutput(outputId="sql_tab"), #     :        hr(), textOutput(outputId="tab_button_message"), sidebarLayout( div(id="table_tool", sidebarPanel( selectInput(inputId="group_fields", label="      ", choices=NULL, selected=NULL, multiple=TRUE), selectInput(inputId="x_field", label="  x   ,       ", choices=NULL, selected=NULL, multiple=FALSE), selectInput(inputId="y_line_fields", label="  y     ", choices=NULL, selected=NULL, multiple=TRUE), selectInput(inputId="y_point_fields", label="  y     ", choices=NULL, selected=NULL, multiple=TRUE), selectInput(inputId="group_shape_field", label="        ", choices=NULL, selected=NULL, multiple=FALSE), actionButton(inputId="tab_button", label="      ", icon=icon("submit")), width=3 ) ), div(id="group_content", mainPanel(dataTableOutput(outputId="group_tab"), width=9 ) ) ) ) ), #     :      shinyjs::hidden( div(id = "plot", hr(), plotlyOutput(outputId="case_viewer", height="600px") ) ) ) server function(input, output, session) { observe({ #   SQL    if(is.null(input$sql_cmd) | input$sql_cmd == "") { shinyjs::disable("refresh_button") } else{ shinyjs::enable("refresh_button") } #           if (input$x_field == "" | (is.null(input$y_line_fields) & is.null(input$y_point_fields)) | is.null(input$group_fields)) { shinyjs::disable("tab_button") } else { shinyjs::enable("tab_button") } }) #   SQL       sql_data $refresh_button, { cat(file=stderr(), "#### event log ####: refresh button clicked
") shinyjs::disable("refresh_button") shinyjs::hide(id = "table", anim = TRUE) shinyjs::hide(id = "plot", anim = TRUE) res $sql_cmd, debug=TRUE) updateSelectInput(session, inputId="group_fields", choices=colnames(res)) updateSelectInput(session, inputId="x_field", choices=colnames(res)) updateSelectInput(session, inputId="y_line_fields", choices=colnames(res)) updateSelectInput(session, inputId="y_point_fields", choices=colnames(res)) updateSelectInput(session, inputId="group_shape_field", choices=c(" ",colnames(res)), selected=" ") shinyjs::enable("refresh_button") shinyjs::show(id = "table", anim = TRUE) shinyjs::hide(id = "group_content", anim = FALSE) return(res) }) # SQL output$download_info if(input$refresh_button == 0){ message " SQL select , " } else{ message " ! ", nrow(sql_data()), ", ", ncol(sql_data()), ", ", as.character(lubridate::now(), format="%Y-%m-%d %H:%M:%S")) }) } message }) # SQL output$sql_tab 'top', selection='single') }) # group_data $tab_button, { cat(file=stderr(), "#### event log ####: tab button clicked
") res % select(one_of(input$group_fields)) %>% distinct() shinyjs::show(id="group_content", anim=TRUE) return(res) }) output$tab_button_message if(input$tab_button == 0) { message " ; , ; , " } else { message " ", nrow(group_data()), ", ", as.character(lubridate::now(), format="%Y-%m-%d %H:%M:%S")) }) } message }) # output$group_tab 'top', selection='single') }) # observeEvent(input$group_tab_rows_selected, { cat(file=stderr(), paste0("#### event log ####: group table row ", input$group_tab_rows_selected, " clicked
")) output$case_viewer $group_tab_row_last_clicked cat(file=stderr(), "#### event log ####: table row", s, "clicked
") p $group_fields], collapse="_")}) # _ unite target_plot_data % unite_("new_var", input$group_fields, remove=FALSE) %>% filter(new_var==filter_str) if(length(input$y_line_fields) > 0) { target_plot_data$dt $x_field], tz="UTC-8") line_df % tidyr::gather(col_name, thresh, one_of(input$y_line_fields)) %>% dplyr::mutate(thresh=as.numeric(thresh)) p if(length(input$y_point_fields) > 0) { target_plot_data$dt $x_field], tz="UTC-8") point_df % tidyr::gather(col_name, thresh, one_of(input$y_point_fields)) %>% dplyr::mutate(thresh=as.numeric(thresh)) if(input$group_shape_field != " ") { point_df[, input$group_shape_field] as.factor(point_df[, input$group_shape_field]) p "dt",y="thresh",color="col_name", shape=input$group_shape_field)) } else{ p "plot", anim = TRUE) }) } shinyApp(ui=ui, server=server)

注意:ユーザーにツールの使用方法を理解させるために、コードはshinyjsを採用して適切なタイミングで対応するコンポーネントを隠し/表示します.eventReactiveイベント駆動の計算では、reactiveに依存する少なくとも1つのコンポーネントが表示されていることを保証する必要があります.そうしないと、計算をトリガーできません.observeEventにはこの問題はありません.
転載先:R可視化:ShinyでクラスExcelデータ透視図を実現する