2016-09-24 3 views
0

저는 RShiny에서 새로 도입되었지만 R에서 약간의 경험이 있습니다. 저는 반짝이는 R 작업을 시작했으며 X에 대한 간단한 양 선형 선 차트를 준비하려고합니다. 중심선. 더보기 좋기 때문에 CSV 업로드 기능을 사용하고 UI에 값을 동적으로 선택했습니다.동적 필터, ggvis의 범례 추가 및 이산 X 축 플롯

year-week Group big small Time 
1415   G1 10  5 1 
1416   G1 20  6 2 
1429   G1 15  4 3 
1530   G1 17  5 4 
1535   G1 20  7 5 
1601   G1 13  6 6 
1606   G1 12  5 7 
1410   G2 9  3 1 
1415   G2 11  4 2 
1439   G2 13  5 3 
1529   G2 15  6 4 
1531   G2 15  4 5 
1610   G2 12  5 6 
1615   G2 19  6 7 
1412   G3 9  10 1 
1417   G3 20  6 2 
1452   G3 13  5 3 
1501   G3 10  4 4 
1530   G3 17  7 5 
1620   G3 16  5 6 

내 주요 목적은 다음과 같습니다 :

1) (그것은 일하고있어)

2) 주 찍 CSV를 선택하고 업로드 다음과 같이 내가 사용 샘플 CSV 데이터입니다 x 축 (주위에 작은 일을하고 있어요. 내가 일주일을 가지고 있기 때문에 1415와 같은 이산 가치가 2014,15 주를 의미하고 1420은 같은 해의 20 번째 주를 의미하고, 그 다음 1515는 2015, 15 주 및 나는 그것을 그대로 음모하고 싶었지만 x 축에 연속 주 숫자를 그려 넣었습니다. 그래서 주위를 둘러 보았을 때 방금 연속 주 동안 기둥을 만들었습니다. 시각.

3) 두 개의 동적 축을 선택한 다음 서로 다른 색으로 Y 축의 선형 차트로 그립니다 (예 : X 축에서 연속 함수로 사용하지 않는 것이 좋습니다. 작동 중입니다.

4) Y에 추가 된 두 줄의 레이블을 추가하십시오. () 작동하지 않습니다. 두 개의 플롯 된 선은 요소의 일부가 아니지만 동적으로 선택되는 두 개의 다른 열이므로, 어떤 색이 어느 선에 해당하는지 설명하기 위해 범례를 그릴 수 없습니다.이 부분에 대한 도움이 필요합니다.)

5) 마지막 부분은 그룹 번호에 동적 필터를 포함하려는 것입니다. (그것은 작동하지 않는다 및 이것에 도움을 필요로한다) UI에있는 selectinput 놓기 시도하고 그러나 server.R 에있는 선정 된 CSV 파일에 그것을지도로 나타내는 방법. 나는 100 's가 있기 때문에 가치를 직접 둘 수 없다. 하나의 그룹에 대응하는 행의 수와 다수의 그룹이있다. 그러나 필자는 하나의 열만 필터를 필요로한다는 것을 알고 있으며 해당 열의 필터 만 꺾은 선형 차트를 표시해야하지만 그 부분을 입력하는 방법에 대해 혼란스럽지 않습니다. 나는 많은 기사와 질문을 마쳤지 만 다른 분야가 동적으로 선택되는 비슷한 상황을 얻지 못했습니다. 년 주에만 52까지이며 그래서 내가에 도움을 필요로 1452 사이에 격차를 취급하기 때문에,

다음은 코드이며 올해 주 Ɒ이 주위 작품으로 주 순서를 복용 한 그룹을 위해 일하고있어 범례 부분과 모든 데이터에서 함께 실행할 수 있도록 그룹을 필터링하는 코드.

UI.R

