动态过滤器,在 ggvis 中添加图例并绘制离散 X 轴
Dynamic filter, Add legend in ggvis and plot discrete X axis
我是 RShiny 的新手,但在 R 方面有一点经验。我刚刚开始研究 R shiny 并尝试准备一个简单的两个数量线图与 X 轴上的周。由于它更像样,我使用了 csv 上传功能并动态选择要在 UI 上填充的值。
我使用的示例 CSV 数据如下:
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
我的主要 objective 是:
1) Select CSV 并上传(有效)
2) 在 x 轴上取一周(它正在处理一个小问题。因为我有年周,它是离散值,如 1415,表示 2014 年第 15 周,然后是 1420,表示同年第 20 周,然后是1515 表示 2015 年,第 15 周等等。我想按原样绘制它,但在 x 轴上,它绘制了一个连续的周数。因此,作为解决方法,我只是为连续的几周制作了一个列作为时间。任何提示非常感谢关于如何按原样使用年周列而不是 X 轴上的连续函数。)
3) Select 两个动态轴,然后将它们绘制为 Y 轴上不同颜色的折线图(有效)
4) 为 Y 上添加的两条线添加标签。(它不起作用。 因为两条绘制线不是因子的一部分,而是两个不同的列动态选择,我无法绘制图例来解释哪种颜色对应哪条线。需要帮助)。
5) 然后最后一部分是我想为组号添加一个动态过滤器。 (它不起作用,需要这方面的帮助)
我尝试将 selectinput 下拉菜单放在 UI 中,但不确定如何将其映射到 server.R 中选定的 CSV 文件
.我不能直接输入值,因为有 100 行对应一个组并且有多个组。
但是,我知道只有一列需要该过滤器,并且只有该列的过滤器才能显示折线图,但我对如何输入该部分感到困惑。我浏览了很多文章和问题,但没有遇到动态选择其他字段的类似情况。
以下是代码,它适用于一个组,尽管有年周,但仍以周顺序作为工作,因为年周仅到 52,并且它处理 1452 和 1501 之间的间隙。
因此,我需要有关图例部分和过滤组的代码的帮助,以便它可以 运行 一起处理所有数据。
以下是我到目前为止使用的代码:
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 或使用 plot 而不是 ggvis 来解决更好的解决方案,那么我也可以更改代码片段但想要满足我的 objective.
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")
})
我是 RShiny 的新手,但在 R 方面有一点经验。我刚刚开始研究 R shiny 并尝试准备一个简单的两个数量线图与 X 轴上的周。由于它更像样,我使用了 csv 上传功能并动态选择要在 UI 上填充的值。 我使用的示例 CSV 数据如下:
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
我的主要 objective 是:
1) Select CSV 并上传(有效)
2) 在 x 轴上取一周(它正在处理一个小问题。因为我有年周,它是离散值,如 1415,表示 2014 年第 15 周,然后是 1420,表示同年第 20 周,然后是1515 表示 2015 年,第 15 周等等。我想按原样绘制它,但在 x 轴上,它绘制了一个连续的周数。因此,作为解决方法,我只是为连续的几周制作了一个列作为时间。任何提示非常感谢关于如何按原样使用年周列而不是 X 轴上的连续函数。)
3) Select 两个动态轴,然后将它们绘制为 Y 轴上不同颜色的折线图(有效)
4) 为 Y 上添加的两条线添加标签。(它不起作用。 因为两条绘制线不是因子的一部分,而是两个不同的列动态选择,我无法绘制图例来解释哪种颜色对应哪条线。需要帮助)。
5) 然后最后一部分是我想为组号添加一个动态过滤器。 (它不起作用,需要这方面的帮助) 我尝试将 selectinput 下拉菜单放在 UI 中,但不确定如何将其映射到 server.R 中选定的 CSV 文件 .我不能直接输入值,因为有 100 行对应一个组并且有多个组。 但是,我知道只有一列需要该过滤器,并且只有该列的过滤器才能显示折线图,但我对如何输入该部分感到困惑。我浏览了很多文章和问题,但没有遇到动态选择其他字段的类似情况。
以下是代码,它适用于一个组,尽管有年周,但仍以周顺序作为工作,因为年周仅到 52,并且它处理 1452 和 1501 之间的间隙。 因此,我需要有关图例部分和过滤组的代码的帮助,以便它可以 运行 一起处理所有数据。
以下是我到目前为止使用的代码:
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 或使用 plot 而不是 ggvis 来解决更好的解决方案,那么我也可以更改代码片段但想要满足我的 objective.
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")
})