如何将Shiny App呈现的表数据/行发送到HBase / Hive中的表?

时间:2019-02-01 16:27:00

标签: r shiny rodbc r-plotly rstudio-server

我正在尝试创建一个应用程序,用户将在该应用程序上单击图形上的任何点,并且每个点都以R Shiny的形式存储到数据表中并进行渲染。用户可以将数据下载到CSV文件,但会保存到我的本地计算机。

我想以某种方式获取该呈现表并将其发送到hive / HBase表吗?如果那不可能,那么当用户关闭应用程序时。该渲染表可以自动以CSV格式保存到我的RStudio服务器文件系统中,而不是保存到本地计算机中。

直到现在,该应用程序已完全正常运行,并且用户单击了图中的某些点。它将填充表格。然后,用户可以单击按钮并将表格下载为CSV文件。该CSV文件将存储在我的本地计算机上。请参考代码。

    library(shiny)
    library(plotly)
    library(DT)

    d1=structure(list(Topic = c("compensation", "manager", "benefits",
                                "family", "communication", "worklifebalance", "perks", "compensation",
                                "benefits", "manager", "communication", "worklifebalance", "family",
                                "perks", "benefits", "compensation", "manager", "communication",
                                "family", "worklifebalance", "perks"),
                      variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
                                           .Label = c("Prct", "Count"), class = "factor"),
                      value = c(2.23121245555964, 0.723305136692411, 0.576192227534633,
                                0.202280250091946, 0.190020840995464, 0.153242613706019,
                                0.0122594090964816, 0.913705583756345, 0.609137055837563,
                                0.50761421319797, 0.50761421319797, 0.304568527918782, 0.203045685279188,
                                0, 1.49977276170277, 1.21193758521436, 0.893803969095592,
                                0.439327374640206, 0.348432055749129, 0.242387517042872,
                                0.0757460990758976),
                      group = c("APAC", "APAC", "APAC", "APAC",  "APAC", "APAC", "APAC",
                                "EMEA", "EMEA", "EMEA", "EMEA", "EMEA", "EMEA", "EMEA",
                                "AMERICAS", "AMERICAS", "AMERICAS", "AMERICAS", "AMERICAS",
                                "AMERICAS", "AMERICAS")),
                 .Names = c("Topic", "variable", "value", "group"), class = c("data.table", "data.frame"),
                 row.names = c(NA, -21L))


    javascript <- "
    function(el, x){
      el.on('plotly_click', function(data) {
        var colors = [];
        // check if color is a string or array
        if(typeof data.points[0].data.marker.color == 'string'){
          for (var i = 0; i < data.points[0].data.marker.color.length; i++) {
            colors.push(data.points[0].data.marker.color);
          }
        } else {
          colors = data.points[0].data.marker.color;
        }
        // some debugging
        //console.log(data.points[0].data.marker.color)
        //console.log(colors)

        // set color of selected point
        colors[data.points[0].pointNumber] = '#FF00FF';
        Plotly.restyle(el,
          {'marker':{color: colors}},
          [data.points[0].curveNumber]
        );
      });
    }"


    ui <- fluidPage(
      fluidRow(plotlyOutput('keywords')),
      fluidRow(verbatimTextOutput("selection")),
      fluidRow(DT::dataTableOutput("table1")),
      actionButton("rem_point", "Remove Last Point"),
      downloadButton('download',"Download the data")
    )   

    d0 = d1
    key <- row.names(d0)

    server = function(input,output){
      output$keywords = renderPlotly({


        d0 <- data.frame(d0, key)  

        p = ggplot(d0, aes(reorder(Topic,-value), value, key = key)) +
          geom_point(aes(colour = value),
                     shape = 16,
                     size = 3,
                     show.legend = F) +
          facet_wrap(~ group)+
          theme_minimal()
        ggplotly(p) %>% onRender(javascript)


      })


      d2 <- reactiveVal(data.frame())

      selection2 = reactive({
        s =  event_data("plotly_click")
        cat("You selected: \n\n")
        data.frame(s)
      })


      observeEvent(event_data("plotly_click"), {
        d2Temp <- rbind(
          d2(),
          d1 %>% filter(key == selection2()$key)
        )
        d2(d2Temp)
      })


      #values <- reactiveValues(dfWorking = d2())

      ## 4. remove row on actionButton click ##
      observeEvent(input$rem_point, {
        rem_row <- d2()[-nrow(d2()), ]
        d2(rem_row)

      })

      output$table1 = renderDT({
        d2()
      }) 

      output$download <- downloadHandler(
        filename = function(){"thename.csv"}, 
        content = function(fname){
          write.csv(d2(), fname)
        }
      )

    }

    shinyApp(ui, server)

我希望而不是将CSV文件存储到我/用户的本地计算机中。它应该使用ODBC连接直接发送到HBase / Hive中的表吗?

我还尝试使用隔离功能,以便可以将反应性数据帧设置为非反应性,但这对我不起作用。我是Shiny和R的新手。谢谢!

0 个答案:

没有答案