2017-02-03 111 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