【问题标题】:User-interactive table made from reactive data由反应数据制成的用户交互表
【发布时间】:2019-10-30 13:59:39
【问题描述】:

我对 Shiny 很陌生,很难理解反应性。

上下文:我希望用户为列选择一个名称,将此列添加到反应表中,然后编辑此表。该表是反应式的(它来自用户过滤的上传文件)。

感谢this 回答非反应性表一切正常(请参阅mydata <- mtcars[1:5,])。 但是当mydata 变成响应式时它就不起作用了!

这是一个可重现的工作示例,其中包含来自 @dww 答案的非反应性数据:

library(rhandsontable)

ui <- fluidPage(
  h2("The mtcars data"),
  rHandsontableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  radioButtons("type", "Column type:",
    c("Integer" = "integer",
      "Floating point" = "numeric",
      "Text" = "character")),
  actionButton("goButton", "Update Table")
)

server <- function(input, output) {
  mydata <- mtcars[1:5,]
  output$mytable = renderRHandsontable(df())
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      if (input$type == "integer") v1 <- integer(NROW(mydata))
      if (input$type == "numeric") v1 <- numeric(NROW(mydata))
      if (input$type == "character") v1 <- character(NROW(mydata))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata, stretchH = "all")
  }, ignoreNULL = FALSE)
  observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))
}

shinyApp(ui,server)

我没有成功地在代码中尝试这些更改(基本上我已经将所有mydata 更改为mydata()):

server <- function(input, output) {

# mydata <- reactive({ }) #make mydata a reactive object

output$mytable = renderRHandsontable(df())
df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
        if (input$type == "integer") v1 <- integer(NROW(mydata()))
        if (input$type == "numeric") v1 <- numeric(NROW(mydata()))
        if (input$type == "character") v1 <- character(NROW(mydata()))
        newcol <- data.frame(v1)
        names(newcol) <- input$NewCol
        mydata <<- cbind(mydata(), newcol)
    }
    rhandsontable(mydata(), stretchH = "all")
}, ignoreNULL = FALSE)
observe(if (!is.null(input$mytable)) mydata() <<- hot_to_r(input$mytable))}

我没有发现 this question answers/cmets 对回答我的问题有用)。

您能解释一下如何在@dww 很棒的答案中使用反应式mydata 吗?

[编辑:标题已更新以更好地适应答案]

【问题讨论】:

    标签: r shiny reactive


    【解决方案1】:

    我删减了一些额外的功能,例如列数据类型...作为一般规则 - 您要呈现的任何内容都可以通过将其包装在“反应性”中而变为反应性。下面我使用“reactiveValues”,但其他反应方法也可以。

    一种使您的输出响应数据输入变化的通用方法 -

    foo_func = function() return(mydata)
    foo_func_reactive = reactive(foo_func)
    output$foo = renderMethod( foo_func_reactive() )
    

    你的例子:

    shinyApp(
    
    ui = fluidPage(
      rHandsontableOutput("out_tbl"),
      textInput(inputId = "in_txt", label = "New column name"),
      actionButton(inputId = "in_btn1", label = "Add new column to the table above ..."),
      actionButton(inputId = "in_btn2", label = "... Or, generate new data")
    ),
    
    
    server = function(input, output, session) {
    
      # establishes tbl_react as the holder for our reactive data, and pre-fills it for the first display with 1,2,3
      tbl_react <- reactiveValues(tbl = 
        data.frame(a = c(1,2,3))
      )
    
       # button one adds a new column with the inputted name
      observeEvent(input$in_btn1,{
        newcolname <- as.character(input$in_txt)
        newcol <- character(NROW(tbl_react$tbl))
        tbl_react$tbl <- cbind(tbl_react$tbl, newcol)
        colnames(tbl_react$tbl) <- c(colnames(tbl_react$tbl)[1:ncol(tbl_react$tbl)-1], newcolname)
      })
    
      # to show our output data is reactive, we can take a dependancy on button two to generate new data - this could instead be using an uploaded file
      observeEvent(input$in_btn2,{
        tbl_react$tbl <- data.frame(b = c(9,10,11))
      })
    
    
      output$out_tbl = renderRHandsontable( rhandsontable(tbl_react$tbl) )
    
    
      }
    )
    

    【讨论】:

    • 非常感谢您的回答,您的代码内 cmets 对解决我的问题很有帮助!
    • 但有一个问题,当您添加一列,对其进行编辑然后再次添加一列时,它会删除以前文件中的值。你如何避免这种情况?
    • 编辑:我的问题中的可复制前代码在添加列时不会删除填充值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2020-06-11
    相关资源
    最近更新 更多