0

I am new to shiny and R in general and am stuck on how to make my app more interactive. I have other parts of the functionality working however the interactivity that i was after just isn't getting there and have hit a brick wall.

I have a dataset that I would like the user to be able view via selected grouping choices, it is a collection of obsveration for given KeyDates and for my purposes, I would like to see this graphed to show by: Year/Quarter or Month.

> head(myData)
     KeyDate              Area       SubArea       MetricGroup                            Metric Count Amount Year Quarter MonthNo WeekNo
1 2019-11-30           Billing       Billing Billing Documents            Bill Documents Created     0     NA 2019       1      11     48
2 2019-11-30           Billing       Billing  Billing Outsorts                     Bill Outsorts     0     NA 2019       1      11     48
3 2019-11-30 Device Management Meter Reading     Meter Reading        Implausible Reads Uploaded     0     NA 2019       1      11     48
4 2019-11-30 Device Management Meter Reading     Meter Reading No of Implausible reads - Backlog   847     NA 2019       1      11     48
5 2019-12-01           Billing       Billing Billing Documents            Bill Documents Created     0     NA 2019       2      12     48
6 2019-12-01           Billing       Billing  Billing Outsorts                     Bill Outsorts     0     NA 2019       2      12     48

ui.R

# Load the relevant libraries
library(shiny)
library(highcharter)

# Use a fluid Bootstrap layout
fluidPage(    

  # Give the page a title
  titlePanel("Demo of dynamic chart selection?"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with two input variables?
    sidebarPanel(
      selectInput("chartGroup", "Chart Grouping:", 
                  choices=c("Year", "Quarter", "MonthNo")),
      # hr(),
      helpText("How would you like to display the data?"),
      selectInput("chartMetric", "Chart Metric:", 
                  choices=c("Bill Documents Created", "Bill Outsorts", 
                            "Implausible Reads Uploaded", "No of Implausible reads - Backlog")),
      # hr(),
      helpText("Which dimension would you like to view?")      
    ),

    # Create a spot for the barplot
    mainPanel(
      highchartOutput("testPlot")  
    )

  )
)

server.R

# Load the relevant libraries
library(dplyr)
library(highcharter)
library(lubridate)

# This setting may help???
options(stringsAsFactors = FALSE)

# Read in the dataset from csv file
testData <- read.csv("./data/testData.csv", header = TRUE, sep = ",")

# Hack the Date into an actual Date field in R (because I have no idea how else to do it)
tempdate <- as.Date(testData$Date, "%d.%m.%Y")

# Bind the two datsets 
temp_data <- cbind(KeyDate = tempdate, testData)

# Now I have dataset to use for the charting ...
myData <- select(temp_data, -Date)

# Take a peek at the dataset
head(myData)

# Define a server for the Shiny app
function(input, output) {

  # >> Start of testPlot
  output$testPlot <- renderHighchart({

# Show the selection from th UI input
  si_choice <- input$chartGroup
  print(si_choice)
  si_metric <- input$chartMetric
  print(si_metric)

# >> This is where I would like to be able to 'select' what is used for grouping
# >> User could select from Year/Quarter/MonthNo

  # Output the chart using highcharter
  hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%
    hc_plotOptions(column = list(pointWidth = 10),
                   line   = list(
                     color  = "blue",
                     marker = list(
                       fillColor = "white",
                       lineWidth = 2,
                       lineColor = NULL
                     )
                   ))

  })
# << End of testPlot

}

Can anyone tell me what I have to do to allow for the "group = Year" to respond to user selectinput. "group = input$chartMetric" ... using the correct syntax that is, so that the result that I am looking for is achieved?

  # Output the chart using highcharter
  hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%

Many thanks in advance for this novices question

lachlindco
  • 35
  • 6
  • hi, can you please make your example [reproducible](https://stackoverflow.com/help/minimal-reproducible-example) using a dataset included in R (```mtcars``` or ```iris``` for example) or using ```dput``` on a subset of your data? – bretauv Feb 03 '20 at 09:44
  • Hi bretauv, sorry for the novice mistake, I will remember this tip when I encounter my next roadblock; I am 100% certain that will occur ;). I managed to find the answer `group = !!sym(input$chartGroup)` .. this was posted by dgyurko on GitHub who i am forever thankful to. Thank you to you for looking at my problem, this was very kind of you – lachlindco Feb 03 '20 at 10:40
  • 1
    you're welcome, you can put the solution you found as an answer, maybe that will help someone in the future – bretauv Feb 03 '20 at 12:01
  • Sure thing ... another rookie mistake, I only included a small snippet of the answer which i can see was very cryptic. The syntax needed is: `group = !!sym(input$chartGroup)` Which apparently converts the text to the field name ` # Output the chart using highcharter hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = !!sym(input$chartGroup))) %>%` – lachlindco Feb 06 '20 at 22:24

1 Answers1

1

The syntax needed is: group = !!sym(input$chartGroup)

Which converts the text to the column name from the dataset

# Output the chart using highcharter hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = !!sym(input$chartGroup))) %>%

lachlindco
  • 35
  • 6