【发布时间】:2015-11-24 09:13:18
【问题描述】:
在下面的玩具示例中,我有两个数据集 df1 和 df2。数据集使用闪亮和 rhandsontable 输出到交互式表格。我的问题是,如果用户没有按照 is.null(input$out) 的指示在闪亮的应用程序中编辑数据集,则 output$out 只会识别数据集的变化。
当 input$out 不为 null 时 input$df 发生变化时,如何修复以下代码以加载 df1 或 df2?
require(rhandsontable)
require(shiny)
df1 <- data.frame(col1 = rnorm(20),
col2 = rep(T, 20))
df2 <- data.frame(col1 = rnorm(20),
col2 = rep(F, 20))
funcX <- function(x) {
x$out <- ifelse(x$col2 == T, x$col1 * 1.5, x$col1)
x$out
}
df2$out <- funcX(df2)
df1$out <- funcX(df1)
server <- function(input, output) {
df <- reactive({
if (input$df == "df1") {
df <- df1
} else {
df <- df2
}
df
})
output$out <- renderRHandsontable({
if (is.null(input$out)) {
hot <- rhandsontable(df())
} else {
str(input$out)
hot <- hot_to_r(input$out)
hot$out <- funcX(hot)
hot <- rhandsontable(hot)
}
hot
})
}
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput(
'df', 'Select data.frame:',
choices = c('df1', 'df2'),
selected = 'df1'
)
),
mainPanel(rHandsontableOutput("out"))))
shinyApp(ui = ui, server = server)
【问题讨论】: