【问题标题】:rpivotTable: dynamic pdf download of pivot table of shinyapp with refreshed adjustmentrpivotTable: 更新调整的shinyapp 数据透视表的动态pdf下载
【发布时间】:2017-04-21 08:05:24
【问题描述】:

我想问一个关于使用rpivotTable包更新调整后的数据透视表pdf下载的问题。

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

这是我的代码:

闪亮的应用程序: 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)

```

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

其他的都已经设置好了,唯一的问题是如何把参数##### Replace "pivotRefresh2()" Here!

非常感谢!

最好的问候!

【问题讨论】:

    标签: r pdf shiny htmlwidgets rpivottable


    【解决方案1】:

    我们的朋友 do.call

    如果我理解正确,do.call 将是解决方案。我们应该使用列表,而不是尝试将参数作为字符串化列表传递。以下是我认为可以实现您的目标的更改代码。您将看到 cmets 进行了更改以将其插入到您的整个示例中。我将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) 
    

    跟进

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

    【讨论】:

    • 哇!!!有用!太感谢了!唯一应该更改的是将PivotTable2&lt;-observe({ rp &lt;&lt;- do.call(rpivotTable,c(list(data=cars),pivotRefresh2())) }) 更改为PivotTable2&lt;-reactive({ rp &lt;&lt;- do.call(rpivotTable,c(list(data=cars),pivotRefresh2())) }),因为由于某种原因,Rmarkdown 只能对reactive() 有效。我非常感谢您的解决方案。
    猜你喜欢
    • 2017-02-10
    • 2017-07-30
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多