簡體   English   中英

閃亮:使用Rhandsontable和外部參數切換反應性數據集

[英]Shiny: Switching reactive datasets with Rhandsontable and external parameters

本質上,我試圖在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)

在這種情況下,小部件和公式有效,但切換無效。

嘗試這個:

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

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM