rpivotTable:动态 pdf 下载 shinyapp 的 pivot table,并进行了更新调整

rpivotTable: dynamic pdf download of pivot table of shinyapp with refreshed adjustment

我想问一个关于 pivot table pdf 下载的问题,使用 rpivotTable 包更新调整。

我非常接近我想要的,但只需要最后一步。

这是我的代码:

闪亮的应用程序: app.r:

library(rpivotTable)
library(shiny)
library(htmlwidgets)

list_to_string <- function(obj, listname) {
if (is.null(names(obj))) {
paste(listname, "=", list(obj) )
} else {
paste(listname, "=", list( obj ),
      sep = "", collapse = ",")
}
}

server <- function(input, output) {


output$pivotRefresh <- renderText({

cnames <- list("cols","rows","vals", "exclusions","aggregatorName",   "rendererName")
# Apply a function to all keys, to get corresponding values
allvalues <- lapply(cnames, function(name) {
  item <- input$myPivotData[[name]]
  if (is.list(item)) {
    list_to_string(item, name)
  } else {
    paste(name,"=","'",item,"'")
  }
 })
 paste(allvalues, collapse = ",")
 })



pivotRefresh2 <- reactive({
cnames <- list("cols","rows","vals", "exclusions","aggregatorName", "rendererName")
# Apply a function to all keys, to get corresponding values
allvalues <- lapply(cnames, function(name) {
  item <- input$myPivotData[[name]]
  if (is.list(item)) {
    list_to_string(item, name)
  } else {
    paste(name,"=","'",item,"'")
  }
 })
 paste(allvalues, collapse = ",")

 })


 PivotTable<-reactive({
 rpivotTable(data=cars, onRefresh=htmlwidgets::JS("function(config) {  Shiny.onInputChange('myPivotData', config); }"))
 })

 PivotTable2<-reactive({

rpivotTable(data=cars, 
##### Replace "pivotRefresh2()" Here
writeLines(pivotRefresh2()  )
)

})

output$mypivot = renderRpivotTable({
PivotTable()
})

output$report = downloadHandler(
filename<- function(){
  paste("Demo_Data_Analysis",Sys.Date(),".pdf",sep = "")
},
content = function(file) {
  src <- normalizePath('Apply.Rmd')

  # temporarily switch to the temp dir, in case you do not have write
  # permission to the current working directory
  owd <- setwd(tempdir())
  on.exit(setwd(owd))
  file.copy(src, 'Apply.Rmd', overwrite = TRUE)

  library(rmarkdown)
  out <- render('Apply.Rmd', pdf_document())
  file.rename(out, file)
 },
 contentType = 'application/pdf'
 )

 }

 ui <- shinyUI(fluidPage(
 fluidRow(column(6,verbatimTextOutput("pivotRefresh")),
       column(6, rpivotTableOutput("mypivot") )),
 downloadButton('report',"Download this plot")
 )
 )

 shinyApp(ui = ui, server = server) 

我对 pdf 的降价: 路径:

---
title: "Untitled"
author: "Statistician"
date: "December 3, 2016"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```


```{r Time_Single, out.width = "500px"}

saveWidget( PivotTable2(), file= 'temp_Time_single123.html')
respivot123 =  webshot::webshot('temp_Time_single123.html','my-  screenshotime_single123.png')
knitr::include_graphics(respivot123)

```

pivot table 顶部的文本输出是 rPivotTable 包的正确参数输入,所以我唯一需要做的就是将它们放入参数输入区域。我尝试 writeLines() 但它不起作用。

其他都已经设置好了,唯一的问题就是参数怎么放##### Replace "pivotRefresh2()" Here!

非常感谢!

此致!

我们的朋友do.call

如果我理解正确,

do.call 将是解决方案。我们不应该尝试将参数作为字符串化列表传递,而应该使用列表。下面是我认为可以完成您的更改的代码 objective。您将看到带有更改的注释,以将其插入到您的整个示例中。我将 rp 指定为全局变量,这样您就可以确保一切正常。您需要删除该分配,并将封闭的 observe 改回 reactive.

library(rpivotTable)
library(shiny)
library(htmlwidgets)

list_to_string <- function(obj, listname) {
  if (is.null(names(obj))) {
    paste(listname, "=", list(obj) )
  } else {
    paste(listname, "=", list( obj ),
          sep = "", collapse = ",")
  }
}

server <- function(input, output) {


  output$pivotRefresh <- renderText({

    cnames <- list("cols","rows","vals", "exclusions","aggregatorName",   "rendererName")
    # Apply a function to all keys, to get corresponding values
    allvalues <- lapply(cnames, function(name) {
      item <- input$myPivotData[[name]]
      if (is.list(item)) {
        list_to_string(item, name)
      } else {
        paste(name,"=","'",item,"'")
      }
    })
    paste(allvalues, collapse = ",")
  })



  pivotRefresh2 <- reactive({
    items <- input$myPivotData[c("cols","rows","vals", "exclusions","aggregatorName", "rendererName")]

    # need to remove the outside list container
    #  for rows and cols
    #  did not test thoroughly but these seemed to be
    #  the only two that require this
    items$cols <- unlist(items$cols,recursive=FALSE)
    items$rows <- unlist(items$rows,recursive=FALSE)

    items
  })

  PivotTable<-reactive({
    rpivotTable(data=cars, onRefresh=htmlwidgets::JS("function(config) {  Shiny.onInputChange('myPivotData', config); }"))
  })


  ########## add this to demo ###############
  ### what we are getting ###################
  observe({str(pivotRefresh2())})

  ########## change this back to reactive ##
  PivotTable2<-observe({
    ### do ugly global assign ################
    ### after done with Shiny ################
    ### rp available to inspect ##############
    rp <<- do.call(rpivotTable,c(list(data=cars),pivotRefresh2()))
  })

  output$mypivot = renderRpivotTable({
    PivotTable()
  })

  output$report = downloadHandler(
    filename<- function(){
      paste("Demo_Data_Analysis",Sys.Date(),".pdf",sep = "")
    },
    content = function(file) {
      src <- normalizePath('Apply.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'Apply.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('Apply.Rmd', pdf_document())
      file.rename(out, file)
    },
    contentType = 'application/pdf'
  )

}

ui <- shinyUI(fluidPage(
  fluidRow(column(6,verbatimTextOutput("pivotRefresh")),
           column(6, rpivotTableOutput("mypivot") )),
  downloadButton('report',"Download this plot")
)
)

shinyApp(ui = ui, server = server) 

跟进

如果您还有其他问题,请告诉我。