我想构建一个闪亮的应用程序,它将矩阵数据作为输入,并根据输出的某些操作返回一个表.通过搜索我发现ShinyTable包可能很有用.我尝试了下面闪亮的代码,但结果应用程序显示为灰色,没有结果.
library(shinyTable)
shiny::runApp(list(
ui=pageWithSidebar(
headerPanel('Simple matrixInput')
,
sidebarPanel(
htable("tbl")
,
submitButton("OK")
)
,
mainPanel(
tableOutput(outputId = 'table.output')
))
,
server=function(input, output){
output$table.output <- renderTable({
input$tbl^2
}
, sanitize.text.function = function(x) x
)
}
))
Run Code Online (Sandbox Code Playgroud)
任何的想法?
Sté*_*ent 20
该shinyTable包已在有了很大的提高rhandsontable包.
这是一个最小的函数,它采用数据框并运行一个闪亮的应用程序,允许编辑它并将其保存在rds文件中:
library(rhandsontable)
library(shiny)
editTable <- function(DF, outdir=getwd(), outfilename="table"){
ui <- shinyUI(fluidPage(
titlePanel("Edit and save a table"),
sidebarLayout(
sidebarPanel(
helpText("Shiny app based on an example given in the rhandsontable package.",
"Right-click on the table to delete/insert rows.",
"Double-click on a cell to edit"),
wellPanel(
h3("Table options"),
radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
),
br(),
wellPanel(
h3("Save"),
actionButton("save", "Save table")
)
),
mainPanel(
rHandsontableOutput("hot")
)
)
))
server <- shinyServer(function(input, output) {
values <- reactiveValues()
## Handsontable
observe({
if (!is.null(input$hot)) {
DF = hot_to_r(input$hot)
} else {
if (is.null(values[["DF"]]))
DF <- DF
else
DF <- values[["DF"]]
}
values[["DF"]] <- DF
})
output$hot <- renderRHandsontable({
DF <- values[["DF"]]
if (!is.null(DF))
rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
})
## Save
observeEvent(input$save, {
finalDF <- isolate(values[["DF"]])
saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename)))
})
})
## run app
runApp(list(ui=ui, server=server))
return(invisible())
}
Run Code Online (Sandbox Code Playgroud)
例如,采用以下数据框:
> ( DF <- data.frame(Value = 1:10, Status = TRUE, Name = LETTERS[1:10],
Date = seq(from = Sys.Date(), by = "days", length.out = 10),
stringsAsFactors = FALSE) )
Value Status Name Date
1 1 TRUE A 2016-08-15
2 2 TRUE B 2016-08-16
3 3 TRUE C 2016-08-17
4 4 TRUE D 2016-08-18
5 5 TRUE E 2016-08-19
6 6 TRUE F 2016-08-20
7 7 TRUE G 2016-08-21
8 8 TRUE H 2016-08-22
9 9 TRUE I 2016-08-23
10 10 TRUE J 2016-08-24
Run Code Online (Sandbox Code Playgroud)
运行应用程序并享受乐趣(尤其是日历^^):
编辑handontable:
单击" 保存"按钮.它将表保存在文件中table.rds.然后在R中阅读:
> readRDS("table.rds")
Value Status Name Date
1 1000 FALSE Mahmoud 2016-01-01
2 2000 FALSE B 2016-08-16
3 3 FALSE C 2016-08-17
4 4 TRUE D 2016-08-18
5 5 TRUE E 2016-08-19
6 6 TRUE F 2016-08-20
7 7 TRUE G 2016-08-21
8 8 TRUE H 2016-08-22
9 9 TRUE I 2016-08-23
10 10 TRUE J 2016-08-24
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找用户可以像在Excel中一样输入矩阵数据的解决方案,您可以查看"shinySky"软件包,更具体地说,查看其组件"Handsontable Input/Output".相关的网址是:https://github.com/AnalytixWare/ShinySky.
另一个类似的解决方案是包shineTable.您可以在https://github.com/trestletech/shinyTable上找到更多信息