我有一个非常简单的闪亮应用程序,它gt使用一些输入来制作表格。
我的目标之一是将用户输入的数字传递到参数中cols_width(),以便我可以向第一列添加填充。尽管在执行类似以下操作时我收到错误,提示找不到输入。
output$table <- render_gt(
reactive_tab() %>%
gt() %>%
cols_width(
1 ~ px(input$colpad)
)
)
Run Code Online (Sandbox Code Playgroud)
我也尝试过这样做{input$colpad},但.(input$colpad)也没有成功。
这是代码:
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)
Run Code Online (Sandbox Code Playgroud)
这不起作用的原因是评估方式gt::cols_width()是参数。它不知道在哪个环境中查找对象input。
规避该问题的一种方法是首先评估input$colpad,然后以一种可以理解的方式传递该值gt::cols_width()。
这是一种这样的方法,我将公式粘贴在一起并在第 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)
Run Code Online (Sandbox Code Playgroud)