问题
I am having trouble recreating this answer in R with Highcharter to make my lines in a line series chart clickable URLs. Someone has already asked a similar question and they received an answer that was very helpful. However, I am having an issue with implementing that answer. They were able to use this.options.key because they were able to assign a column to key. I can't figure out how to assign one of the columns of my dataframe to key. I have imported my data from a csv file; my first few lines of data look like this:
How would I go about doing this?
library(shiny)
library(highcharter)
library(dplyr)
data <- read.csv("data/daily states.csv")
ui <- fluidPage(
titlePanel("Timeline"),
sidebarLayout(
sidebarPanel(
h2("Actions", align="center"),
fluidRow(
column(5,
selectizeInput("state",
h3("State:"),
c("All",
unique(data$state))))
),
fluidRow(
column(5,
selectInput("outcome",
h3("Outcome:"),
c("All",
unique(data$variable))))
),
fluidRow(
column(5,
dateRangeInput("date",
h3("Date range"),
min = "2020-01-22",
start = "2020-01-22",
end = as.character(Sys.Date())))
),
fluidRow(
column(5,
checkboxInput("federal",
"Show federal level",
value = TRUE))
)
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", highchartOutput("hcontainer")),
tabPanel("Table", DT::dataTableOutput("table"))),
)
)
)
server <- function(input, output, session){
newData <- reactive({
if (input$state != "All"){
data <- filter(data, state == input$state)
}
if (input$outcome != "All"){
data <- filter(data, variable == input$outcome)
}
data
})
output$table <- DT::renderDataTable(DT::datatable({
newData()
}))
output$hcontainer <- renderHighchart({
hc <- highchart(type = "chart") %>%
hc_xAxis(categories = unique(newData()$date)) %>%
hc_plotOptions(series = list(
allowPointSelect = TRUE,
cursor = "pointer",
point = list(
events = list(
click = JS( "function () { location.href = 'https://covidtracking.com/data/state/' + this.options.key + '#historical'}")
)
)
)) %>%
hc_add_series(name = (paste(input$state,input$outcome)), data = newData()$value, type = "line", mapping = hcaes(x = date, key = state, y = value))
hc
})
}
shinyApp(ui = ui, server = server)
来源:https://stackoverflow.com/questions/62777482/hyperlink-chart-in-highcharter