【问题标题】:Update selectInput() choices after editing rhandsontable in shiny app在闪亮的应用程序中编辑 rhandsontable 后更新 selectInput() 选项
【发布时间】:2019-01-29 18:48:39
【问题描述】:

我有一个简单的闪亮应用程序,我在其中使用numericInput()“测试”向数据框添加行。然后我将“标签”列的名称作为selectInput()“标签2”的选择。问题是,当我在表格的“标签”列中编辑名称时,selectInput() 选项不会相应更新。例如,如果我在表中将“Test 1”重命名为“Test 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())

  )
}

【问题讨论】:

  • 为此查找 updateselectinput
  • 当我使用 updateSelectInput() 时,我认为“运算符对原子向量无效”

标签: r shiny


【解决方案1】:

这似乎有效。你没有读回编辑过的rhandsontable 在你的代码中。 所以我添加了一个observe 来做到这一点

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

同样在代码中,我添加了一些req 语句来在初始化时检查NULL 条件,您也可以使用您在其他一些问题中使用的if..else 机制。

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

【讨论】:

  • 这是一个特殊的答案。感谢您提供有用的修复!
  • 问题:'req(input$text2)' 在 renderRHandsontable 中有什么作用?
  • 它处理 Null 值和空白。本质上,req 下的其余代码仅在 Req 中的条件为非空白时才会执行。它与 rhandsontable 本身没有任何关系
猜你喜欢
  • 1970-01-01
  • 2016-04-09
  • 2019-07-15
  • 2018-02-21
  • 2021-02-04
  • 2017-02-21
  • 2019-01-30
  • 2015-10-27
  • 2019-10-26
相关资源
最近更新 更多