library(ggvis) 
library(shiny) 
shinyUI(pageWithSidebar(
    div(), 
    sidebarPanel(
    fileInput('datfile', ''), 
    selectInput('x', 'x:' ,'x'), 
    selectInput('y', 'y:', 'y'), 
    selectInput('z', 'z:', 'z'), 
    ######### Need to choose one of the following 2 methods for filtering ######## 
    #1# 
    #selectInput("w", label = h3("Filter group"), 
    #   ("Group" = "Group"),selected = "Group"), 
    ############################## OR ###################################    
    # 2# To make a select box 
    selectInput("select", label = h3("Filter Group"), 
       choices = list("G1" = 1, "G2" = 2, "G3" = 3), 
       selected = 1), 
    ###################################################################### 
    hr(), 
    fluidRow(column(3, verbatimTextOutput("value"))), 
    uiOutput("plot_ui") 

), 
    mainPanel(
    ggvisOutput("plot") 
) 
)) 

Server.R 어떤 도움이 많이 감사합니다

library(shiny) 
library(dplyr) 
library(ggvis) 

shinyServer(function(input, output, session) { 
    #load the data when the user inputs a file 
    theData <- reactive({ 
    infile <- input$datfile   
    if(is.null(infile)) 
     return(NULL)   
    d <- read.csv(infile$datapath, header = T) 
    d   
    }) 

    # dynamic variable names 
    observe({ 
    data<-theData() 
    updateSelectInput(session, 'x', choices = names(data)) 
    updateSelectInput(session, 'y', choices = names(data)) 
    updateSelectInput(session, 'z', choices = names(data)) 
    }) # end observe 

    #gets the y variable name, will be used to change the plot legends 
    yVarName<-reactive({ 
    input$y 
    }) 

    #gets the x variable name, will be used to change the plot legends 
    xVarName<-reactive({ 
    input$x 
    }) 

    #gets the z variable name, will be used to change the plot legends 
    zVarName<-reactive({ 
    input$z 
    }) 

    #make the filteredData frame 

    filteredData<-reactive({ 
    data<-isolate(theData()) 
    #if there is no input, make a dummy dataframe 
    if(input$x=="x" && input$y=="y" && input$z=="z"){ 
     if(is.null(data)){ 
     data<-data.frame(x=0,y=0,z=0) 
     } 
    }else{ 
     data<-data[,c(input$x,input$y,input$z)] # Here data shall be filtered 
     names(data)<-c("x","y","z") 
    } 
    data 
    }) 

    #plot the ggvis plot in a reactive block so that it changes with filteredData 
    vis<-reactive({ 
    plotData<-filteredData() 

    plotData %>% 
     ggvis(~x, ~y) %>% 
     #ggvis(~x, ~y, storke = c(~y,~z)) %>% # It's not working & not picking y&z together 
     #set_options(duration=0) %>% 
     layer_paths(stroke := "darkblue", fill := NA) %>% 
     layer_paths(x = ~x, y = ~z, stroke := "orangered", fill := NA) %>% 
     add_axis("y", title = "Big v/s small") %>% 
     add_axis("x", title = xVarName()) %>% 
     #add_legend('stroke', orient="left") %>% # Unable to put the legend 
     add_tooltip(function(df) format(sqrt(df$x),digits=2)) 
    }) 
    vis%>%bind_shiny("plot", "plot_ui") 

}) 

:

에 따라 지금까지 작업 한 코드입니다. 그것은 힘들지 않을 수도 있습니다. 그리고 저는 dplyr을 서브 세트와 필터로 사용하여 R에서 비슷한 부분을 만들었지 만, 같은 것을 여기에 매핑하는 방법을 모르겠습니다. 위의 내용이 불명확하거나 추가 정보가 필요한 경우 알려 주시기 바랍니다. CVS를 직접로드하거나 ggvis의 plot inspite를 사용하여 솔루션이 더 좋은 경우에는 코드 조각을 변경하는 것이 좋지만 내 목표를 충족시키고 싶습니다.

