我是带有R的Shiny包的新手。我想绘制动态数量的绘图,并能够根据每个绘图的用户输入单独控制它们中的每一个。这意味着它将有一组输入,将为每个绘图动态弹出。
我从代码here知道可以动态添加图表。从提到的代码here,我也能够动态生成输入。但是,我不能将它们并排放置。
给出以下输出。 enter image description here
当我尝试做两个情节时,我得到以下内容。
我想要的如下:
你能帮我解决这个问题吗?
代码如下:
ui.R
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),
mainPanel(
# This is the dynamic UI for the plots
uiOutput("plots"),
uiOutput("Dynamic")
)
))
server.R
max_plots <- 50
shinyServer(function(input, output) {
# Insert the right number of plot output objects into the web page
output$plots <- renderUI({
# radioButtons(inputId = paste0("mVariable",i), label = paste0("mVariable",i), choices = c("A","B","C"))
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})
output$Dynamic <- renderUI({
dynamic_selection_list <- lapply(1:input$n, function(i) {
radioButtons(inputId = paste0("mVariable",i), label = paste0("mVariable",i), choices = c("A","B","C"))
})
do.call(tagList, dynamic_selection_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n is ", input$n, sep = "")
)
})
})
}
})
答案 0 :(得分:0)
你可以使用splitLayout
和一些css脚本来做到这一点。 splitLayout
将帮助您将绘图和单选按钮并排放置。需要css脚本,以便单选按钮容器的高度与绘图的高度相同。这是修改后的ui
,您可以使用它获得所需的输出。
ui <- shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),
mainPanel(
tags$head(tags$style(".shiny-input-container:not(.shiny-input-container-inline)
{height: 280px;}")),
splitLayout(uiOutput("plots"),
uiOutput("Dynamic"))
)
))
输出看起来像这样:
希望它有所帮助!