1

I have a very simple shiny app that is makes a gt table using some inputs.

One of my goals is to pass a user input which is numeric into the cols_width() argument so I can add padding to my first column. Although when doing something like the following I get an error that the input is not found.

  output$table <- render_gt( 
    reactive_tab() %>% 
      gt() %>% 
      cols_width(
        1 ~ px(input$colpad)
      )
  )
  

I've also tried doing {input$colpad} and .(input$colpad) with no success either.

Desired Output: enter image description here

Here is the code:

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)

data <- gtcars %>% head(10) %>% 
  select(mfr, model, msrp)

ui <- navbarPage("Y u no pad??",
                 tabPanel("Table", icon = icon("table"),
                          
                          sidebarLayout(
                            sidebarPanel(
                              selectInput("input",
                                          label = "Choose mfr",
                                          choices = c("All", data$mfr)),
                              numericInput("colpad", label = "First Column Padding", min = 1, max = 10000, value = 150),
                              screenshotButton(selector="#table", label = 'Download Png', filename = 'screenshot'),
                            ),
                            
                            mainPanel(
                              gt_output("table")
                            )
                          )
                 )
)



server <- function(input, output, session) {
  
  reactive_tab  <- reactive({
    d <- data
    if(input$input != "All")
      d <- subset(d, cyl == input$input)
    d 
  })
  
  output$table <- render_gt( 
    reactive_tab() %>% 
      gt() %>% 
      cols_width(
        1 ~ px(input$colpad)
      )
  )
  
  

}
shinyApp(ui, server)
2
  • Take a look at 728 Commented Jul 2, 2022 at 0:47
  • Hmm. I looked it and although it’s closed. There doesn’t really seem like a clear solution? Any thoughts? Thank you for the link Commented Jul 2, 2022 at 2:16

1 Answer 1

6
+250

Reason

The reason this is not working is because of the way gt::cols_width() evaluates is arguments. It does not know which environment to look in to find the input object.

One way to circumvent the issue is to first evaluate input$colpad and then pass the value in a way gt::cols_width() will understand.

Code

Here is one such approach where I paste together a formula and cast it as such on line 46:

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)
select <- dplyr::select

data <- gtcars %>%
  head(10) %>%
  select(mfr, model, msrp)

ui <- navbarPage(
  "Y u no pad??",
  tabPanel("Table",
    icon = icon("table"),
    sidebarLayout(
      sidebarPanel(
        selectInput("input",
          label = "Choose mfr",
          choices = c("All", data$mfr)
        ),
        numericInput("colpad", label = "First Column Padding", min = 1, max = 10000, value = 150),
        screenshotButton(selector = "#table", label = "Download Png", filename = "screenshot"),
      ),
      mainPanel(
        gt_output("table")
      )
    )
  )
)



server <- function(input, output, session) {
  reactive_tab <- reactive({
    d <- data
    if (input$input != "All") {
      d <- subset(d, cyl == input$input)
    }
    d
  })

  output$table <- render_gt(
    reactive_tab() %>%
      gt() %>%
      cols_width(
        as.formula(paste0("1 ~ ", input$colpad)) # EDIT HERE
      )
  )
}



shinyApp(ui, server)

Result

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.