英文:
shiny, table slicing, textfield
问题
需要帮助实现的是:
我在Shiny中有一个表格,并希望根据用户的输入来切分表格。
例如,用户输入1、4-6,然后他们只会看到第1列和第4-6列。
我考虑使用文本框来实现,但首先它不起作用,其次从安全角度来看,允许用户输入字符是危险的,因为它应该只是数字。
我想到的另一个解决方案是使用多个NumericInputs/Numeric Range Input,但我想知道是否有更加优雅的解决方案?(某种文本框,只允许输入数字值,用户可以输入范围以及单个数字值)
library(shiny)
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
textInput("slice", "Slicing", "")
),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("mtcars", DT::dataTableOutput("mytable2"))
)
)
)
)
server <- function(input, output) {
output$mytable2 <- DT::renderDataTable({
print(input$slice)
xm <- as.numeric(input$slice)
if (!is.na(xm)){
DT::datatable(mtcars[,xm], options = list(orderClasses = TRUE))
}
else {
DT::datatable(mtcars, options = list(orderClasses = TRUE))
}
})
}
shinyApp(ui, server)
英文:
What I need help implementing is:
I have a table in shiny, and I would like to slice the table according to the user input.
For example the user types 1, 4-6, and then they would see only the first and 4th-6th column.
I thought of implementing it with a textfield, but first of all it does not work, second it is dangerous from a security point of view to allow users to type also characters, since it should only be numeric anyway.
Another solution that comes into my mind is to have several NumericInputs/ Numeric Range Input, but I thought maybe knows of a bit more elegant solution? (some textfield where only numeric values would be allowed. where the user could type ranges as well as single numeric values)
library(shiny)
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
textInput("slice", "Slicing", "")
),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("mtcars", DT::dataTableOutput("mytable2"))
)
)
)
)
server <- function(input, output) {
output$mytable2 <- DT::renderDataTable({
print(input$slice)
xm <- as.numeric(input$slice)
if (!is.na(xm)){
DT::datatable(mtcars[,xm], options = list(orderClasses = TRUE))
}
else {
DT::datatable(mtcars, options = list(orderClasses = TRUE))
}
})
}
shinyApp(ui, server)
答案1
得分: 2
一种方法是使用 pickerInput
:
library(shiny)
library(DT)
library(shinyWidgets)
ui <- fluidPage(
title = "mtcars DataTable",
sidebarLayout(
sidebarPanel(
pickerInput("columns", "Select columns", choices = seq_along(mtcars),
options = list(`actions-box` = TRUE), multiple = TRUE)
),
mainPanel(
DT::dataTableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
if (is.null(input$columns)) {
DT::datatable(mtcars, options = list(orderClasses = TRUE))
} else {
selected_columns <- as.numeric(input$columns) # convert character to numeric
DT::datatable(mtcars[, selected_columns, drop = FALSE], options = list(orderClasses = TRUE))
}
})
}
shinyApp(ui, server)
英文:
One way is to use pickerInput
:
library(shiny)
library(DT)
library(shinyWidgets)
ui <- fluidPage(
title = "mtcars DataTable",
sidebarLayout(
sidebarPanel(
pickerInput("columns", "Select columns", choices = seq_along(mtcars),
options = list(`actions-box` = TRUE), multiple = TRUE)
),
mainPanel(
DT::dataTableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
if (is.null(input$columns)) {
DT::datatable(mtcars, options = list(orderClasses = TRUE))
} else {
selected_columns <- as.numeric(input$columns) # convert character to numeric
DT::datatable(mtcars[, selected_columns, drop = FALSE], options = list(orderClasses = TRUE))
}
})
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论