我想了解为什么将这两个方法用于索引行号中的空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:
Run Code Online (Sandbox Code Playgroud)> df Number Text NA 123456 abcdef 2 456789 abcdef
方法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:
Run Code Online (Sandbox Code Playgroud)> df Number Text 1 123456 abcdef 2 456789 abcdef
我看到的唯一区别是第一个方法访问data.frame使用列名而不是列号,但我没有看到为什么这导致NA行号被分配给第一个观察的原因,因为行号似乎从第二行开始按预期工作.
我正在尝试使用 更新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工作(注释掉代码)。它不断显示以下错误:
Run Code Online (Sandbox Code Playgroud)Warning: Error in !: invalid argument type 56: name2int
"rnorm(5)"这是我在调用时遇到的错误formatStyle。
Run Code Online (Sandbox Code Playgroud)Warning: Error …
我想明白为什么使用时,下表是不会自动更新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)