Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R和Shiny:基于服务器模块数据的动态UI_R_Shiny - Fatal编程技术网

R和Shiny:基于服务器模块数据的动态UI

R和Shiny:基于服务器模块数据的动态UI,r,shiny,R,Shiny,我正在构建一个闪亮的应用程序,其UI元素取决于观察的数量。应用程序在UI模块中绘制的绘图数量与在服务器模块中计算的观测数量相同。下面的代码只是我需要的示例: library(shiny) library(shinythemes) library(shinydashboard) library(corrplot) employee <- c('John Doe', 'John Doe', 'John Doe', 'Peter Gynn', 'Peter Gynn

我正在构建一个闪亮的应用程序,其UI元素取决于观察的数量。应用程序在UI模块中绘制的绘图数量与在服务器模块中计算的观测数量相同。下面的代码只是我需要的示例:

library(shiny)
library(shinythemes)
library(shinydashboard)
library(corrplot)

employee <- c('John Doe', 'John Doe', 'John Doe',
              'Peter Gynn', 'Peter Gynn', 'Peter Gynn',
              'Jolie Hope', 'Jolie Hope', 'Jolie Hope')
salary <- c(21000, 23400, 26800,
            22000, 25000, 24000,
            23000, 25500, 24500)
year <- c("2016", "2015", "2014",
          "2016", "2015", "2014",
          "2016", "2015", "2014")
mydata<-data.frame(employee, salary, year)

header <- dashboardHeader(title = "test", titleWidth=600) 
sidebar <- dashboardSidebar(disable=TRUE)

frowlist<-list()
for (i in 1:length(unique(mydata$employee))){
    frowlist[[i]]<-assign(paste0("frow",i),fluidRow( 
    status="success",
    collapsible = TRUE, 
    mainPanel(do.call(plotOutput,"myoutput"), width='98%')
    ))
}

body <- do.call(dashboardBody, frowlist) # Binding rows to body of dashboard
ui <- dashboardPage(header, sidebar, body, skin="yellow") 

server <- function(input, output) {
    myoutput<-reactive({
        myoutput<-list()
    for (i in 1:length(unique(mydata$employee))){
        myoutput[[i]]<-assign(paste0("output$plot_",i),renderPlot({ 
            ggplot(mydata[which(mydata$employee==unique(mydata$employee)[i])]$employee, aes(x = year, y = salary)) + geom_bar()
        }))
    }
    myoutput
})
}
shinyApp(ui, server)
如何将列表作为函数的变量从UI模块传递

编辑
首先,我无法从脚本的服务器端读取myoutput变量,将其作为脚本的全局类似端的列表。

基于Winston Chang的工作,我终于找到了解决方法


max\u绘图请指出您期望的闪亮绘图类型?在调试这个复杂的示例之前,请使用Shining dashboard查看简单的Shining教程:与其说是绘图类型,不如说是我期望的绘图数量。我期望N个图,其中N=mydata dataframe中的观察数。因此,在我的示例中,应该有三个条形图。
Error : second argument must be a list
max_plots <- 5

shinyServer(function(input, output) {

  # 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)
  })

  # 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 = "")
        )
      })
    })
  }
})