【问题标题】:Data input via shinyTable in R shiny application在 R 闪亮的应用程序中通过闪亮表输入数据
【发布时间】:2014-04-11 22:15:20
【问题描述】:

我想构建一个闪亮的应用程序,它获取矩阵数据作为输入,并根据对它的一些操作返回一个表作为输出。通过搜索,我发现 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 
    )
  }
))

有什么想法吗?

【问题讨论】:

  • 这是一个已知问题,它发生在新版本的闪亮“因为闪亮现在处理自定义输入的方式”参见:groups.google.com/forum/#!topic/shiny-discuss/F8aAtv85ZGs
  • 谢谢朱利安,我们有希望解决吗?
  • 请查看stackoverflow.com/questions/21910853/subsetting-shinytable 以及我在那里使用的shinyshinyTable 版本。
  • 我尝试像您一样安装和加载闪亮表和闪亮表,但出现此错误:“get(Info[i, 1], envir = env) 中的错误:延迟加载数据库 'P '已损坏另外:警告消息:在get(Info [i,1],envir = env)中:R_decompress1中的内部错误-3错误:'shiny'的包或命名空间加载失败“

标签: r shiny


【解决方案1】:

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

运行应用程序并享受乐趣(尤其是日历^^):

编辑handsontable

点击保存按钮。它将表格保存在文件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

【讨论】:

  • 嗨@Stéphane Laurent:我是新来的闪亮,我试图了解您解决方案的观察者中的代码,这个代码块的目的是什么?如果我跳过这段代码,我会错过什么功能?
【解决方案2】:

如果您正在寻找一种解决方案,用户可以像在 excel 中一样输入他们的矩阵数据,您可能会查看“shinySky”包,更具体地说是它的组件“Handsontable Input/Output”。相关网址为:https://github.com/AnalytixWare/ShinySky

另一个类似的解决方案是shinyTable 包。您可以在https://github.com/trestletech/shinyTable找到更多信息

【讨论】:

    【解决方案3】:

    您可以使用shinysky package 中的hotable("matrixTable")

    library(shiny, shinysky)
    shinyApp(
      ui     = shinyUI (wellPanel(hotable("matrixTable"),hotable("resultTable"))),
    
      server = shinyServer (function(input, output) {
        A = matrix(c(1:6), nrow=2) # init - input matrix A
        output$matrixTable <- renderHotable({data.frame(A)}, readOnly = FALSE)
    
        R = matrix(rep(0,6), nrow=2) # init - result matrix R
        output$resultTable <- renderHotable({data.frame(R)}, readOnly = TRUE)
    
        observe({  # process matrix
          df <- hot.to.df(input$matrixTable)
          if(!is.null(df)) {    # ensure data frame from table exists
            B = data.matrix(df) # ensure its numeric
            R = B^2             # some matrix operation
            output$resultTable <- renderHotable({data.frame(R)})
          }
        }) # end of observe
      }) # end of server
    )
    

    在用户界面ui 中,输入"matrixTable""resultTable" 被可视化。 server 初始化这些表,以便可以编辑 matrixTable。这意味着您可以从 Excel 复制和粘贴数据,或手动更改值。每当注意到输入 matrixTable 发生变化时,就会激活 observe 功能。我们从该表中提取带有ho.to.df 的数据框df。如果不是NULL,我们将其转换为矩阵,并应用一些矩阵运算(例如,对每个元素求平方)并将输出作为新矩阵返回。

    这个解决方案是通过克里斯托的建议和斯蒂芬的方法得到的。

    【讨论】:

    • 得到警告错误包“shinysky”不可用(适用于 R 版本 3.6.3)
    【解决方案4】:

    我不确定以下是否是您要查找的内容,但这里有。假设您可以将矩阵作为文本/csv 文件输入,那么对上面代码的以下修改将起作用。这直接来自 Shiny 教程:http://rstudio.github.io/shiny/tutorial/#uploads

    shiny::runApp(list(
        ui=pageWithSidebar(
            headerPanel('Simple matrixInput')
            ,
            sidebarPanel(
                fileInput('file1', 'Choose CSV File',
                  accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))
                ,
                tags$hr(),
                checkboxInput('header', 'Header', TRUE),
                radioButtons('sep', 'Separator',
                     c(Comma=',',
                       Semicolon=';',
                       Tab='\t'),
                     'Comma'),
                radioButtons('quote', 'Quote',
                     c(None='',
                       'Double Quote'='"',
                       'Single Quote'="'"),
                     'Double Quote')
            )
            ,
            mainPanel(
    
                tableOutput(outputId = 'table.output')
            ))
        ,
        server=function(input, output){
            output$table.output <- renderTable({
    
            inFile <- input$file1
    
           if (is.null(inFile))
            return(NULL)
    
            tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
            return(tbl^2)
            })
        }
    ))
    

    【讨论】:

    • 感谢您的回复 rmk。我对上传文件不感兴趣。我需要一个解决方案,用户可以在类似表格的 Excel 中输入他们的矩阵数据。
    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2020-06-16
    • 2016-12-20
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多