2017-02-03 72 views
2

我试图切换显示/隐藏列按钮的标签,并且还保持点击次数的轨迹以改变显示的列数表。我做到了,但我不能直接使用柜台价值的偶数/奇数差异。相反,我不得不使用这个:(vars$counter+1)/2) %% 2 == 0)来使它工作,因为每次点击都会改变计数器2次。我想请求一个更简单的程序,也许有一个shinyBS的呢?更改闪亮按钮的标签并点击计数

## app.R ## 
library(shiny) 
library(shinydashboard) 
library(DT) 

body<-dashboardBody(
    textOutput("count"), 
    uiOutput('showallcolumnsbutton'), 
    DT::dataTableOutput('table2') 
) 

ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(), 
    body 
) 

server <- function(input, output) { 
    table<-data.frame(replicate(10,sample(0:1,1000,rep=TRUE))) 
    vars<-reactiveValues() 
    vars = reactiveValues(counter = 0) 
    observe({ 
    if(!is.null(input$showallcolumns)){ 
     input$showallcolumns 
     isolate({ 
     vars$counter <- vars$counter + 1 
     }) 
    } 
    }) 
    label <- reactive({ 
    if(!is.null(input$showallcolumns)){ 
     if(((vars$counter+1)/2) %% 2 == 0) label <- "Hide" 
     else label <- "Show" 
    } 
    }) 
    output$showallcolumnsbutton <- renderUI({ 
    actionButton("showallcolumns", label = label(), 
       icon("hand-pointer-o"), 
       style="color: #000; background-color: #0099ff; border-color: #2e6da4" 
    ) 
    }) 
    output$count<-renderText({paste("counter value:",vars$counter)}) 
    columnstoshow = reactive ({ 
    x= ((vars$counter+1)/2) # %% 2 == 0) 
    if (!is.null (x)) 
    { 
     if (x %% 2 == 0) { 
     c=c(1:10) 
     } 
     else { 
     c=c(1:5) 
     } 
    } #end 1st if 
    else { 
     c=c(1:10) 
    } 
    }) 
    output$table2 = DT::renderDataTable({ 
    DT::datatable(table[, columnstoshow()]) 
    }) 

} # end server 

shinyApp(ui, server) 
+0

我不是100%清楚你;再试图做 –

回答

1

因为我不是100%你想要的,是这样吗?请注意,我用其他的库如shinyBS

rm(list = ls()) 
library(shiny) 
library(shinydashboard) 
library(DT) 
library(shinyBS) 

body <- dashboardBody(bsButton("showallcolumns", label = "Hide", block = F, style="danger",icon=icon("hand-pointer-o")),br(),DT::dataTableOutput('table2')) 
ui <- dashboardPage(dashboardHeader(),dashboardSidebar(),body) 

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

    table <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE))) 
    vars <- reactiveValues(counter = 1:10) 

    observeEvent(input$showallcolumns,{ 
    if(input$showallcolumns %% 2){ 
     updateButton(session, "showallcolumns",label = "Show", block = F, style = "success",icon=icon("hand-pointer-o")) 
     vars$counter <- 1:5 
    } 
    else{ 
     updateButton(session, "showallcolumns",label = "Hide", block = F, style = "danger",icon=icon("hand-pointer-o"))  
     vars$counter <- 1:10 
    }                               
    }) 

    output$table2 = DT::renderDataTable({ 
    DT::datatable(table[, vars$counter]) 
    }) 

} # end server 

shinyApp(ui, server) 
+0

柜台本来是为点击,但是你的答案是没有必要的,也许这是问题 – Ferroao