闪亮:使用Rhandsontable和外部参数切换反应数据集

时间:2017-10-16 14:51:54

标签: r shiny rhandsontable

基本上,我试图在Rhandsontables之间切换,这些Rhandsontables也有外部小部件来操纵它们。此外,这些表中还有基础公式。这两个表基本相同,小部件和公式在两种情况下的工作方式相同,我只想在它们之间切换。以下示例可能是为了提供信息......

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)

在这种情况下,小部件和公式可以工作,但不能切换......

1 个答案:

答案 0 :(得分:0)

试试这个:

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

})