【问题标题】:r - input value by user to dataframe via shinyr - 用户通过闪亮的数据框输入值
【发布时间】:2016-03-31 20:11:29
【问题描述】:

我正在构建一个应用程序,允许用户从selectizeInputcheckboxInput 传递值以形成数据框。我搜索了一段时间,发现这些来源与我的预期相似:

  1. 实用

来自这里:https://github.com/jrowen/rhandsontable。我的和这个例子很相似:

shiny::runGitHub("jrowen/rhandsontable",
                 subdir = "inst/examples/rhandsontable_portfolio")

但我想使用闪亮的小部件将值传递给数据框。它应该能够添加/删除行,如下例所示:

  1. 闪亮的孵化器

代码在这里:

library("shiny")
library('devtools')
install_github('shiny-incubator', 'rstudio')
library("shinyIncubator")

# initialize data with colnames
df <- data.frame(matrix(c("0","0"), 1, 2))
colnames(df) <- c("Input1", "Input2")
    server = shinyServer(
      function(input, output) {
        # table of outputs
        output$table.output <- renderTable(
          { res <- matrix(apply(input$data,1,prod))
          res <- do.call(cbind, list(input$data, res))
          colnames(res) <- c("Input 1","Input 2","Product")
          res
          }
          , include.rownames = FALSE
          , include.colnames = TRUE
          , align = "cccc"
          , digits = 2
          , sanitize.text.function = function(x) x      
        )  
      }
    )

ui = shinyUI(
  pageWithSidebar(

    headerPanel('Simple matrixInput example')

,
sidebarPanel(

  # customize display settings    
  tags$head(
    tags$style(type='text/css'
               , "table.data { width: 300px; }"
               , ".well {width: 80%; background-color: NULL; border: 0px solid rgb(255, 255, 255); box-shadow: 0px 0px 0px rgb(255, 255, 255) inset;}"
               , ".tableinput .hide {display: table-header-group; color: black; align-items: center; text-align: center; align-self: center;}"
               , ".tableinput-container {width: 100%; text-align: center;}"
               , ".tableinput-buttons {margin: 10px;}"
               , ".data {background-color: rgb(255,255,255);}"
               , ".table th, .table td {text-align: center;}"

    )
  )
  ,
  wellPanel(
    h4("Input Table")
    ,      
    matrixInput(inputId = 'data', label = 'Add/Remove Rows', data = df)
    , 
    helpText("This table accepts user input into each cell. The number of rows may be controlled by pressing the +/- buttons.")
  )
)
,
mainPanel(
  wellPanel(
    wellPanel(
      h4("Output Table")
      ,
      tableOutput(outputId = 'table.output')
      ,
      helpText("This table displays the input matrix together with the product of the rows of the input matrix")
    )
  )
    )
  )
)
runApp(list(ui = ui, server = server))

用户应从 selectizeInputcheckboxInputtextInput 等闪亮的小部件输入该值,并在用户单击我的 actionButton 后传递给数据框。我想要的与上述功能的组合非常相似,但我不知道该怎么做。有什么建议吗?

非常感谢。

【问题讨论】:

  • 最简单的方法是直接使用rhandsontable(可以编辑、添加和删除行)。可以使用 textInput 和 actionButton 的组合自己构建它(在服务器代码中,使用observeEvent 观察按钮的点击,然后从 textInput 获取值并构建您的数据框)
  • 我现在可以通过闪亮的小部件传递价值,但仍然不知道如何添加/删除行。另外,我需要在添加新行后修复上一行。也就是说,如果我按submit,我的第一行将使用默认值,这些默认值可能会随着selectizeInputtextInput 的值而改变,因为现在我只有一行。然后,在我submit 使用新值的新行之后,第一行的值应该是固定的,即不会随着输入而改变,因为第二行会。
  • 您需要三个不同的按钮:“插入”、“更新”和“删除”。对于后两者,您还需要一个额外的输入来指定要更新或删除哪一行。
  • @warmoverflow 谢谢。我想我找到了解决方案:)

标签: r shiny


【解决方案1】:

虽然我最终没有使用这两个包,但效果很好:

library(shiny)

server = shinyServer(function(input, output, session){

  values <- reactiveValues()

values$DT <- data.frame(Name = NA,
                        status = NA,
                        compare = NA,
                        stringsAsFactors = FALSE)

newEntry <- observeEvent(input$addrow, {
    newLine <- c(input$textIn, input$boxIn, input$selectIn)
    values$DT <- rbind(values$DT, newLine)
})

newEntry <- observeEvent(input$revrow, {
  deleteLine <- values$DT[-nrow(values$DT), ]
  values$DT <- deleteLine
})

  output$table <- renderTable({
    values$DT
  })

})

ui = shinyUI(navbarPage(
  "Backtest System", inverse = TRUE, id = "navbar",
  tabPanel("Strategy",
           sidebarLayout(
             sidebarPanel(
               h4("Indicator"),
               textInput("textIn", "Text", "try"),
               checkboxInput("boxIn", "Box", TRUE),
               selectizeInput("selectIn", "Select", 
                              choices = c(">" = ">",
                                          ">=" = ">=",
                                          "<" = "<",
                                          "<=" = "<=")),
               actionButton("addrow", "Add Row"),
               actionButton("revrow", "Remove Row")
             ),
             mainPanel(
               tableOutput("table")
             )
           )
  )
  )
)
runApp(list(ui = ui, server = server))

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 2016-02-09
    • 2014-02-26
    • 1970-01-01
    • 2018-09-23
    • 2021-06-21
    • 2021-08-10
    • 2021-10-31
    • 2017-08-17
    相关资源
    最近更新 更多