Shiny:如何将rhandson表重置为默认值?

时间:2017-01-05 15:53:08

标签: r shiny handsontable shinyjs rhandsontable

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

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除外。

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

1 个答案:

答案 0 :(得分:0)

在rhandsontable上添加reactiveValue和更多细节可以完成工作,但这可能效率不高:

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