闪亮,表格切片,文本字段

huangapple go评论66阅读模式
英文:

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 &lt;- fluidPage(
  title = &quot;Examples of DataTables&quot;,
  sidebarLayout(
    
    sidebarPanel(
      textInput(&quot;slice&quot;, &quot;Slicing&quot;, &quot;&quot;)
    ),
    mainPanel(
      tabsetPanel(
        id = &#39;dataset&#39;,

        tabPanel(&quot;mtcars&quot;, DT::dataTableOutput(&quot;mytable2&quot;))
      )
    )
  )
)

server &lt;- function(input, output) {
  

  
  output$mytable2 &lt;- DT::renderDataTable({
    print(input$slice)
    xm &lt;- 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 &lt;- fluidPage(
  title = &quot;mtcars DataTable&quot;,
  
  sidebarLayout(
    sidebarPanel(
      pickerInput(&quot;columns&quot;, &quot;Select columns&quot;, choices = seq_along(mtcars), 
                  options = list(`actions-box` = TRUE), multiple = TRUE)
    ),
    
    mainPanel(
      DT::dataTableOutput(&quot;table&quot;)
    )
  )
)

server &lt;- function(input, output) {
  output$table &lt;- DT::renderDataTable({
    if (is.null(input$columns)) {
      DT::datatable(mtcars, options = list(orderClasses = TRUE)) 
    } else {
      selected_columns &lt;- as.numeric(input$columns) # convert character to numeric
      DT::datatable(mtcars[, selected_columns, drop = FALSE], options = list(orderClasses = TRUE))
    }
  })
}

shinyApp(ui, server)

闪亮,表格切片,文本字段

huangapple
  • 本文由 发表于 2023年7月11日 01:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定