【问题标题】:Shiny: How to reset rhandson table to default?Shiny:如何将 rhandson 表重置为默认值?
【发布时间】:2017-05-20 05:52:58
【问题描述】:

我有一个像这样的小应用程序:

require(shiny)
require(shinyjs)
require(rhandsontable)

shinyApp(ui = fluidPage(useShinyjs(),
                        div(id = 'div1',
                            titlePanel("RHOT - Form"),
                            fluidRow(column(width = 3,selectizeInput("Trialid","What Iteration is this?",choices = c('1','2-3','4-7','8-15'))),
                                     column(width = 3,textInput("Techie_Name","Your Name",value='EE')),
                                     column(width = 3,textInput("lab_id","LAB ID",value='NA')),
                                     column(width = 3,textInput("email","Your Email ID",value='eeshanchatterjee@gmail.com'))
                            ),
                            h4('Observations:'),
                            rHandsontableOutput("handsontable_obs"),
                            actionButton("SaveObs", "Save Observations")
                            ),
                        shinyjs::hidden(div(id = 'SubmitMsg',
                                            h3("Thanks for submitting the Observations!"),
                                            actionLink('addNextObs',"Add Another Observation"))
                                        )
                        ),
         server = function(input, output,session){
           output$handsontable_obs = renderRHandsontable({
             rhandsontable(data.frame(Obs_itr = c(1:5),
                                      Val1 = rep(0,5),
                                      Val2 = rep(0,5)))
           })

           observeEvent(input$SaveObs,{
             shinyjs::reset("div1")
             shinyjs::hide("div1")
             shinyjs::show("SubmitMsg")
           })

           observeEvent(input$addNextObs,{
             shinyjs::show("div1")
             shinyjs::hide("SubmitMsg")
           })
         }
)

当我运行它时,我可以编辑输入字段以及表格。点击保存按钮后,此 div 将重置(使用 shinyjs::reset)、隐藏,并显示隐藏的谢谢 div。 单击第二个 div 上的另一个操作链接会重新打开原始操作链接。 现在,除了handsontable 之外,输入字段已重置为其默认值。

问题是,我如何确保handsontable 与其他输入字段一起重置为默认值?

【问题讨论】:

  • 只是对 shinyjs::reset 的评论——它只适用于原生闪亮元素。预计不适用于 rhandsontable 或其他附加小部件
  • 我想了这么多-我实际上正在寻找一些解决方法的想法:)
  • 您可以编写自己的js函数将值重置为null。

标签: r shiny handsontable shinyjs rhandsontable


【解决方案1】:

添加reactiveValue 和更多关于 rhandsontable 的详细信息可以完成工作,但这可能效率不高:

shinyApp(ui = fluidPage(useShinyjs(),
                        div(id = 'div1',
                            titlePanel("RHOT - Form"),
                            fluidRow(column(width = 3,selectizeInput("Trialid","What Iteration is this?",choices = c('1','2-3','4-7','8-15'))),
                                     column(width = 3,textInput("Techie_Name","Your Name",value='EE')),
                                     column(width = 3,textInput("lab_id","LAB ID",value='NA')),
                                     column(width = 3,textInput("email","Your Email ID",value='eeshanchatterjee@gmail.com'))
                            ),
                            h4('Observations:'),
                            rHandsontableOutput("handsontable_obs"),
                            actionButton("SaveObs", "Save Observations")
                        ),
                        shinyjs::hidden(div(id = 'SubmitMsg',
                                            h3("Thanks for submitting the Observations!"),
                                            actionLink('addNextObs',"Add Another Observation"))
                        )
),
server = function(input, output,session){

  vals <- reactiveValues(reset=TRUE)

  output$handsontable_obs = renderRHandsontable({
    input$addNextObs
    if(isolate(vals$reset) | is.null(input$handsontable_obs)) {
      isolate(vals$reset <- FALSE)
      df <- data.frame(Obs_itr = c(1:5),
                       Val1 = rep(0,5),
                       Val2 = rep(0,5))
    } else df <- hot_to_r(input$handsontable_obs)
    rhandsontable(df)
  })

  observeEvent(input$SaveObs,{
    shinyjs::reset("div1")
    shinyjs::hide("div1")
    shinyjs::show("SubmitMsg")
    vals$reset <- TRUE
  })

  observeEvent(input$addNextObs,{
    shinyjs::show("div1")
    shinyjs::hide("SubmitMsg")
  })
}
)

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 2016-04-11
    • 2011-04-06
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 2013-12-17
    相关资源
    最近更新 更多