Paranoidantroid
Paranoidantroid

Reputation: 37

Outputting R data in shiny bar chart

I am trying to ouput my data into a graph. I assumed from the example (https://shiny.rstudio.com/gallery/telephones-by-region.html) the code below is ok, but it just throws back errors.

The below code outputs my data into the table:

enter image description here

If I wanted to get "date_start" on the X axis and "clicks" on the Y axis what is the simplest way todo that?

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

The server.R is:

   output$phonePlot <- renderPlot({

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

Any help apreciated.

Upvotes: 0

Views: 2022

Answers (1)

5th
5th

Reputation: 2395

You had some errors:

  1. I assumed you want to order the data after the date
  2. You forgot to use the ui.R file from the example
  3. Your first vector in the barplot function is completely wrong. It should be the y-variable
  4. In main you need a single string not an R variable
  5. Your shiny architecture is missing (cf. https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/)

The corrected code (this file has to be called 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)

Upvotes: 0

Related Questions