小编New*_*ser的帖子

将第一行添加到空data.frame时,行号不同(NA与1)

我想了解为什么将这两个方法用于索引行号中的空data.frame结果NA分配给第一行:

方法1:

df <- data.frame(Number=numeric(), Text=character(), stringsAsFactors = FALSE)
df[1,]$Number <- 123456
df[1,]$Text <- "abcdef"
df[2,]$Number <- 456789
df[2,]$Text <- "abcdef"
Run Code Online (Sandbox Code Playgroud)

输出1:

> df
   Number   Text
NA 123456 abcdef
2  456789 abcdef
Run Code Online (Sandbox Code Playgroud)

方法2:

df <- data.frame(Number=numeric(), Text=character(), stringsAsFactors = FALSE)
df[1,1] <- 123456
df[1,2] <- "abcdef"
df[2,1] <- 456789
df[2,2] <- "abcdef"
Run Code Online (Sandbox Code Playgroud)

输出2:

> df
  Number   Text
1 123456 abcdef
2 456789 abcdef
Run Code Online (Sandbox Code Playgroud)

我看到的唯一区别是第一个方法访问data.frame使用列名而不是列号,但我没有看到为什么这导致NA行号被分配给第一个观察的原因,因为行号似乎从第二行开始按预期工作.

r dataframe

6
推荐指数
1
解决办法
179
查看次数

如何通过 dataTableProxy 使用 formatStyle 更新数据表

我正在尝试使用 更新backgroundColor列的dataTableProxy。但是,我不确定如何正确处理列名称。这是一个例子:

library(shiny)
library(DT)

ui <- fluidPage(
   fluidRow(
       DT::dataTableOutput("myplot")
    )
)

server <- function(input, output) {

    output$myplot <- DT::renderDataTable({
        datatable(as.data.frame(rnorm(5))) %>%
            formatStyle(1, backgroundColor = 'red')
    })

    proxy <- DT::dataTableProxy("myplot")
    mycolors <- c("red", "green", "blue")

    observe({
        invalidateLater(1000)

        proxy %>% replaceData(as.data.frame(rnorm(5)))

        # proxy %>% replaceData(as.data.frame(rnorm(5))) %>%
        #     formatStyle(1, backgroundColor = sample(mycolors, 1))
    })
}


shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

尽管数字按我的预期更新,但我无法使其formatStyle工作(注释掉代码)。它不断显示以下错误:

Warning: Error in !: invalid argument type
  56: name2int
Run Code Online (Sandbox Code Playgroud)

"rnorm(5)"这是我在调用时遇到的错误formatStyle

Warning: Error …
Run Code Online (Sandbox Code Playgroud)

r datatables shiny dt

5
推荐指数
1
解决办法
1906
查看次数

闪亮的自动更新表或图与 invalidateLater()

我想明白为什么使用时,下表是不会自动更新invalidateLater()的内部observeEvent()。我准备了以下程序来说明我的问题,“mytable2”使用reactiveTimer()并确实产生了所需的输出,但是“mytable”使用invalidateLater()并且不会自动更新,除非我单击“更新”按钮。为什么?

library(shiny)

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(
      actionButton("update", "Update")
    ),

    mainPanel(
      column(6, tableOutput('mytable')),
      column(6, tableOutput('mytable2'))
    )
  )
)

server <- function(input, output) {

  values <- reactiveValues(df   = RenderMyTable())

  observeEvent(invalidateLater(1000), {
    values$df <- RenderMyTable()  # This does not update after 1 sec
  })

  observeEvent(input$update, {
    values$df <- RenderMyTable()  # This does update upon clicking
  })

  output$mytable  <- renderTable(values$df)  # Depends on reactiveValues

  autoInvalidate <- reactiveTimer(1000)

  output$mytable2 <- renderTable({
    autoInvalidate()
    RenderMyTable()  # >This does …
Run Code Online (Sandbox Code Playgroud)

plot r auto-update shiny

0
推荐指数
1
解决办法
2281
查看次数

标签 统计

r ×3

shiny ×2

auto-update ×1

dataframe ×1

datatables ×1

dt ×1

plot ×1