如何基于shiny中的复选框输入将多个dySeries发送到次轴?
我试图使用LungDeath时间序列绘制动态图,但我希望“mdeath”和“fdeath”位于次轴上,前提是在“ldeath”旁边至少选择了其中一个 下面是一个工作示例: global.R如何基于shiny中的复选框输入将多个dySeries发送到次轴?,r,shiny,r-dygraphs,R,Shiny,R Dygraphs,我试图使用LungDeath时间序列绘制动态图,但我希望“mdeath”和“fdeath”位于次轴上,前提是在“ldeath”旁边至少选择了其中一个 下面是一个工作示例: global.R library(dygraphs) library(shiny) library(datasets) library(stringr) lungDeaths <- cbind(mdeaths, fdeaths) shinyUI(fluidPage( titlePanel("Predicted De
library(dygraphs)
library(shiny)
library(datasets)
library(stringr)
lungDeaths <- cbind(mdeaths, fdeaths)
shinyUI(fluidPage(
titlePanel("Predicted Deaths from Lung Disease (UK)"),
sidebarLayout(
sidebarPanel(
id="sidebar", width = 3,
div(
checkboxGroupInput(
inputId = "selection", label = "Variables:",
choiceNames = list("ldeaths",
strong("mdeaths"),
strong("fdeaths")
),
choiceValues = c("ldeaths",
"mdeaths",
"fdeaths"), selected = "ldeaths"),
uiOutput("rendered"),
style = "font-size:75%"
)
),
mainPanel(
dygraphOutput("dygraph")
)
)
))
shinyServer(function(input, output) {
lungDeaths <- cbind(mdeaths, fdeaths, ldeaths)
rData <- reactive({
rData <- ts(lungDeaths[,input$selection])
})
output$dygraph <- renderDygraph({
if(length(input$selection) > 1 & length(str_subset(input$selection, 'ldeaths$'))>0){
if(length(str_subset(input$selection, 'fdeaths$'))>0 & length(str_subset(input$selection, 'mdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("mdeaths", axis = 'y2') %>%
dySeries("fdeaths", axis = 'y2')
}
else if(length(str_subset(input$selection, 'mdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("mdeaths", axis = 'y2')
}
else if(length(str_subset(input$selection, 'fdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("fdeaths", axis = 'y2')
}
}
else
dygraph(rData(), main = "Deaths from Lung Disease (UK)")
})
})
server.R
library(dygraphs)
library(shiny)
library(datasets)
library(stringr)
lungDeaths <- cbind(mdeaths, fdeaths)
shinyUI(fluidPage(
titlePanel("Predicted Deaths from Lung Disease (UK)"),
sidebarLayout(
sidebarPanel(
id="sidebar", width = 3,
div(
checkboxGroupInput(
inputId = "selection", label = "Variables:",
choiceNames = list("ldeaths",
strong("mdeaths"),
strong("fdeaths")
),
choiceValues = c("ldeaths",
"mdeaths",
"fdeaths"), selected = "ldeaths"),
uiOutput("rendered"),
style = "font-size:75%"
)
),
mainPanel(
dygraphOutput("dygraph")
)
)
))
shinyServer(function(input, output) {
lungDeaths <- cbind(mdeaths, fdeaths, ldeaths)
rData <- reactive({
rData <- ts(lungDeaths[,input$selection])
})
output$dygraph <- renderDygraph({
if(length(input$selection) > 1 & length(str_subset(input$selection, 'ldeaths$'))>0){
if(length(str_subset(input$selection, 'fdeaths$'))>0 & length(str_subset(input$selection, 'mdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("mdeaths", axis = 'y2') %>%
dySeries("fdeaths", axis = 'y2')
}
else if(length(str_subset(input$selection, 'mdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("mdeaths", axis = 'y2')
}
else if(length(str_subset(input$selection, 'fdeaths$'))>0){
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
dySeries("fdeaths", axis = 'y2')
}
}
else
dygraph(rData(), main = "Deaths from Lung Disease (UK)")
})
})
shinyServer(功能(输入、输出){
肺死亡(0){
动态图(rData(),main=“肺病死亡(英国)”)%>%
dySeries(“mdeaths”,轴='y2')%>%
dySeries(“fdeaths”,轴='y2')
}
else if(长度(str_子集(输入$selection,'mdeaths$)>0){
动态图(rData(),main=“肺病死亡(英国)”)%>%
dySeries(“mdeaths”,轴='y2')
}
else if(长度(str_子集(输入$selection,'fdeaths$)>0){
动态图(rData(),main=“肺病死亡(英国)”)%>%
dySeries(“fdeaths”,轴='y2')
}
}
其他的
动态图(rData(),main=“肺病死亡(英国)”)
})
})
这段代码做了我希望它做的事情,但我想避免使用太多的if和else,因为我实际处理的项目有6个项目,当被选中时应该转到y2。有没有办法不必详细说明每种可能性就可以做到这一点
我已经研究过了,怎么做,但是我找到的答案对我来说并不适用。我通常会收到一个错误,上面写着:
$运算符对于原子向量无效
我成功了,所以我发布这个答案是为了帮助那些一直在同一问题上挣扎的人 在尝试执行条件管道求值时,我没有正确使用if-else,这就是为什么我一直收到问题中提到的错误 事实证明,我必须使用点(.)来指定管道之前的对象应该插入以下函数的确切位置,可能是因为该函数位于条件求值中。下面的代码正是我想要它做的:
output$dygraph <- renderDygraph({
dygraph(rData(), main = "Deaths from Lung Disease (UK)") %>%
{
if(length(str_subset(input$selection, 'mdeaths$')) > 0 )
dySeries(.,"mdeaths", axis = 'y2')
else
.
} %>%
{
if(length(str_subset(input$selection, 'fdeaths$')) > 0 )
dySeries(.,"fdeaths", axis = 'y2')
else
.
}
})
输出$dygraph%
{
if(长度(str_子集(输入$selection,'mdeaths$)>0)
dySeries(,“mdeaths”,轴='y2')
其他的
.
} %>%
{
如果(长度(str_子集(输入$selection,'fdeaths$)>0)
dySeries(,“Fdeath”,轴='y2')
其他的
.
}
})
动态图现在在次轴上显示dyseries,无论何时选择它们作为输入,您都应该执行以下操作:
dg = dygraphs(rData, main="...") %>%
dyAxis("y", label="...") %>%
dyAxis("y2", label="...")
for(col in leftCols)
dg = dg %>% dySeries(col, axis="y")
for(col in rightCols)
dg = dg %>% dySeries(col, axis="y2")