在闪亮的条形图中输出R数据

时间:2018-04-23 11:37:51

标签: r shiny

我正在尝试将数据输出到图表中。我假设从示例(https://shiny.rstudio.com/gallery/telephones-by-region.html)下面的代码是可以的,但它只是抛出错误。

以下代码将我的数据输出到表格中:

enter image description here

如果我想在X轴上获得“date_start”并在Y轴上“点击”,那么最简单的方法是什么?

  # Getting the output into a tidy dataframe
  content_result <- content(report)
  content_result[["paging"]] <- NULL
  result_data <- content_result$data
  result_data <- result_data %>% reduce(bind_rows)


  # Setting classes of variables - numerical, data, etc and putting into a frame called 'import'
  result_data$impressions <- as.numeric(result_data$impressions)
  result_data$unique_clicks <- as.numeric(result_data$unique_clicks)
  result_data$clicks <- as.numeric(result_data$clicks)
  result_data$spend <- as.numeric(result_data$spend)
  result_data$date_start <- as.Date(result_data$date_start)
  result_data$date_stop <- as.Date(result_data$date_stop)

  import <- result_data


sum_by_day <- import  %>%
    group_by(date_start) %>%
    summarise(clicks = sum(clicks), impressions = sum(impressions), spend=sum(spend)) %>%
    mutate(CPC_new=spend/clicks) %>%
    mutate(CTR_new=clicks / impressions)

server.R是:

   output$phonePlot <- renderPlot({

    # Render a barplot
    barplot(sum_by_day[,clicks, 
            main=clicks,
            ylab="Number of clicks",
            xlab="Year")
  })

任何有助于帮助的人。

1 个答案:

答案 0 :(得分:0)

你有一些错误:

  1. 我假设你想在日期之后订购数据
  2. 您忘记使用示例
  3. 中的ui.R文件
  4. barplot函数中的第一个向量是完全错误的。它应该是y变量
  5. main中,您需要一个字符串而不是R变量
  6. 缺少闪亮的建筑(参见https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
  7. 更正的代码(此文件必须称为app.r):

        library(shiny)
    
    sum_by_day<-data.frame(date_start=17637:17643,
                       clicks=c(32,30,38,26,27,35,27),
                       impressions=c(4226,3918,2763,2913,2989,2908,2973),
                       spend =c(31.5,32.08,31.32,30.47,30.28,29.32,31.19),
                       CPC_new=c(0.98,1.07,0.82,1.17,1.12,0.84,1.16),
                       CTR_new=rep(0.01,7))
    
    
    ## ui.r
    
    ui<-fluidPage(    
    
      # Give the page a title
      titlePanel("Barplot by KPI"),
    
      # Generate a row with a sidebar
      sidebarLayout(      
    
        # Define the sidebar with one input
        sidebarPanel(
          selectInput("my_kpi", "Choose KPI:", 
                      choices=colnames(sum_by_day)[-1]),
          hr(),
          helpText("Data from AT&T (1961) The World's Telephones.")
        ),
    
        # Create a spot for the barplot
        mainPanel(
          plotOutput("phonePlot")  
        )
    
      )
    )
    
    
    ## server.r
    server<-function(input, output){
      output$phonePlot <- renderPlot({
    
        tmp<-sum_by_day[order(sum_by_day[,input$my_kpi]),input$my_kpi]
        names(tmp)
        names(tmp)<-sum_by_day$date_start[order(sum_by_day[,input$my_kpi])]
    
        # Render a barplot
        barplot(tmp,main=as.character(input$my_kpi),
                         ylab=as.character(input$my_kpi),
                         xlab="Time")
      })
    }
    
    ## deploy!
    shinyApp(ui = ui, server = server)