2017-10-16 36 views
0

本质上,我试图在也具有操纵它们的外部小部件的Rhandsontables之间切换。此外,这些表中还有基础公式。这些表格基本相同,在这两种情况下,小部件和公式的工作方式都是相同的,我只是希望能够在它们之间切换。下面的例子可能是内容丰富...闪亮:使用Rhandsontable和外部参数切换反应数据集

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) 

在这种情况下,小工具和公式的工作,而不是切换...

+0

我不你很明白你的意思是“切换”? –

+0

我在上面的示例中有两个数据框,我试图在UI部分中使用radioButtons('C1','C2')之间切换。我试图在这里拉开的技巧是在RepData1和RepData2之间切换,同时保持连接到滑块和总输入... – TesterL3X18999

回答

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

}) 
+0

由于某种原因,当我这样做时,我失去了我的'数量'列和连接到滑块和总输入... – TesterL3X18999

+0

我改变我的anwserm,尝试。 –

+0

酷!谢谢。这工作。我过早将它转换为hot_table的问题是什么?如果我不想伤害反应性,那么这只能在输出步骤中完成吗? – TesterL3X18999