Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.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&;闪亮的&x2013 ;动态生成后更新反应图的动态数量_R - Fatal编程技术网

R&;闪亮的&x2013 ;动态生成后更新反应图的动态数量

R&;闪亮的&x2013 ;动态生成后更新反应图的动态数量,r,R,我正在构建一个简单的报告系统,但是我在尝试用Shiny而不是我通常的Python/PHP goto路线来构建这个系统时遇到了第一个障碍 以下是到目前为止我得到的信息: library(shiny) library(ggplot2) library(googleAnalyticsR) server <- function(input, output) { id <- 'XXXXXXX' #dynamically create the right number of out

我正在构建一个简单的报告系统,但是我在尝试用Shiny而不是我通常的Python/PHP goto路线来构建这个系统时遇到了第一个障碍

以下是到目前为止我得到的信息:

library(shiny)
library(ggplot2)
library(googleAnalyticsR)

server <- function(input, output) {

  id <- 'XXXXXXX'

  #dynamically create the right number of output
  output$plots <- renderUI({

    web_data <- google_analytics_4(id, 
                                   date_range = c(format(input$dates[1]), format(input$dates[2])),
                                   metrics = c("sessions","pageviews","bounces"),
                                   dimensions = c("date", "channelGrouping"),
                                   anti_sample = TRUE)

    plot_output_list <- lapply(unique(web_data$channelGrouping), function(i) {
      plotname <- paste0("plot", i)
      plotOutput(plotname)
    })

    tagList(plot_output_list)



  }) 

  for (channel in unique(web_data$channelGrouping)) {
    local({ 
      plotname <- paste0("plot", channel)
      localChannel <- channel
      filtered_data <- subset(web_data, channelGrouping == channel, select=-channelGrouping)
      filtered_data.long <- melt(filtered_data, id.vars='date')

      output[[plotname]] <- renderPlot({
        ggplot(filtered_data.long, aes(date, value, color=variable))+ggtitle(localChannel)+geom_line()
      })
    })
  }
}

ui <- fluidPage(

  titlePanel("XXXXXX Reporting"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("choose_view"),
      dateRangeInput("dates", "dates") 
    ),
    mainPanel(
      uiOutput("plots")
    )
  )
)

shinyApp(ui = ui, server=server)

如果我的代码与此完全相同,那么绘图区域将在HTML代码中输出,但没有图形。由于缺乏理解,我尝试将被动块放入renderUI块,但遇到了强制错误。我敢肯定这是一个打字错误或什么的,有什么想法吗?

您可以从renderUI中删除web\u数据,并将其放置在一个反应块中

web_data<-reactive({
        google_analytics_4(id, 
                           date_range = c(format(input$dates[1]), 
                           format(input$dates[2])),
                           metrics = c("sessions","pageviews","bounces"),
                           dimensions = c("date", "channelGrouping"),
                           anti_sample = TRUE)
})

web_数据您可以从renderUI中删除web_数据并将其放置在反应块中

web_data<-reactive({
        google_analytics_4(id, 
                           date_range = c(format(input$dates[1]), 
                           format(input$dates[2])),
                           metrics = c("sessions","pageviews","bounces"),
                           dimensions = c("date", "channelGrouping"),
                           anti_sample = TRUE)
})

在Shayne03的回答之后,经过一些实验,
Webu数据最终得到了它。除了他的回答,我还需要调用reactive块,该块在
output$plots
块的开头重新编写数据

以下代码最终给出了我在此阶段的预期输出:

library(shiny)
library(reshape2)
library(ggplot2)
library(googleAnalyticsR)

server <- function(input, output) {

  id <- 'XXXXXXX'

  output$plots <- renderUI({

    updatePlots()

    plot_output_list <- lapply(unique(web_data()$channelGrouping), function(i) {
      plotname <- paste0("plot", i)
      plotOutput(plotname)
    })

    #
  }) 

  updatePlots <- reactive({
    for (channel in unique(web_data()$channelGrouping)) {

      local({ 
        plotname <- paste0("plot", channel)
        localChannel <- channel
        filtered_data <- subset(web_data(), channelGrouping == channel, 
                                select=-channelGrouping)
        filtered_data.long <- melt(filtered_data, id.vars='date')

        print("Hi")
        output[[plotname]] <- renderPlot({
          ggplot(filtered_data.long, aes(date, value, 
                                         color=variable))+ggtitle(localChannel)+geom_line()
        })
      })
    }
  })


  web_data<-reactive({
    #get the GA data, between the dates specified by user
    google_analytics_4(id, 
                       date_range = c(format(input$dates[1]), 
                                      format(input$dates[2])),
                       metrics = c("sessions","pageviews","bounces"),
                       dimensions = c("date", "channelGrouping"),
                       anti_sample = TRUE)
  })

}

ui <- fluidPage(

  titlePanel("XXXXX Reporting"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("choose_view"),
      dateRangeInput("dates", "dates") 
    ),
    mainPanel(
      uiOutput("plots")
    )
  )
)

shinyApp(ui = ui, server=server)
库(闪亮)
图书馆(E2)
图书馆(GG2)
图书馆(谷歌分析)

服务器在Shayne03的回答之后经过一些实验,最终得到了它。除了他的回答,我还需要调用reactive块,该块在
output$plots
块的开头重新编写数据

以下代码最终给出了我在此阶段的预期输出:

library(shiny)
library(reshape2)
library(ggplot2)
library(googleAnalyticsR)

server <- function(input, output) {

  id <- 'XXXXXXX'

  output$plots <- renderUI({

    updatePlots()

    plot_output_list <- lapply(unique(web_data()$channelGrouping), function(i) {
      plotname <- paste0("plot", i)
      plotOutput(plotname)
    })

    #
  }) 

  updatePlots <- reactive({
    for (channel in unique(web_data()$channelGrouping)) {

      local({ 
        plotname <- paste0("plot", channel)
        localChannel <- channel
        filtered_data <- subset(web_data(), channelGrouping == channel, 
                                select=-channelGrouping)
        filtered_data.long <- melt(filtered_data, id.vars='date')

        print("Hi")
        output[[plotname]] <- renderPlot({
          ggplot(filtered_data.long, aes(date, value, 
                                         color=variable))+ggtitle(localChannel)+geom_line()
        })
      })
    }
  })


  web_data<-reactive({
    #get the GA data, between the dates specified by user
    google_analytics_4(id, 
                       date_range = c(format(input$dates[1]), 
                                      format(input$dates[2])),
                       metrics = c("sessions","pageviews","bounces"),
                       dimensions = c("date", "channelGrouping"),
                       anti_sample = TRUE)
  })

}

ui <- fluidPage(

  titlePanel("XXXXX Reporting"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("choose_view"),
      dateRangeInput("dates", "dates") 
    ),
    mainPanel(
      uiOutput("plots")
    )
  )
)

shinyApp(ui = ui, server=server)
库(闪亮)
图书馆(E2)
图书馆(GG2)
图书馆(谷歌分析)

服务器谢谢,这让我很接近,但不是很接近。我已经编辑过了,你有没有可能再看一眼?谢谢,这让我很接近,但还不太清楚。我编辑过了,你能再看一眼吗?