闪亮:输出的动态数量 Elements/Plots
Shiny: Dynamic Number of Output Elements/Plots
我想制作一个反应式显示,根据选择的输入选择器的值显示不同数量的图。在 mtcars 数据集的情况下,假设我想让用户选择按 Nr 进行切割。齿轮或 Nr。 Carburatos 用于要制作的地块。
查看 unique(mtcars$gear)
我们看到它有 4 3 5
3 个可能的值,而 unique(mtcars$carb)
有 4 1 2 3 6 8
6 个可能的值。因此,我想在选择 Nr. of Carburators
时生成 6 个单独的图,而在选择 Nr. of Gears
时只生成 3 个图。我玩过 conditionalPanel
但它总是在我在选择器之间切换一两次后爆炸。帮助?
闪亮UI:
library(shiny)
library(googleVis)
shinyUI(bootstrapPage(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear",
"Nr. of Carburators"="carb")),
htmlOutput('mydisplay') ##Obviously I'll want more than one of these...
# conditionalPanel(...)
))
闪亮的服务器:
shinyServer(function(input, output) {
#Toy output example for one out of 3 unique gear values:
output$mydisplay <- renderGvis({
gvisColumnChart(
mtcars[mtcars$gear==4,], xvar='hp', yvar='mpg'
)
})
})
您试过制作总共 9 (6+3) 个条件面板吗?如果是,您是否尝试过 3 个内部有条件的裸输出面板在绘图之间切换,以及 3 个额外的条件面板用于非重叠绘图?
另一种方法可能是制作一个带有内部条件的输出面板,然后将 3 或 6 个绘图堆叠成一个绘图
if(cond1) {
par(mfrow=c(3,1))
plot1
plot2
plot3
} else {
par(mfrow=c(3,2))
plot1
plot2
plot3
plot4
plot5
plot6
}
受 this 启发,您可以:
ui.R
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear", "Nr. of Carburators"="carb"))
),
mainPanel(
# This is the dynamic UI for the plots
uiOutput("plots")
)
))
server.R
library(googleVis)
shinyServer(function(input, output) {
#dynamically create the right number of htmlOutput
output$plots <- renderUI({
plot_output_list <- lapply(unique(mtcars[,input$choosevar]), function(i) {
plotname <- paste0("plot", i)
htmlOutput(plotname)
})
tagList(plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max(unique(mtcars[,"gear"]),unique(mtcars[,"carb"]))) {
local({
my_i <- i
plotname <- paste0("plot", my_i)
output[[plotname]] <- renderGvis({
data <- mtcars[mtcars[,input$choosevar]==my_i,]
if(dim(data)[1]>0){
gvisColumnChart(
data, xvar='hp', yvar='mpg'
)}
else NULL
})
})
}
})
它基本上动态创建 htmlOutput
图,并在子集中有数据时绑定 googleVis
图。
我想制作一个反应式显示,根据选择的输入选择器的值显示不同数量的图。在 mtcars 数据集的情况下,假设我想让用户选择按 Nr 进行切割。齿轮或 Nr。 Carburatos 用于要制作的地块。
查看 unique(mtcars$gear)
我们看到它有 4 3 5
3 个可能的值,而 unique(mtcars$carb)
有 4 1 2 3 6 8
6 个可能的值。因此,我想在选择 Nr. of Carburators
时生成 6 个单独的图,而在选择 Nr. of Gears
时只生成 3 个图。我玩过 conditionalPanel
但它总是在我在选择器之间切换一两次后爆炸。帮助?
闪亮UI:
library(shiny)
library(googleVis)
shinyUI(bootstrapPage(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear",
"Nr. of Carburators"="carb")),
htmlOutput('mydisplay') ##Obviously I'll want more than one of these...
# conditionalPanel(...)
))
闪亮的服务器:
shinyServer(function(input, output) {
#Toy output example for one out of 3 unique gear values:
output$mydisplay <- renderGvis({
gvisColumnChart(
mtcars[mtcars$gear==4,], xvar='hp', yvar='mpg'
)
})
})
您试过制作总共 9 (6+3) 个条件面板吗?如果是,您是否尝试过 3 个内部有条件的裸输出面板在绘图之间切换,以及 3 个额外的条件面板用于非重叠绘图?
另一种方法可能是制作一个带有内部条件的输出面板,然后将 3 或 6 个绘图堆叠成一个绘图
if(cond1) {
par(mfrow=c(3,1))
plot1
plot2
plot3
} else {
par(mfrow=c(3,2))
plot1
plot2
plot3
plot4
plot5
plot6
}
受 this 启发,您可以:
ui.R
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear", "Nr. of Carburators"="carb"))
),
mainPanel(
# This is the dynamic UI for the plots
uiOutput("plots")
)
))
server.R
library(googleVis)
shinyServer(function(input, output) {
#dynamically create the right number of htmlOutput
output$plots <- renderUI({
plot_output_list <- lapply(unique(mtcars[,input$choosevar]), function(i) {
plotname <- paste0("plot", i)
htmlOutput(plotname)
})
tagList(plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max(unique(mtcars[,"gear"]),unique(mtcars[,"carb"]))) {
local({
my_i <- i
plotname <- paste0("plot", my_i)
output[[plotname]] <- renderGvis({
data <- mtcars[mtcars[,input$choosevar]==my_i,]
if(dim(data)[1]>0){
gvisColumnChart(
data, xvar='hp', yvar='mpg'
)}
else NULL
})
})
}
})
它基本上动态创建 htmlOutput
图,并在子集中有数据时绑定 googleVis
图。