【问题标题】:Shiny Table app with money in one column and filer by factor?Shiny Table 应用程序在一列中有钱并按因素过滤?
【发布时间】:2014-10-10 01:13:34
【问题描述】:

我正在尝试创建一个闪亮的应用程序,它基本上是一个表格,带有一个关于选项卡。

我遇到了两件事:

  1. 其中一列是美元金额。我希望格式为 1,225 美元。我能做到的最接近的方法是使用 paste0,但是当您对列进行排序时,它不能按预期工作,而且我没有得到数千个逗号。

  2. 我希望能够使用“全部、A、C、D”选项的下拉菜单。如果我选择“A”,它将只显示“Something==A”的表格。现在这正在工作,但我不确定我是否以最好的方式编码。

这是我的 ui.R

library(shiny)

shinyUI(fluidPage(
  title = 'Some tittle',
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput('show_vars', 'Columns to show:',
                         c("Something", "Money"), 
                         selected = c("Money")),
      selectInput(inputId = "Something",
                  label = "Choose a category",
                  choices = c("All", "A", "C", "D"),
                  selected = "All")
      ),

    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel('Table', dataTableOutput('mytable1')),
        tabPanel('about', includeMarkdown("about.md"))
      )
    )
  )
))

我的服务器.R

library(shiny)
library(dplyr)
DF1 = data.frame("Rank"=1:4, "Something"=as.factor(c("A","A","C","D")), "Money"=c(2345.5, 1234.67, 5.2, 9878.46))
# change the data to have dollar sign
DF1 = DF1 %>% mutate(Money= paste0("$",round(Money,0))) 
original = DF1
shinyServer(function(input, output) {
  observe({
    if(input$Something!="All"){
      DF1 = original %>% filter(Something==input$Something)
      # a large table, reative to input$show_vars
      output$mytable1 <- renderDataTable({
        DF1[, c("Rank", input$show_vars), drop = FALSE]
      })
      }else{
      DF1 = original
      output$mytable1 <- renderDataTable({
        DF1[, c("Rank", input$show_vars), drop = FALSE]
      })
      }
})
})

还有我的 about.md

# Title
+ bla
+ bla
+ bla

感谢您的帮助!

【问题讨论】:

  • 美元格式见scales::dollar()

标签: shiny


【解决方案1】:

我有一个解决方案,但我确信这段代码会更好。如果有人可以就如何改进它给我建议,我将不胜感激:-)。我的解决方案是使用刻度来添加逗号

我的服务器如下所示:

library(shiny)
library(dplyr)
library(scales)
DF1 = data.frame("Rank"=1:4, "Something"=as.factor(c("A","A","C","D")), "Money"=c(2345.5, 1234.67, 5.2, 9878.46))
# change the data to have dollar sign
DF1 = DF1 %>% mutate(Money=round(Money,0)) %>% mutate(Money=comma_format()(Money)) %>% mutate(Money=paste0("$",Money))
original = DF1
shinyServer(function(input, output) {
  observe({
    if(input$Something!="All"){
      DF1 = original %>% filter(Something==input$Something)
      # a large table, reative to input$show_vars
      output$mytable1 <- renderDataTable({
        DF1[, c("Rank", input$show_vars), drop = FALSE]
      })
      }else{
      DF1 = original
      output$mytable1 <- renderDataTable({
        DF1[, c("Rank", input$show_vars), drop = FALSE]
      })
      }
})
})

我认为反应部分的效率非常低,但我还不确定如何改进。

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 2017-08-28
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2015-12-23
    相关资源
    最近更新 更多