如何让之前有效的数据table转置,重启电脑后又有效?
How to make data table transposition that previously worked, work again after a computer restart?
在下面的可重现代码中,单击单选按钮转置数据 table 工作正常,直到我重新启动计算机。现在post-重新启动,此应用程序在尝试转置时崩溃。可能之前在内存中加载了一个包或某个对象,使它能够工作。我试过找到这个丢失的物体,但还没有找到运气。无论如何,我怎样才能点击单选按钮(如底部图像所示)来转置数据 table?
此 post 是 的后续解决方案,其解决方案运行良好(但不再有效)。
可重现代码(从上面引用的 post 缩短):
library(DT)
library(shiny)
library(dplyr)
library(htmltools)
library(data.table)
data <-
data.frame(
ID = c(1,1,1,2,2,2,3,3,3),
Period = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
Values = c(5, 10, 15, 0, 2, 4, 3, 6, 9),
State = c("X0","X1","X2","X0","X2","X0", "X2","X1","X0")
)
numTransit <- function(x, from=1, to=3){
setDT(x)
unique_state <- unique(x$State)
all_states <- setDT(expand.grid(list(from_state = unique_state, to_state = unique_state)))
dcast(x[, .(from_state = State[from],
to_state = State[to]),
by = ID]
[,.N, c("from_state", "to_state")]
[all_states,on = c("from_state", "to_state")],
to_state ~ from_state, value.var = "N"
)
}
ui <- fluidPage(
tags$head(tags$style(".datatables .display {margin-left: 0;}")),
h4(strong("Base data frame:")),
tableOutput("data"),
h4(strong("Transition table inputs:")),
numericInput("transFrom", "From period:", 1, min = 1, max = 3),
numericInput("transTo", "To period:", 2, min = 1, max = 3),
radioButtons("transposeDT",
label = "Transpose table:",
choiceNames = c('Columns','Rows'),
choiceValues = c('Columns','Rows'),
selected = 'Columns',
inline = TRUE
),
h4(strong("Output transition table:")),
DTOutput("resultsDT"),
)
server <- function(input, output, session) {
results <-
reactive({
results <- numTransit(data, input$transFrom, input$transTo)
})
output$data <- renderTable(data)
output$resultsDT <- renderDT(server=FALSE, {
datatable(
data =
# the line immediately below causes the app to crash when transposing the data table
if(input$transposeDT=='Rows'){results()%>%transpose(make.names = 'to_state',keep.names = 'to_state')}
else {results()},
rownames = FALSE,
filter = 'none',
class = "display"
) %>%
formatStyle(c(1), `border-right` = "solid 1px")
})
}
shinyApp(ui, server)
YBS 评论(尝试 data.table::transpose(...))解决了问题。见postWhat are the double colons (::) in R?,里面解释了可能存在多个包中的同名函数(我用transpose()
好像就是这种情况)。双冒号运算符 ::
允许您指定特定包中的特定函数以在特定情况下使用。
在下面的可重现代码中,单击单选按钮转置数据 table 工作正常,直到我重新启动计算机。现在post-重新启动,此应用程序在尝试转置时崩溃。可能之前在内存中加载了一个包或某个对象,使它能够工作。我试过找到这个丢失的物体,但还没有找到运气。无论如何,我怎样才能点击单选按钮(如底部图像所示)来转置数据 table?
此 post 是
可重现代码(从上面引用的 post 缩短):
library(DT)
library(shiny)
library(dplyr)
library(htmltools)
library(data.table)
data <-
data.frame(
ID = c(1,1,1,2,2,2,3,3,3),
Period = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
Values = c(5, 10, 15, 0, 2, 4, 3, 6, 9),
State = c("X0","X1","X2","X0","X2","X0", "X2","X1","X0")
)
numTransit <- function(x, from=1, to=3){
setDT(x)
unique_state <- unique(x$State)
all_states <- setDT(expand.grid(list(from_state = unique_state, to_state = unique_state)))
dcast(x[, .(from_state = State[from],
to_state = State[to]),
by = ID]
[,.N, c("from_state", "to_state")]
[all_states,on = c("from_state", "to_state")],
to_state ~ from_state, value.var = "N"
)
}
ui <- fluidPage(
tags$head(tags$style(".datatables .display {margin-left: 0;}")),
h4(strong("Base data frame:")),
tableOutput("data"),
h4(strong("Transition table inputs:")),
numericInput("transFrom", "From period:", 1, min = 1, max = 3),
numericInput("transTo", "To period:", 2, min = 1, max = 3),
radioButtons("transposeDT",
label = "Transpose table:",
choiceNames = c('Columns','Rows'),
choiceValues = c('Columns','Rows'),
selected = 'Columns',
inline = TRUE
),
h4(strong("Output transition table:")),
DTOutput("resultsDT"),
)
server <- function(input, output, session) {
results <-
reactive({
results <- numTransit(data, input$transFrom, input$transTo)
})
output$data <- renderTable(data)
output$resultsDT <- renderDT(server=FALSE, {
datatable(
data =
# the line immediately below causes the app to crash when transposing the data table
if(input$transposeDT=='Rows'){results()%>%transpose(make.names = 'to_state',keep.names = 'to_state')}
else {results()},
rownames = FALSE,
filter = 'none',
class = "display"
) %>%
formatStyle(c(1), `border-right` = "solid 1px")
})
}
shinyApp(ui, server)
YBS 评论(尝试 data.table::transpose(...))解决了问题。见postWhat are the double colons (::) in R?,里面解释了可能存在多个包中的同名函数(我用transpose()
好像就是这种情况)。双冒号运算符 ::
允许您指定特定包中的特定函数以在特定情况下使用。