我想使用闪亮的App将表保存在spark本地服务器中。
1-我可以通过操作按钮进行连接
2-关闭有光泽的窗户时断开连接
3-但是我无法使用dplyr::copy_to
这是一个正在运行的示例。 取消注释行
#Studies_tbl <- dplyr::copy_to(global_var$sc, Studies, overwrite = TRUE)
看到错误。
library(shiny)
library(cgdsr)
library(dplyr)
library(sparklyr)
library(DT)
library(shinyjs)
jscode <- "shinyjs.closeWindow = function() { window.close(); }"
ui <- function(){
sidebarLayout(
sidebarPanel(
# used to stop shiny when windows is closed
useShinyjs(),
extendShinyjs(text = jscode, functions = c("closeWindow")),
actionButton("close", "Close window & disconnect"),
actionButton("connect", "Connect to spark"),
checkboxInput('buttonID', label = "save stable to spark"),
verbatimTextOutput("Saved")
),
mainPanel(
DT::dataTableOutput(outputId = "StudiesTable")
)
)
}
server <- function(input, output, session){
global_var <- reactiveValues()
## connect to cgdsr server
cgds <- cgdsr::CGDS("http://www.cbioportal.org/public-portal/")
## get Studies
shiny::withProgress(message = 'Loading Studies from cgdsr server...', value = 1, {
Studies<- cgdsr::getCancerStudies(cgds)
})
# render table of Studies
output$StudiesTable <- DT::renderDataTable({
table <- DT::datatable(Studies, filter = list(position = "top", clear = FALSE, plain = TRUE),
rownames = FALSE, style = "bootstrap", escape = FALSE,
class = "compact",
options = list(
#ajax = list(url = action),
search = list(search = "",regex = TRUE),
columnDefs = list(list(className = 'dt-center', targets = "_all")),
autoWidth = FALSE,
processing = FALSE,
pageLength = 10,
lengthMenu = list(c(10, 25, 50, -1), c('10','25','50','All'))
)
)
return(table)
})
## Reactive checkBox to save Studies table to spark local server
output$Saved <- renderText({
if(input$buttonID && !is.null(global_var$sc)){
#Studies_tbl <- dplyr::copy_to(global_var$sc, Studies, overwrite = TRUE)
}
## print value of checkBox
input$buttonID
})
## connect to spark
observeEvent(input$connect, {
shiny::withProgress(message = 'Connecting to Spark local server...', value = 1, {
sc <- spark_connect(master = "local")
## get sc as global variable to use it when we want to disconnect
global_var[["sc"]] <- sc
})
})
## disconnect spark if shiny is closed
observeEvent(input$close, {
js$closeWindow()
stopApp()
## disconnect spark is already connected
if(!is.null(global_var$sc)){
spark_disconnect(global_var$sc)
}
})
}
shinyApp(ui, server)
谢谢
Karim