我想问一个关于使用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降价: RMD:
---
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
!
非常感谢你!
祝你好运!
答案 0 :(得分:2)
do.call
将是解决方案。我们应该使用列表,而不是尝试将参数作为字符串化列表传递。以下是我认为可以实现目标的变更代码。您将看到带有更改的注释,以便将其插入到整个示例中。我将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)
如果您有任何其他问题,请与我们联系。