简体   繁体   中英

Update selectInput() choices after editing rhandsontable in shiny app

I have a simple shiny app in which I use a numericInput() "tests" to add rows to the dataframe. Then I give the names of the "Label" column as choices to the selectInput() "Label2". The problem is that when I edit the names in column "Label" of the table the selectInput() choices are not updated accordingly. For example if I rename "Test 1" to "Test A" in the table I want it to change in the selectInput() as well.

#ui.r
library(shiny)
library(rhandsontable)
ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(
             sidebarPanel(
               uiOutput("tex2")
             ),
             mainPanel(
               rHandsontableOutput("hot3"),
               uiOutput("book12")

             )
           )))
#server.r
server <- function(input, output,session) {



  output$tex2<-renderUI({
    numericInput("text2", "#tests", value = 1, min=1)
  })

  output$book12<-renderUI({

    selectInput("bk12", 
                "Label2", 
                choices=(rt4()$Label))
  })
  rt4<-reactive({
    DF <- data.frame(
      Label=paste("Test",1:input$text2),
      stringsAsFactors = FALSE)

  })

  output$hot3 <-renderRHandsontable(
    rhandsontable(rt4())

  )
}

This seems to work. You were not reading back the edited rhandsontable in your code. So i ve added an observe to do this

 observe({
    if(!is.null(input$hot3))
      rt4$DF <- hot_to_r(input$hot3)
  })

Also in the code, Ive added some req statements to check for NULL conditions at the time of initialisation, you can use the if..else mechanism that you have used in some of your other questions too.

 #ui.r
library(shiny)
library(rhandsontable)
ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(
             sidebarPanel(
               uiOutput("tex2")
             ),
             mainPanel(
               rHandsontableOutput("hot3"),
               uiOutput("book12")

             )
           )))
#server.r
server <- function(input, output,session) {

  rt4<- reactiveValues()

  output$tex2<-renderUI({
    numericInput("text2", "#tests", value = 1, min=1)
  })

  output$book12<-renderUI({

    selectInput("bk12",
                "Label2",
                choices=(rt4$DF$Label))
  })


  observe({

    req(input$text2)

    rt4$DF <- data.frame(
      Test=paste(1:input$text2),
      Label=paste("Test",1:isolate(input$text2)),
      stringsAsFactors = FALSE)

  })
  output$hot3 <-renderRHandsontable({
    req(input$text2)
    rhandsontable(rt4$DF)
    } )

  observe({
    if(!is.null(input$hot3))
      rt4$DF <- hot_to_r(input$hot3)
  })
}

shinyApp(ui,server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM