티스토리 뷰

#Archive

dn_shiny

Gom Guard 2018. 10. 5. 21:24
반응형

url = 'http://kosis.kr/openapi/statisticsBigData.do?method=getList&apiKey=YjJhODMwYjhiMzc0MTBjOGUzYmRmMTE4ZTg5NDgwMDY=&format=xls&userStatsId=knholic/101/DT_1L9H002/3/1/20181127222300&prdSe=Q&newEstPrdCnt=100'



https://cran.r-project.org/web/packages/shinyjqui/vignettes/orderInput.html

https://yang-tang.github.io/shinyjqui/

https://shiny.rstudio.com/articles/css.html


#

# This is a Shiny web application. You can run the application by clicking

# the 'Run App' button above.

#

# Find out more about building applications with Shiny here:

#

#    http://shiny.rstudio.com/

#

library(shinyjqui)

library(tidyverse)

library(DT)

library(magrittr)

library(lazyeval)

library(shiny)


base_pivot <- function(para_dt, para_grp, para_grp2, para_summ, para_func){

  # para_dt <- iris

  # para_grp <- "Sepal.Length"

  # para_grp2 <- "Species"

  # para_summ <- c("Sepal.Length","Sepal.Width")

  # para_func <- c("sum", "mean")

  summ <- c()

  

  

  for (idx in 1:length(para_summ)) {

    assign(paste0("func", idx), match.fun(para_func[idx]), envir = .GlobalEnv)

    temp <- paste0( "interp(~func",idx,"(v), v=as.name(\"", para_summ[idx], "\"))") %>% as.formula()

    summ %<>% c(., temp)

  }

  names_summ <- paste0(para_func, "_", para_summ)    

  names <- c(para_grp, para_grp2, names_summ)

  

  b <- para_dt %>% 

    group_by(.dots = c(para_grp, para_grp2)) %>%

    summarise_(.dots = summ) %>%

    set_names(names) 

    

  if (length(para_summ) == 1) {

    b %<>% unite("nVal", para_grp2)%>% spread( nVal, names_summ)    

  }

  b

  

}


# iris <- read_csv("testtest.csv") %>% head(1000)

# Define UI for application that draws a histogram

ui <- fluidPage(

  includeCSS("test.css"),

   tags$div( class = "header1",

     titlePanel("Pivot Shiny"),tags$hr()),

   # Application title

   

   

   # Sidebar with a slider input for number of bins 

     fluidPage(

       fluidRow(

        

        column(width = 10,

               dateInput('thedate', '기준일 선택'),

               orderInput('source', 'Source', items = names(iris),

                          as_source = TRUE, connect = c('행', '열'), item_class = 'warning'), tags$br(),

               orderInput('행', '행', items=NULL, placeholder = 'Drag items here...', connect = 'delete', item_class = 'primary'), tags$br(),

               orderInput('열', '열', items=NULL, placeholder = 'Drag items here...', connect = 'delete'),  tags$hr(),

               

               orderInput('source', 'Source', items = names(iris)[sapply(iris, class)=="numeric" ],

                          as_source = TRUE, connect = c('연산대상'), item_class = 'success'),tags$br(),

               orderInput('연산대상', '연산대상', items=NULL, placeholder = 'Drag items here...', connect = 'delete'),tags$br(),

               orderInput('source', 'Source', items = c("sum","mean","n"),

                          as_source = TRUE, connect = c('연산'), item_class = 'info'),tags$br(),

               orderInput('연산', '연산', items=NULL, placeholder = 'Drag items here...', connect = 'delete'),tags$hr(),

               verbatimTextOutput('order'),tags$hr(),

               actionButton('run', "RUN"),

               tags$br(),

               tags$br(),

               tags$hr(),

               tags$br()

               

               ),

        column(width = 2,

               orderInput('delete', '삭제란', items=NULL, placeholder = 'Drag items here...')

               )

       ),

       tabsetPanel(

         tabPanel('tab1',

                  fluidRow(

                    

                    column(width = 12,tags$br(),

                           DT::dataTableOutput('dtoutp')

                           

                           

                    )

                  ),

                  fluidRow(

                    column(width = 3,tags$br(),

                           selectizeInput('slt_x','x', choices = NULL),

                           selectizeInput('slt_y','y', choices = NULL)

                    ),

                    column(width = 9,tags$br(),

                           plotOutput('ploutp')

                           

                    )

                  )

            ),

         tabPanel('tab2',

                  fluidRow(

                    column(width=12,tags$br(),

                           tableOutput('dtoutp2')

                           )

                  ))

       )

       

       

     ) 

   )





# Define server logic required to draw a histogram

server <- function(input, output, session) {

  output$order <- renderPrint({ 

    print(paste0(input$연산_order,"_",input$연산대상_order) )

    })

  

  aa <- reactive({

    c(input$aaaa_order[1], input$aaaa_order[2])

  })

  

  output$distPlot <- renderPlot({

    ggplot(iris, aes_string(aa()[1], aa()[2])) +

      geom_point()

  })

  

  dtf <- eventReactive(input$run, {

    a <- base_pivot(iris, input$행_order, input$열_order, input$연산대상_order, input$연산_order)

    updateSelectizeInput(session, 'slt_x', choices =names(a), server = TRUE)

    updateSelectizeInput(session, 'slt_y', choices =names(a), server = TRUE)

    a

  })

  

  output$dtoutp <- DT::renderDataTable(dtf())

  output$dtoutp2 <- renderTable(dtf())

  output$ploutp <- renderPlot({

    cnames <- names(dtf())

    ggplot(dtf(), aes_string(input$slt_x, input$slt_y)) +

      geom_point()

    }

  )

  

  observe({

    print(input$연산대상_order)

    print(input$연산_order)

    print(paste0(input$연산대상_order,"_",input$연산_order))

  })

  

  

}





# library(rpivotTable)

# rpivotTable(mtcars)


# Run the application 

shinyApp(ui = ui, server = server)



반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함