使用highcharter使用Shiny返回多个数据

时间:2016-04-26 15:39:27

标签: r highcharts shiny

我想创建一个带有可拖动点的多线图,并将各个点的数据返回到Shiny服务器中的数据表。我不知道如何编写代码。有没有办法将myZax线图的更改返回到数据表m_data?

server.R code

library(shiny)
library(highcharter)
library(data.table)


myData <- data.table(x=2012:2016, data.frame(myYax = c(3900,  4200,  5700,  8500, 11900), myZax=c(3900,  4200,  500,  800, 1000)))

shinyServer(function(input, output) {

  #Make a copy of the data table. It will be run only once when use opens the application
  m_data <- myData

  output$text1 <- renderText({ 
    paste("You have selected ",input$xaxis_name)
  })

  output$text2 <- renderText({ 
    paste("You have selected ",input$yaxis_name)
  })

  output$view <- renderTable({                      
                      m_data[x==input$xaxis_name]$myYax = input$yaxis_name                      
                      m_data})

  output$hcontainer <- renderHighchart({    
    hc <- highchart() %>% 
      hc_chart(type = "line", animation=FALSE) %>% 
      hc_title(text = "A simple demo") %>% 
      hc_subtitle(text = "A subtitle") %>% 
      hc_xAxis(categories = m_data$x) %>% 
      hc_plotOptions(
        series = list(
          point = list(
            events = list(
              drop = JS("function(){
                        Shiny.onInputChange('xaxis_name', this.category)
                        Shiny.onInputChange('yaxis_name', this.y)          
                        }")))
          ,stickyTracking = FALSE
          ),
        column = list(
          stacking = "normal"
        ),
        line = list(
          cursor = "ns-resize"
        )) %>%
      hc_add_series(       
        data = m_data$myYax , name = "Downloads",draggableY = TRUE
      ) %>%
      hc_add_series(       
        data = m_data$myZax , name = "Downloads",draggableY = TRUE
      ) 
    hc    
  })
})

ui.R

    library(shiny)

shinyUI(
  fluidPage(
            titlePanel("High Charter plotting"),  
            highchartOutput("hcontainer",height = "500px"),
            textOutput("text1"),
            textOutput("text2"),
            tableOutput("view")            
           )
)

1 个答案:

答案 0 :(得分:1)

不确定如果我100%理解你的问题怎么办?但是我想提出一些建议,我希望它可以帮助解决你的问题。

  1. 而不是发送2 Shiny.onInputChange,你可以发送一个javascript对象,它像列表一样返回到R:

    Shiny.onInputChange('hcinput', { id: this.series.name, x: this.category, y: this.y })
    

    这是一个更清晰的代码和闪亮的服务器。

  2. 所以在output$view你可以做类似的事情:

    output$view <- renderTable({
      colname <- ifelse(input$hcinput$id == "Downloads1", "myYax", "myZax")
      m_data[x==input$hcinput$x][[colname]] = input$hcinput$y                      
      m_data
    })
    
  3. 如果您解决了问题,请讨论。

    感谢您对包裹进行测试!