【问题标题】:R Shiny: Formatting reactive data.frame from sql queryR Shiny:格式化来自 sql 查询的响应式 data.frame
【发布时间】:2016-02-22 11:07:19
【问题描述】:

我试图弄清楚在从反应式 sql 查询中获取数据后如何更改列的类型...

例如,当我从数据库中获取数据时,某些列是字符,我希望它们是因子。有些列是数字的(这是正确的),但我需要将它们显示为数据表中的因子(用于数据表,因为名为 Tolerance 的列,并且容差不能按范围过滤,它应该是一个号)。

简单代码:

library(ROracle)
library(shiny)
library(DT)


server <- shinyServer(
  function(input, output, session) {

    con <- dbConnect(dbDriver("Oracle"),"xx/K",username="user",password="pwd")
    tableList <- dbListTables(con,schema="K")

    updateSelectizeInput(session, "tabnames", server = TRUE, choices = tableList)

      sqlOutput <- reactive({
        sqlInput <- paste("select rownum * from K.",input$tabnames)
        dbGetQuery(con$cc, sqlInput, stringsAsFactors = T)#it hasnt worked neither
      })

    output$table <- DT::renderDataTable(sqlOutput(), server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))

    session$onSessionEnded(function() { dbDisconnect(con) })
  })

ui_panel <- 
  tabPanel("Test",
           sidebarLayout(
             sidebarPanel( 
             ),
             mainPanel(
               selectizeInput("tabnames",label = "server side", choices = NULL),

               tableOutput("out"),
               tableOutput("table")
             )
           )
  )


ui <- shinyUI(navbarPage("Test",ui_panel))

runApp(list(ui=ui,server=server))

当我简单地尝试过时:

output$table <- DT::renderDataTable({

sqlOutput()$HOEHE_TOLP <- as.factor(sqlOutput()$HOEHE_TOLP)

datatable(sqlOutput(), server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))})

它不起作用,并给了我一个错误:

Error in sqlOutput()$HOEHE_TOLP <- as.factor(sqlOutput()$HOEHE_TOLP) : 
  ungültige (NULL) linke Seite in Zuweisung

*invalid (NULL) left side of assignment

有什么想法可以将某些列转换为响应式数据框的因子吗?

干杯

【问题讨论】:

  • 您可以将数据框本地保存在反应函数中,然后在需要的列上调用 as.factor()。
  • 这可能会很棘手,因为会有很多用户会使用该应用程序,其次过滤的数据会非常大......
  • 不确定您的意思,因为将数据框保存并返回到变量中与反应返回的内容(数据框)没有什么不同。

标签: r shiny


【解决方案1】:

编辑:

只需替换这个表达式:

output$table <- DT::renderDataTable(sqlOutput(), server=TRUE, 
  rownames=TRUE, filter="top", options=list(pageLength=10))

与:

output$table <- DT::renderDataTable({
  intermed <- sqlOutput()
  intermed$HOEHE_TOLP <- as.factor(intermed$HOEHE_TOLP)
  datatable(intermed) %>% formatStyle("RUND2_MITT", color = 'red', 
    backgroundColor = 'lightyellow', fontWeight = 'bold')
}, server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))

这是一个独立的例子:

library(DT)
library(shiny)

ui <- fluidPage(
  actionButton("inst", "Instigate Reactive"),
  dataTableOutput("test")
)

server <- function(input, output){
  data <- eventReactive(input$inst, {
    iris
  })

  output$test <- renderDataTable({
    set <- data()
    set$Sepal.Length <- as.factor(set$Sepal.Length)
    datatable(set) %>% formatStyle("Petal.Length", color = 'red', 
                                   backgroundColor = 'lightyellow',
                                   fontWeight = 'bold')
  })
}

shinyApp(ui, server)

【讨论】:

  • 非常感谢!它工作得很好,但只有一件事:当我使用%&gt;% formatStyle("RUND2_MITT", color = 'red', backgroundColor = 'lightyellow', fontWeight = 'bold') 作为数据表时,它给了我一个错误:Error in appendFormatter(x$options$rowCallback, columns, colnames, rownames, : You specified the columns: RUND2_MITT, but the column names of the data are。你知道我如何实现formatStyle吗?
猜你喜欢
  • 2013-12-10
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 2021-01-23
  • 2015-10-05
  • 1970-01-01
  • 2021-11-02
  • 2013-05-29
相关资源
最近更新 更多