【发布时间】:2017-11-08 17:44:18
【问题描述】:
当我尝试在 Shiny 中获取反应性对象并进一步操作它时,我一遍又一遍地遇到错误“
testdf <- data.frame(row1 = c(1,3,6), row2 = c(7, 5, 1))
ui <- fluidPage(
titlePanel("Education in Tanzania"),
sidebarLayout(
sidebarPanel(
#Select aggregation level of data
selectInput("AggregationSelect",
label = "Aggregation",
choices = list("School" = 1,
"District" = 2,
"Region" = 3),
selected = 1)
),
mainPanel(
DT::dataTableOutput("OutputTable")
)
)
)
server <- function(input, output) {
Output1 <- reactive({
testdf
})
observe({
if(2 %in% input$AggregationSelect) {
observe({Output1()$name[3] <- "b"})
} else if(3 %in% input$AggregationSelect) {
observe({Output1()$name[2] <- "c"})
} else if(1 %in% input$AggregationSelect) {
observe({Output1()$name[1] <- "a"})
}
})
output$OutputTable <- {DT::renderDataTable({
DT::datatable(Output1(),
options = list(pagelength = 25,
searching = TRUE,
paging = TRUE,
lengthChange = FALSE),
rownames = FALSE)
})
}
}
shinyApp(ui = ui, server = server)
在我的实际代码中我需要做的是通过 UI 组装一个数据框(我可以这样做,因此在这里只是替换了一个随机 df),然后添加一些信息(这里用添加的“名称”表示" 列)基于在 UI 中选择的内容。似乎将列添加到 df 应该不那么难,但是在反应对象上下文中,我尝试过的任何事情都没有奏效。欢迎使用其他修改反应性对象的方法,只要它们可以应用于更复杂的多步骤场景 - 我无法将所需的所有内容捆绑到反应性对象的初始分配中。
【问题讨论】:
-
你在
testdf中没有名为name的变量