R ---- shiny package Introduction

Source: Internet
Author: User
Why shiny?

Shiny allows data analysts to write analysis and visualization code. After a few minutes, they can engineer the analysis code and quickly convert the analysis results into interactive web pages to share with others. Therefore, if you are a data analyst using R, it is wise to choose shiny because it does not require you to have new skills and it is too fast to develop. It is different from other frameworks we usually know: other frameworks are generally separated from the front and back ends, and the back end provides JSON. The front end draws a table based on JSON, and several programmers need to work together to complete the development. However, this visualization tool is often not supported by R & D resources, and only one data analyst can perform full-outsourcing operations.

#########################
# A time series data visualization Chestnut
#########################
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 <-XXXXX # define a function based on the data storage location, run the SQL statement} else {# Test Data group_id <-Rep (1, nrow (Economics) dt <-paste (. character (Economics $ date), "00:00:00") dF <-cbind (group_id, DT, Economics)} return (DF)} ui <-fluidpage (US Eshinyjs (), titlepanel ("time series data visualization tool"), # Part 1: SQL command submission interface Div (ID = "Download", fluidrow (column (12, textoutput (outputid = "download_info"), fluidrow (column (12, HTML (paste ('<textarea id = "SQL _cmd" rows = "10 ", cols = "180"> ', "select * from XXXX limit 1000;",' </textarea> '), fluidrow (column (12, actionbutton (inputid = "refresh_button", label = "load data", icon = icon ("Submit"), shinyjs: hidden (Div (Id = "table", # Part 2: HR (), ableableoutput (outputid = "SQL _tab"), # Part 3: Visual rule setting 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 = "set the X axis field, which must be a date time", choices = NULL, selected = NULL, multiple = false), selectinput (in Putid = "y_line_fields", label = "set Y-axis line chart field", choices = NULL, selected = NULL, multiple = true), selectinput (inputid = "y_point_fields ", label = "set Y axis vertex map field", choices = NULL, selected = NULL, multiple = true), selectinput (inputid = "group_shape_field", label = "set vertex map shape field ", choices = NULL, selected = NULL, multiple = false), actionbutton (inputid = "tab_button", label = "show group table", icon = icon ("Submit ")), width = 3), Div (ID = "group_content", Mainpanel (datatableoutput (outputid = "group_tab"), width = 9), # Part 4: Visual Image shinyjs: hidden (Div (ID = "plot ", HR (), plotlyoutput (outputid = "case_viewer", Height = "600px") server <-function (input, output, session) {observe ({# Check the SQL input box if (is. NULL (Input $ SQL _cmd) | Input $ SQL _cmd = "") {shinyjs: Disable ("refresh_button")} else {shinyjs: Enable ("refresh_button ")} # Check 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 ")}}) # Run the SQL command to obtain the data SQL _data <-eventreactive (Input $ refresh_button, {CAT (file = stderr (), "#### Event Log ####: refresh button clicked \ n ") shinyjs: Disable (" refresh_button ") shinyjs: Hide (ID =" table ", anim = true) shinyj S: Hide (ID = "plot", anim = true) RES <-run. SQL (Input $ 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, Indium Utid = "group_shape_field", choices = C ("NONE", colnames (RES), selected = "NONE") shinyjs: Enable ("refresh_button") shinyjs :: show (ID = "table", anim = true) shinyjs: Hide (ID = "group_content", anim = false) Return (RES )}) # SQL command execution status output $ download_info <-rendertext ({If (Input $ refresh_button = 0) {message <-"Enter the SQL SELECT query statement, click "} else {message <-isolate ({paste0 (" table download successful! Total number of rows ", nrow (SQL _data (),", total number of columns ", ncol (SQL _data (),", Update time is ",. character (lubridate: Now (), format = "% Y-% m-% d % H: % m: % s")} message }) # display SQL Execution result output $ SQL _tab <-Dt: renderable able ({datatable (SQL _data (), filter = 'top', selection = 'single ')}) # obtain the drawing group result group_data <-eventreactive (Input $ tab_button, {CAT (file = stderr (), "#### Event Log ####: tab button clicked \ n ") RES <-SQL _data () %> % select (on E_of (Input $ group_fields) %> % distinct () shinyjs: Show (ID = "group_content", anim = true) Return (RES )}) output $ tab_button_message <-rendertext ({If (Input $ tab_button = 0) {message <-"set data visualization rules on the left side of the page; click the button, the Data Group result is displayed in the table at the right of the table. Click a row in the table, the image "} else {message <-isolate ({paste0 (" Number of drawing groups ", nrow (group_data ()),", the Update time is ",. character (lubridate: Now (), format = "% Y-% m-% d % H: % m: % s")} message}) # Display the drawing group result output $ group_tab <-Dt: renderable able ({datatable (group_data (), filter = 'top', selection = 'single ')}) # display plotting observeevent (Input $ group_tab_rows_selected, {CAT (file = stderr (), paste0 ("#### Event Log ####: group table row ", input $ group_tab_rows_selected, "clicked \ n") Output $ case_viewer <-renderplotly ({S <-Input $ group_tab_row_last_clicked CAT (file = stderr (), "#### Event Log ####: Ta Ble row ", S," clicked \ n ") P <-ggplot () filter_str <-isolate ({str_c (group_data () [s, Input $ group_fields], collapse = "_")}) # Use _ with the unite method target_plot_data <-SQL _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 <-lubridate: ymd_hms (target_plot_data [, Input $ x_field], TZ = "UTC-8") line_df <-Target _ Plot_data %> % tidyr: Gather (col_name, thresh, one_of (Input $ y_line_fields) %> % dplyr: mutate (thresh =. numeric (thresh) P <-P + geom_line (Data = line_df, AES (x = DT, y = Thresh, color = col_name ))} if (length (Input $ y_point_fields)> 0) {target_plot_data $ dt <-lubridate: ymd_hms (target_plot_data [, Input $ x_field], TZ = "UTC-8 ") point_df <-target_plot_data %> % tidyr: Gather (col_name, thresh, one_of (Input $ y_po Int_fields) %> % dplyr: mutate (thresh = As. Numeric (thresh) if (Input $ group_shape_field! = "NONE") {point_df [, Input $ group_shape_field] <-. factor (point_df [, Input $ group_shape_field]) P <-P + geom_point (Data = point_df, aes_string (x = "DT", y = "thresh ", color = "col_name", shape = Input $ group_shape_field)} else {P <-P + geom_point (Data = point_df, AES (x = DT, y = Thresh, color = col_name) }}p <-P ggplotly (p)} shinyjs: Show ("plot", anim = true)} shinyapp (ui = UI, server = server)

Note: To help users understand how to use the tool, the Code uses shinyjs to hide/display corresponding components at the right time. In eventreactive event-driven computing, make sure that at least one dependency and the reactive component are displayed. Otherwise, the computation cannot be triggered, and the observeevent does not.

Reprinted from: R visualization: Use shiny to implement Excel-like data perspective

 
 

R ---- shiny package Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.