Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在R中打印带有plot和datatable的活动tabPanel?_Javascript_Css_R_Shiny_Shinyjs - Fatal编程技术网

Javascript 如何在R中打印带有plot和datatable的活动tabPanel?

Javascript 如何在R中打印带有plot和datatable的活动tabPanel?,javascript,css,r,shiny,shinyjs,Javascript,Css,R,Shiny,Shinyjs,我正在尝试将活动选项卡面板打印为屏幕上显示的内容,并使用闪亮的“打印”按钮。选项卡面板可以包含各种绘图和数据表 我在下面制作了一个可复制的示例,下面代码的问题如下所示 数据表在打印和 打印时无法对齐到中心 library(shiny) library(ggplot2) # for the diamonds dataset library(shinyjs) library(DT) library(dplyr) jsCode <- "shinyjs.winprint = function(

我正在尝试将活动选项卡面板打印为屏幕上显示的内容,并使用闪亮的“打印”按钮。选项卡面板可以包含各种绘图和数据表

我在下面制作了一个可复制的示例,下面代码的问题如下所示 数据表在打印和 打印时无法对齐到中心

library(shiny)
library(ggplot2)  # for the diamonds dataset
library(shinyjs)
library(DT)
library(dplyr)

jsCode <- "shinyjs.winprint = function(){

var mywindow = window.open('', 'PRINT', 'height=400,width=600');

mywindow.document.write('</head><body >');
mywindow.document.write(document.getElementsByClassName('tab-pane active')[0].innerHTML);
mywindow.document.write('</body></html>');

mywindow.print();
mywindow.close();

return true;
}"

ui <- fluidPage(
  title = "Examples of DataTables",
  sidebarLayout(
    sidebarPanel(
      conditionalPanel(
        'input.dataset === "diamonds"',
        checkboxGroupInput("show_vars", "Columns in diamonds to show:",
                           names(diamonds), selected = names(diamonds))
      ),
      conditionalPanel(
        'input.dataset === "mtcars"',
        helpText("Click the column header to sort a column.")
      ),
      conditionalPanel(
        'input.dataset === "iris"',
        helpText("Display 5 records by default.")
      ),
      useShinyjs(),
      extendShinyjs(text = jsCode),
      actionButton("print", "PRINT")
    ),
    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("diamonds", DT::dataTableOutput("mytable1"), 
                 plotOutput("plot1")),
        tabPanel("mtcars", DT::dataTableOutput("mytable2")),
        tabPanel("iris", DT::dataTableOutput("mytable3"))
      )
    )
  )
)

server <- function(input, output) {

  # choose columns to display
  diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]

  output$mytable1 <- DT::renderDataTable(
    DT::datatable(diamonds2[diamonds2$color %in% c("H","D"), input$show_vars, drop = FALSE]) %>% 
      DT::formatStyle("color", target = "row",
                      backgroundColor = styleEqual(c("H", "D"), c('red', 'white')))
  )

  output$plot1 <- renderPlot({
    plot(cars, type="p")
  })
  # sorted columns are colored now because CSS are attached to them
  output$mytable2 <- DT::renderDataTable({
    DT::datatable(mtcars, options = list(orderClasses = TRUE))
  })

  # customize the length drop-down menu; display 5 rows per page by default
  output$mytable3 <- DT::renderDataTable({
    DT::datatable(iris, options = list(lengthMenu = c(5, 30, 50), pageLength = 5))
  })
  observeEvent(input$print, {
    js$winprint()
  })


}

shinyApp(ui, server)
库(闪亮)
库(ggplot2)#用于钻石数据集
图书馆(shinyjs)
图书馆(DT)
图书馆(dplyr)
jsCode