【问题标题】:Shiny: Switching reactive datasets with Rhandsontable and external parametersShiny:使用 Rhandsontable 和外部参数切换反应式数据集
【发布时间】:2018-03-28 03:26:05
【问题描述】:

基本上,我正在尝试在 Rhandsontables 之间切换,这些 Rha​​ndsontables 也具有操作它们的外部小部件。此外,这些表中还有基础公式。这些表本质上是相同的,小部件和公式在两种情况下的工作方式相同,我希望能够在它们之间切换。下面的例子可能是为了提供信息......

library(shiny)
library(rhandsontable)

RepData1 <- data.frame(col1 = c(1:10)
                      ,col2 = "C1"
                      ,col3 = runif(10,0,0.023)
                      ,col4 = runif(10,0,1))
RepData2 <- data.frame(col1 = c(1:10)
                      ,col2 = "C2"
                      ,col3 = runif(10,0,0.023)
                      ,col4 = runif(10,0,1))

ui=fluidPage(

   sliderInput("mySlider",label="Slider", min = 0, max = 100, post  = " %", value = 50)
  ,numericInput("Total", "Total:", 500000)
  ,verbatimTextOutput("value")

  ,fluidRow(
        column(3, radioButtons("Buttn10", label="col", choices= c("C1","C2"), selected = "C1", inline = TRUE))
       ,column(6,rHandsontableOutput("hotable1"))
        )
)

server <- shinyServer(function(input, output, session) {

  selData <- ""

    previous <- reactive({
    if(input$Buttn10 == "C1") { 
      if(selData == "" | selData == "C2") {
        selData <<- "C1"

        return(RepData1) 
      }
    } else {
      if(selData == "C1") {
        selData <<- "C2"

        return(RepData2) 
      }
    }

  }) 

  MyChanges <- reactive({
    if(is.null(input$hotable1)){
      return(previous())

      } else if(!identical(previous(),input$hotable1)){

    mytable <- as.data.frame(hot_to_r(input$hotable1))

    x <- input$Total*(input$mySlider/100)


      mytable$QTY <- x*mytable$col4
      mytable
    }
  })

  output$hotable1 <-  renderRHandsontable({


                      if(is.null(MyChanges())) return()
                      df_ <- MyChanges() 

                      rhandsontable(df_, readOnly = FALSE, rowHeaders= NULL, useTypes= TRUE) %>%
                      hot_table(highlightCol = TRUE, highlightRow = TRUE) 
                    })

})

shinyApp(ui,server)

在这种情况下,小部件和公式有效,但切换无效...

【问题讨论】:

  • 我不太明白你所说的“切换”是什么意思?
  • 我在上面的示例中有两个数据帧,我试图在 UI 部分中使用单选按钮('C1','C2')之间进行切换。我在这里尝试实现的技巧是在 RepData1 和 RepData2 之间切换,同时保持连接到滑块和总输入...

标签: r shiny rhandsontable


【解决方案1】:

试试这个:

server <- shinyServer(function(input, output, session) {

  previous <- reactive({
    if(input$Buttn10 == "C1") { 
        return(RepData1) 
    }
    if(input$Buttn10 == "C2"){
        return(RepData2)
    }

  }) 

  MyChanges <- reactive({
     mytable <- previous()

     x <- input$Total*(input$mySlider/100)


     mytable$QTY <- x*mytable$col4
     mytable
  })

  output$hotable1 <-  renderRHandsontable({

  df_ <- MyChanges() 

  rhandsontable(df_, readOnly = FALSE, rowHeaders= NULL, useTypes=TRUE) %>%
       hot_table(highlightCol = TRUE, highlightRow = TRUE) 
  })

})

【讨论】:

  • 由于某种原因,当我这样做时,我丢失了“数量”列以及与滑块和总输入的连接......
  • 我换了anwserm,试试看。
  • 酷!谢谢你。那行得通。问题是我过早将其转换为 hot_table 吗?如果我不想损害反应性,是否应该只在输出步骤中完成?
  • 我不知道,但对我来说,你做了一些不必要的循环。但我很感激你喜欢。谢谢!!
猜你喜欢
  • 2015-11-24
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 2016-09-14
相关资源
最近更新 更多