Align Issue In renderTable

360 views
Skip to first unread message

Cyrus Lentin

unread,
Jan 26, 2015, 11:49:04 PM1/26/15
to shiny-...@googlegroups.com

Have dataset in which want to show columns which is depends on user input

```
##ui.r
library(shiny)
shinyUI(
    fluidPage(
        sidebarLayout(
            sidebarPanel(
                selectInput("thisMenu", label="Mode", choices=c("2","3","4"), selected=1)
            ),
            mainPanel(
                tableOutput("fileData")
            )
        )
    )
)
```

```
##server.r
library(shiny)
library(dplyr)

shinyServer(function(input, output) {

    output$fileData <- renderTable({
        fileData <- read.csv("app-data.csv")
        sCols <- as.numeric(input$thisMenu)
        fileData <- select(fileData, 1:sCols)
        fileData[,2:sCols] <- prettyNum(fileData[,2:sCols], width=15, justify="right", big.mark=",")
        fileData <<- fileData
        fileData
    }, align='eval(myAlign())')

    myAlign <- function(x) {
        sCols <- as.numeric(input$thisMenu)
        align <- rep("r",sCols-1)
        align <- paste("ll",paste(align, collapse=""), sep="")
        #align <<- align
        return(align)
    }
})
```
Notice the use of function in "align" of renderTable.
This works the first time but once the selectInput value changes the align gives a problem.

Sample data-set provided below
```
### fileData
  LeftColName RightColumn.1 RightColumn.2 RightColumn.3 RightColumn.4
1       Row 1      1,00,000      2,00,000      3,00,000      4,00,000
2       Row 2      1,00,000      2,00,000      3,00,000      4,00,000
3       Row 3      1,00,000      2,00,000      3,00,000      4,00,000
4       Row 4      1,00,000      2,00,000      3,00,000      4,00,000
5       Row 5      1,00,000      2,00,000      3,00,000      4,00,000
6       Row 6      1,00,000      2,00,000      3,00,000      4,00,000
7       Row 7      1,00,000      2,00,000      3,00,000      4,00,000
8       Row 8      1,00,000      2,00,000      3,00,000      4,00,000
9       Row 9      1,00,000      2,00,000      3,00,000      4,00,000
```
Any help on this will be appreciated.

Joe Cheng

unread,
Jan 27, 2015, 9:17:40 PM1/27/15
to Cyrus Lentin, shiny-...@googlegroups.com
Maybe try this inside your shinyServer:

observe({
    sCols <- as.numeric(input$thisMenu)
    align <- rep("r",sCols-1)
    align <- paste("ll",paste(align, collapse=""), sep="")

    output$fileData <- renderTable({
        fileData <- read.csv("app-data.csv")
        sCols <- as.numeric(input$thisMenu)
        fileData <- select(fileData, 1:sCols)
        fileData[,2:sCols] <- prettyNum(fileData[,2:sCols], width=15, justify="right", big.mark=",")
        fileData <<- fileData
        fileData
    }, align=align)
})


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/b1e85205-93bf-4434-8980-9af2febaf9f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages