在闪亮的应用程序中编辑rhsonsontable后更新selectInput()选择

时间:2018-08-23 11:39:02

标签: r shiny

我有一个简单的闪亮应用程序,其中使用numericInput()“测试”将行添加到数据框。然后,将“标签”列的名称作为selectInput()“标签2”的选项。问题是,当我编辑表的“标签”列中的名称时,selectInput()选项不会相应地更新。例如,如果我在表中将“测试1”重命名为“测试A”,我也希望它在selectInput()中进行更改。

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

  )
}

1 个答案:

答案 0 :(得分:2)

这似乎有效。您没有回读编辑过的rhandsontable 在您的代码中。 为此,我添加了一个observe

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

在代码中,我还添加了一些req语句以在初始化时检查NULL条件,还可以使用在其他一些问题中使用过的if..else机制。 / p>

 #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)
相关问题