我无法使用Highcharter在R中重新创建this answer,以使我的折线图图表中的线条可以点击。有人已经问过similar question,他们收到的答案非常有帮助。但是,我在执行该答案时遇到问题。他们能够使用this.options.key,因为他们能够为key分配一列。我不知道如何将我的数据框的列之一分配给键。我已经从csv文件导入了数据;我的前几行数据如下所示:
如何将数据框的状态列分配给密钥?我试图通过将它放在示例中的“ mapping = hcaes(key = state)”中来进行设置,但是它不起作用,因为数据等于newData()$ value。我必须将数据分配给该值,否则绘图将不会显示。我该如何解决这个问题?
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)