2017-01-05 19 views
3

반짝 이는 DT에 스파크 라인을 포함하고 싶습니다. RStudio 뷰어에서는 잘 작동하지만 Shiny에서는 스파크 라인이 렌더링되지 않습니다. 다음은 최소한의 예입니다.샤이니의 스파크 라인으로 데이터 테이블 렌더링

# dependencies 
    require(sparkline) 
    require(DT) 
    require(shiny) 

# create data with sparklines 
    spark_data <- data.frame(
    id = c('spark1', 'spark2'), 
    spark = c(
     spk_chr(values = 1:3, elementId = 'spark1'), 
     spk_chr(values = 3:1, elementId = 'spark2') 
    ) 
) 

# render in RStudio viewer (this works) 
    tbl <- datatable(spark_data, escape = FALSE) 
    spk_add_deps(tbl) 

# render in Shiny (no sparklines rendered in DT) 
    ui <- fluidPage(
     sparklineOutput("test_spark"), 
     dataTableOutput("tbl") 
) 

    server <- function(input, output) { 
    # sparkline outside DT (works fine) - also ensures sparkline dependencies are attached 
     output$test_spark <- renderSparkline(sparkline(1:3)) 

    # sparkline inside DT (does not render) 
     output$tbl <- renderDataTable(
     expr = spark_data, 
     escape = FALSE 
    ) 
    } 

    shinyApp(ui = ui, server = server) 
+0

반짝이 데이터 테이블에 스파크를 얻기 위해 (https://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html)이 [링크] 참조. – SBista

+0

고마워, 네, 그 페이지에서 시간을 보냈습니다. REST 뷰어에서 렌더링 한 첫 번째 사례를 다루고 있는데 문제가 없습니다. 그러나 링크가 두 번째 경우를 다루지는 않습니다 (왜 Shiny에서 렌더링하지 못합니까). 문제가 종속성과 관련이 있다고 생각합니다. 반짝 이는 응용 프로그램에서 'spk_add_deps'를 사용하는 방법이 나에게 명확하지 않으므로 첨부 된 종속성을 얻으려면 DT 외부에 더미 스파크 라인을 렌더링해야합니다. – Grant

+0

저는이 질문의 버전을 다시 게시합니다. https://stackoverflow.com/questions/47041415/include-sparkline-htmlwidget-in-datatable-cells-in-a-shiny-app-without-resortin 솔루션은 JavaScript를 수동으로 입력 할 필요가 없습니다. 뷰어에서 작동하는 경우 왜 샤이닝을 사용하지 않습니까? –

답변

3

스파크 라인을 생성하도록 코드를 수정했습니다. 스파크 라인을 생성하려면이 link을 추천했습니다.

require(sparkline) 
require(DT) 
require(shiny) 

# create data 
spark_data1<- data.frame(id = c('spark1', 'spark2'), 
          spark = c("1,2,3", "3,2,1")) 



ui <- fluidPage(
    sparklineOutput("test_spark"), 
    DT::dataTableOutput("tbl") 
) 

server <- function(input, output) { 

    line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'" 

    cd <- list(list(targets = 1, render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }"))) 

    cb = JS(paste0("function (oSettings, json) {\n $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", 
       line_string, " });\n}"), collapse = "") 

    output$tbl <- DT::renderDataTable({ 
    dt <- DT::datatable(as.data.frame(spark_data1), rownames = FALSE, options = list(columnDefs = cd,fnDrawCallback = cb)) 
    }) 
} 

shinyApp(ui = ui, server = server) 

희망 하시겠습니까?

+0

매력처럼 작동합니다! 고맙습니다 – Grant

1

오래된 질문입니다. 질문에 대한 정보를 바탕으로 대답을 작성했습니다. Add label to sparkline plot in datatable 해결책은 처음에 몇 줄을 더한 것뿐입니다. 여기서는 뷰어에서 작동하는 데모 데모 파트를 잘라 내고 스파크 라인을 작동시키는 데 필요한 것을 추가했습니다.

# dependencies 
require(sparkline) 
require(DT) 
require(shiny) 

# create data with sparklines 
spark_data <- data.frame(
    id = c('spark1', 'spark2'), 
    spark = c(
    spk_chr(values = 1:3, elementId = 'spark1'), 
    spk_chr(values = 3:1, elementId = 'spark2') 
) 
) 

### adding this <------------ 
cb <- htmlwidgets::JS('function(){debugger;HTMLWidgets.staticRender();}') 

ui <- fluidPage(
    ### and this <------------ 
    htmlwidgets::getDependency('sparkline'), 
    dataTableOutput("tbl") 
) 

server <- function(input, output) { 

    output$tbl <- renderDataTable(
    expr = spark_data, 
    escape = FALSE, 
    ### and this <------------ 
    options = list(
     drawCallback = cb 
    ) 
) 
} 

shinyApp(ui = ui, server = server)