下载rpivotTable输出闪亮 [英] download rpivotTable ouput in shiny
问题描述
我发现一个有趣的包 rpivotTable
。
我想创建闪亮的应用程序
,其中包括 rpivotTable
,可以使用 downloadHandler
。
但是,我无法找到解决方案,如何创建数据。框架
或其他可以传递给 downloadHandler
功能的其他内容。
rpivotTable
创建一个类的对象:
class(pivot)
[1]rpivotTablehtmlwidget
是thren任何下载输出的可能性的这个功能?
另外,我附上了一个例子,如何创建闪光的枢轴以及我想使用的下载功能的例子。 p>
也许是任何其他想法或建议吗?
set.seed 1992)
n = 99
年份< - sample(2013:2015,n,replace = TRUE,prob = NULL)
Month< - sample(1:12,n,replace = TRUE,prob = NULL)
类别< - sample(c(Car,Bus,Bike),n,replace = TRUE,prob = NULL)
品牌< ,n,replace = TRUE,prob = NULL)
品牌< - paste0(品牌,样本(1:14,n,替换= TRUE,prob = NULL))
USD < rnorm(n))* 100
df< - data.frame(年,月,类别,品牌,美元)
输出$ Pivot< - rpivotTable :: renderRpivotTable({
rpivotTable(data = df,rows =Brand,col =Category,vals =USD,aggregatorName =Sum,rendererName =Table )
})
输出$ downloadData< - downloadHandler(
filename = function(){paste(filename,'.csv' sep ='')},
content = function(file){
write.csv(PivotOutput,file)
})
我刚刚在github上推送了rpivotTable的主分支,解决了获取用户参数的问题是/已经在服务器端看过e。
使用 devtools
下载 rpivotTable
代码:
devtools :: install_github(smartinsightsfromdata / rpivotTable,ref =master)
这是一个如何在服务器端获取所选数据的示例。该示例不完整满足您的需求:您需要将原始数据框架与从rpivotTable返回的内容进行子集。但是这应该足够给你一个开端。
库(rpivotTable)
库(闪亮)
list_to_string< - function(obj,listname){
if(is.null(names(obj))){
paste(listname,[[,seq_along )]] =,obj,
sep =,collapse =\\\
)
} else {
粘贴(listname,$,names(obj) ,=,obj,
sep =,collapse =\\\
)
}
}
服务器&输出){
输出$ pivotRefresh< - renderText({
cnames< - list(cols,rows,vals aggregateatorName,rendererName)
#对所有键应用一个函数,以获取相应的值
allvalues< - lapply(cnames,function(name){
item& $ myPivotData [[name]]
if(is.list(item)){
list_to_string(item,name)
} else {
paste(name,item,sep = =)
}
})
贴(allvalues,collapse =\\\
)
})
输出$ mypivot = renderRpivotTable({
rpivotTable(data = cars,onRefresh = htmlwidgets :: JS(function(config){Shiny.onInputChange('myPivotData' } $)
ui< - shinyUI(fluidPage(
fluidRow(column(6,verbatimTextOutput(pivotRefresh)),
列(6,rpivotTableOutput(mypivot)))
)
)
shinyApp(ui = ui,server = server)
I've found an interesting package rpivotTable
.
I'd like to create shiny app
which includes rpivotTable
with the possibility to download generated data using downloadHandler
.
However, I am unable to find the solution, how to create data.frame
or something else which I'd be able to pass to the downloadHandler
function.
rpivotTable
creates an object of class:
class(pivot)
[1] "rpivotTable" "htmlwidget"
Is threne any possibilities to download the output of the this function?
Also, I enclose the example, how the pivot is created in shiny and the example of download function which I'd like to use.
Maybe are the any other ideas or suggestions?
set.seed(1992)
n=99
Year <- sample(2013:2015, n, replace = TRUE, prob = NULL)
Month <- sample(1:12, n, replace = TRUE, prob = NULL)
Category <- sample(c("Car", "Bus", "Bike"), n, replace = TRUE, prob = NULL)
Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
Brand <- paste0(Brand, sample(1:14, n, replace = TRUE, prob = NULL))
USD <- abs(rnorm(n))*100
df <- data.frame(Year, Month, Category, Brand, USD)
output$Pivot <- rpivotTable::renderRpivotTable({
rpivotTable(data = df, rows = "Brand", col = "Category", vals = "USD", aggregatorName = "Sum", rendererName = "Table")
})
output$downloadData <- downloadHandler(
filename = function() { paste(filename, '.csv', sep='') },
content = function(file) {
write.csv(PivotOutput, file)
})
I've just pushed on the master branch of rpivotTable on github a change that addresses the issue of getting the parameters the user is / has looked at on the server side.
Download the rpivotTable
code with devtools
:
devtools::install_github("smartinsightsfromdata/rpivotTable",ref="master")
This is an example of how to get the selected data on the server side. The example is not complete for your needs: you need to subset the original data frame with what you get back from rpivotTable. But this should be enough to give you an head start.
library(rpivotTable)
library(shiny)
list_to_string <- function(obj, listname) {
if (is.null(names(obj))) {
paste(listname, "[[", seq_along(obj), "]] = ", obj,
sep = "", collapse = "\n")
} else {
paste(listname, "$", names(obj), " = ", obj,
sep = "", collapse = "\n")
}
}
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, sep=" = ")
}
})
paste(allvalues, collapse = "\n")
})
output$mypivot = renderRpivotTable({
rpivotTable(data=cars, onRefresh=htmlwidgets::JS("function(config) { Shiny.onInputChange('myPivotData', config); }"))
})
}
ui <- shinyUI(fluidPage(
fluidRow(column(6, verbatimTextOutput("pivotRefresh")),
column(6, rpivotTableOutput("mypivot") ))
)
)
shinyApp(ui = ui, server = server)
这篇关于下载rpivotTable输出闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!