R 由反应数据生成的用户交互表

R 由反应数据生成的用户交互表,r,shiny,reactive,R,Shiny,Reactive,我对Shiny很陌生,很难理解它的反应性 上下文:我希望用户为列选择一个名称,将此列添加到反应表中,然后编辑此表。该表是被动的(它来自用户过滤的上传文件) 多亏了answer,使用非反应性表(请参见mydata我修剪了一些额外的功能,比如列数据类型…作为一般规则-任何您要呈现的内容都可以通过将其包装为“反应性”而变为反应性。下面我使用“反应性值”,但其他反应性方法也可以 使输出响应数据输入变化的通用方法- foo_func = function() return(mydata) foo_func

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

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


多亏了answer,使用非反应性表(请参见
mydata我修剪了一些额外的功能,比如列数据类型…作为一般规则-任何您要呈现的内容都可以通过将其包装为“反应性”而变为反应性。下面我使用“反应性值”,但其他反应性方法也可以

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

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) )


  }
)
shinyApp(
ui=fluidPage(
rHandsontableOutput(“输出tbl”),
textInput(inputId=“in_txt”,label=“新列名”),
actionButton(inputId=“in_btn1”,label=“向上表添加新列…”),
actionButton(inputId=“in_btn2”,label=“…或,生成新数据”)
),
服务器=功能(输入、输出、会话){
#建立tbl_react作为我们反应数据的持有者,并在第一次显示时用1,2,3预填充它

tbl_react非常感谢您的回答,您的代码内注释对解决我的问题非常有帮助!但有一个问题,当您添加一列,编辑它,然后再添加一列时,它会删除以前文件中的值。您如何避免这种情况?编辑:我问题中的可复制ex的代码在添加列时不会删除填充值
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) )


  }
)