我想问一个有关使用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
!
非常感谢!
最好的问候!
我们的朋友do.call
如果我正确理解, 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)
跟进
如果您还有其他问题,请告诉我。