R Shiny循环显示多个图

use*_*875 0 r shiny

好的,我有你可以在下面运行的代码.我想修改这段代码:

https://gist.github.com/wch/5436415/

这样就可以使用UI的输入动态设置max_plots.UI的输入也是动态的.

这是我的UI.R:

shinyUI(pageWithSidebar(
  headerPanel("Dynamic number of plots"),
  sidebarPanel(
    selectInput("Desk", "Desk:" ,  c("A","B")),
   uiOutput("choose_Product"),
uiOutput("choose_File1"),
uiOutput("choose_Term1"),
sliderInput("n", "Number of plots", value=1, min=1, max=5)
  ),

  mainPanel(
    # This is the dynamic UI for the plots
    h3("Heading 1"),
    uiOutput("plots"),
   h3("Heading 2")
  )
))
Run Code Online (Sandbox Code Playgroud)

这是我的Server.R:

shinyServer(function(input, output) {

  output$choose_Product <- renderUI({
    selectInput("Product", "Product:", as.list( c("Product1","Product2","Product3")))
  })

  output$choose_File1 <- renderUI({
selectInput("File1", "File1:", as.list(c("File1","File2","File3")))
  })

# Insert the right number of plot output objects into the web page

  output$plots <- renderUI({
      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)
  })##End outputplots

  PrintPlots<- reactive({
## I need max_plots to be set as the length of the Vector returns from getAugmentedTerms()
max_plots<-length(getAugmentedTerms(input$Desk, input$Product, input$File1))

# 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 = "") )
        })
      })
    }##### End FoR Loop
  })# END OF PRINT PLOTS
})# END OF ENERYTHING
Run Code Online (Sandbox Code Playgroud)

这是我的全球.R:

getAugmentedTerms<-function(desk, product, file)
{
   # this function takes 3 inputs from the UI and returns a vector
   # for simplicity I am just returning a 2 element vector
   d<-c(1,2)
   return d
}
Run Code Online (Sandbox Code Playgroud)

这是运行它的代码:

library(shiny)
runApp("C:/Users/user/Desktop/R Projects/TestDynamicPlot")
Run Code Online (Sandbox Code Playgroud)

当你运行它时,你可以看到空间被创建就像有输出图但输出是空白的.有任何想法吗?谢谢!

Jul*_*rre 6

因此max_plots是硬编码的.当你启动应用程序时,循环for (i in 1:max_plots ) {(它只运行一次)创建5(= max_plots)个新output元素(output$plot1 output$plot2 ...) which render a plot, but the plots are displayed only when the respectiveplotOutputs exists in the UI. TheplotOutput s are created dynamically in the functionlapply(1:input $ n,function(i){ input$n is the value of the slider. 5 plots are created but we only createinput $ n plot outputs. That's why this is dynamic even ifmax_plots`是硬编码的.

如果需要,可以用max_plots动态参数替换,但需要将循环包含在观察者中.并且它会在每次input$n更改时运行.

例如 :

observe({
    for (i in 1:input$n) {
        # 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 = "")
            )
          })
        })
     }
})
Run Code Online (Sandbox Code Playgroud)

我不知道你的函数返回了什么,但我想如果你将它包含在服务器函数中并且在一个可以读取input对象的被动函数内部它也可以工作.

observe({ 
    max_plots<-length(getTerm1UI(input$Desk, input$Product, input$File1))
    for(i in 1:max_plots) {
       ...
Run Code Online (Sandbox Code Playgroud)