在下面的玩具示例中,我有两个数据集df1和df2。使用shiny和rhandsontable将这些数据集输出到交互式表格中。我的问题是,当用户没有编辑在shiny应用程序中的数据集时(如is.null(input$out)所示),output$out仅识别数据集的偏移量。
当input$out不为空时,如何修复以下代码以加载df1或df2?
当input$out不为空时,如何修复以下代码以加载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)