我想进行反应性显示,根据选择的输入选择器的值显示不同数量的绘图。在mtcars数据集的情况下,假设我想让用户选择在Nr之间切割。 Gears或Nr。 Carcarratos对于要生产的地块。
查看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
一起玩过,但是我在选择器之间切换一次或两次后总是会爆炸。帮助
闪亮的用户界面:
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(...)
))
Shiny Server:
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'
)
})
})
答案 0 :(得分:8)
受到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
图。
答案 1 :(得分:0)
Have you tried making a total of 9 (6+3) conditionalPanels? If yes, have you tried 3 naked output panels that have conditionals inside them to switch between the plots, and 3 additional conditionalPanels for the non-overlapped plots?
Another way might be to make a single output panel with an internal conditional, and then stack your 3 or 6 plots into a single plot ala
.py