2016-09-24 25 views
0

我是RShiny中的新成员,但是在R中有一点经验。我刚开始研究R闪亮并尝试准备一个简单的两个数量线阵图轴。由于其更具表现力,我使用了csv上传功能并动态选择要在UI上填充的值。 ,我使用的样本CSV数据如下:动态过滤器,在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)选择一个CSV文件并上传它(它的工作)

2)以周因为我有一周的时间,它的离散值是1415,这意味着2014第15周,然后是1420,这意味着同一年第20周,那么1515是2015年,第15周和第15周。等等,我想把它按原样绘制,但是在X轴上,它绘制了一个连续的星期数。所以,作为一个工作周,我只是连续几周的专栏时间。 3)选择两个动态轴,然后将它们绘制为Y轴上不同颜色的折线图(如图所示)它的工作)

4)添加标签的两条线添加在Y.(它没有工作由于两条标绘线不是因素的一部分,但是两个不同的列动态选择, m无法绘制图例来解释哪种颜色对应于哪一行,需要帮助)。

5)然后最后一部分是我想包括一个组号码的动态过滤器。 (它不工作,需要帮助) 我试着把选择输入下拉在用户界面,但不知道如何将其映射到选定的CSV文件在server.R 。我不能直接把值,因为有100的行对应于一个组,并且有多个组。 但是,我知道只有一列需要该过滤器,只有该列的过滤器应显示折线图,但对如何输入该部分只有一点困惑。我经历了许多文章和问题,但没有得到类似的情况,其中的其他领域得到动态选择。

以下是代码,它的工作为一组,以一周序列工作年左右,周inspite,因为每年一周,直到只有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") 

}) 

任何帮助深表感谢。这可能不是那么艰难,我在R中使用dplyr进行子集和过滤,但是不知道如何在这里映射相同的部分。如果以上任何内容不清楚或需要更多信息,请告诉我。 如果通过直接加载CVS或使用阴谋,尽管ggvis,更好的解决方案是更好的,那么我也可以改变代码块,但要符合我的目标。

+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") 
})