在R SHINY应用程序中通过shinyTable输入数据 [英] Data input via shinyTable in R shiny application
本文介绍了在R SHINY应用程序中通过shinyTable输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想构建一个闪亮的应用程序,该应用程序获取矩阵数据作为输入,并根据对其进行的一些操作返回一个表作为输出。通过搜索,我发现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
)
}
))
有什么想法吗?
推荐答案
shinyTable
包在rhandsontable
package中有了很大改进。
这里有一个最小的函数,它获取一个数据框并运行一个闪亮的应用程序,允许对其进行编辑并将其保存在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())
}
以下面的数据框为例:
> ( 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
运行应用程序并享受乐趣(尤其是使用日历^^):
编辑手持台:
单击保存按钮。它将表保存在文件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
这篇关于在R SHINY应用程序中通过shinyTable输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文