【发布时间】:2017-03-18 07:47:16
【问题描述】:
我想使用另一个数据帧 (B) 更新一个数据帧 (A)。 B 在ui 中,用户可以选择更新表 B 中的数据。根据 B 中的更新值,我想更新 A。请参见下面的代码,该代码允许用户更新 B,但之后 A 没有更新点击操作按钮:
df2 <- structure(list(A = c(1L, 4L, 0L, 1L),
B = c("3", "*", "*", "2"),
C = c("4", "5", "2", "*"),
D = c("*", "9", "*", "4")),
.Names = c("A", "B", "C", "D"),
class = "data.frame",
row.names = c(NA, -4L))
df1 <- structure(list(variable = c("A", "B", "C", "D"),
Value = c(2L,1L, 9L, 0L)),
.Names = c("variable", "Value"),
class = "data.frame",
row.names = c(NA, -4L))
shinyApp(
ui <-
fluidPage(
titlePanel("Sample"),
# Create a new row for the table.
sidebarLayout(
sidebarPanel(
selectInput("select", label = h3("Select Variable"),
choices = unique(df1$variable),
selected = unique(df1$variable)[1]),
numericInput("num", label = h3("Replace * with"),
value = unique(df1$variable)[1]),
actionButton("applyChanges", "Apply Changes")),
mainPanel(
dataTableOutput(outputId="table")
))),
Server <- function(input, output) {
# Filter data based on selections
output$table <- renderDataTable({
df1$Value[df1$variable==input$select] <<- input$num
df1
})
df2_new <- eventReactive(input$applyChanges,{
df1[as.character(df1$variable)] <- Map(function(x, y)
replace(x, x=="*", y), df2[as.character(df1$variable)], df1$Value)
df2_new <- df2
return(df2_new)
})
})
任何帮助将不胜感激。谢谢!!
【问题讨论】:
-
你可能想使用
reactiveValues