+0

편집 : 난 그냥 필터 부분은 수행 만 전설 부분 왼쪽과 X 축에 이산 값을 받고 있어요. 누군가가 그것을 검토한다면 친절하게도이 두 부분에 대해서만 알려주십시오. – PD1

+0

범례도 완료되었습니다. 곧 답변을 게시 할 예정입니다. – PD1

답변

1

UI.R

library(ggvis) 
library(shiny) 
shinyUI(fluidPage ((img(src="picture.jpg")), 
    theme = "bootstrap.css", 
    fluidRow( 
    #headerPanel(title=div(img(src="picture.jpg"))), 
     column(9, align="center", offset = 2, 
      textInput("string", label="",value = "Big V/s Small"), 
      tags$style(type="text/css", "#string { height: 50px; width: 100%; text-align:center; font-size: 26px;}") 
    ) 
), 
    pageWithSidebar(
    div(), 
    sidebarPanel(
     fileInput('datfile', ''), 
     selectInput('x', 'x:' ,'x'), 
     selectInput('y', 'y:', 'y'), 
     selectInput('z', 'z:', 'z'), 
     ################ Need to choose one of the following method for filtering ############ 
     # To make a select box 
     selectInput("w", label = h3("Filter Group"), 
        choices = list()), 
     ###################################################################### 
     hr(), 
     fluidRow(column(3, verbatimTextOutput("value"))), 
     uiOutput("plot_ui") 

    ), 
    mainPanel(
     ggvisOutput("plot") 
    ) 
)) 
) 

Sevrer.R

#install.packages('rsconnect') 
#install.packages('bitops') 
library(shiny) 
library(dplyr) 
library(ggvis) 
library(reshape2) 

shinyServer(function(input, output, session) { 
    #load the data when the user inputs a file 
    theData <- reactive({ 
    infile <- input$datfile 
    if (is.null(infile)) 
     return(NULL) 
    d <- 
     read.csv(infile$datapath, 
       header = T, 
       stringsAsFactors = FALSE) 
    d 
    }) 

    # dynamic variable names 
    observe({ 
    data <- theData() 
    updateSelectInput(session, 'x', choices = names(data)) 
    updateSelectInput(session, 'y', choices = names(data)) 
    updateSelectInput(session, 'z', choices = names(data)) 
    updateSelectInput(session, 'w', choices = unique(data$group)) 
    }) # end observe 

    #gets the y variable name, will be used to change the plot legends 
    yVarName <- reactive({ 
    input$y 
    }) 

    #gets the x variable name, will be used to change the plot legends 
    xVarName <- reactive({ 
    input$x 
    }) 

    #gets the z variable name, will be used to change the plot legends 
    zVarName <- reactive({ 
    input$z 
    }) 

    #gets the w variable name, will be used to change the plot legends 
    wVarName <- reactive({ 
    input$w 
    }) 

    #make the filteredData frame 

    filteredData <- reactive({ 
    data <- isolate(theData()) 
    #if there is no input, make a dummy dataframe 
    if (input$x == "x" && input$y == "y" && input$z == "z") { 
     if (is.null(data)) { 
     data <- data.frame(x = 0, y = 0, z = 0) 
     } 
    } else{ 
     data = data[which(data$fineline_nbr == input$w), ] 
     data <- data[, c(input$x, input$y, input$z)] 
     names(data) <- c('x', input$y, input$z) 
    } 
    data 
    }) 

    #plot the ggvis plot in a reactive block so that it changes with filteredData 
    vis <- reactive({ 
    plotData <- filteredData() 
    plotData <- melt(plotData, id.vars = c('x')) 
    print(names(plotData)) 
    plotData %>% ggvis(x = ~ x, 
         y = ~ value, 
         stroke = ~ variable) %>% layer_lines() 
    }) 
    vis %>% bind_shiny("plot", "plot_ui") 
})