【发布时间】:2017-08-28 04:53:09
【问题描述】:
我有 2 个单独工作的代码(代码 A 和代码 B)。当我使用这些代码创建单个应用程序时,当两个输入都更新时它会显示错误。不知道错误在哪里?
代码 A
library(shiny)
dfaa <- data.frame(A = c( 1L, 4L, 0L, 1L),
B = c("3","*","*","2"),
C = c("4","5","2","*"),
D = c("*","9","*","4"),stringsAsFactors = F)
dfbb <- data.frame(variable = c("A","B","C","D"),
Value = c( 2L, 1L, 9L, 0L),stringsAsFactors = F)
dfbb["Drop_Variable"] <- "No"
ui <- fluidPage(titlePanel("Sample"),
sidebarLayout(
sidebarPanel(
selectInput("select2", label = h3("Select any other Variable to drop"),
choices = unique(dfbb$variable),
selected = unique(dfbb$variable)[1]),
selectInput("select3", label = h3("Yes/No"),
choices = list("Yes", "No"),
selected = "No"),
actionButton("applyChanges", "Apply Changes specified in B to A")),
mainPanel(
h3("Table A"), dataTableOutput(outputId="tableA"),
h3("Table B"), dataTableOutput(outputId="tableB")
)))
server <- function(input, output) {
rv <- reactiveValues(dfA=dfaa,dfB=dfbb)
observe({
# update dfB immediately when the variable or value in the ui changes
rv$dfB$Drop_Variable[rv$dfB$variable==input$select2] <- input$select3
})
observeEvent(input$applyChanges,{
drop <- as.character(rv$dfB$variable[rv$dfB$Drop_Variable == "Yes"])
rv$dfA <- rv$dfA[,!(names(rv$dfA) %in% drop)]
})
output$tableB <- renderDataTable({ rv$dfB })
output$tableA <- renderDataTable({ rv$dfA })
}
shinyApp(ui=ui,server=server)
代码 B
library(shiny)
dfaa <- data.frame(A = c( 1L, 4L, 0L, 1L),
B = c("3","*","*","2"),
C = c("4","5","2","*"),
D = c("*","9","*","4"),stringsAsFactors = F)
dfbb <- data.frame(variable = c("A","B","C","D"),
Value = c( 2L, 1L, 9L, 0L),stringsAsFactors = F)
dfbb["Drop_Variable"] <- "No"
ui <- fluidPage(titlePanel("Sample"),
sidebarLayout(
sidebarPanel(
selectInput("select", label = h3("Select Variable"),
choices = unique(dfbb$variable),
selected = unique(dfbb$variable)[1]),
numericInput("num", label = h3("Replace * in A with"),
value = unique(dfbb$Value)[1]),
actionButton("applyChanges", "Apply Changes specified in B to A")),
mainPanel(
h3("Table A"), dataTableOutput(outputId="tableA"),
h3("Table B"), dataTableOutput(outputId="tableB")
)))
server <- function(input, output) {
rv <- reactiveValues(dfA=dfaa,dfB=dfbb)
observe({
# update dfB immediately when the variable or value in the ui changes
rv$dfB$Value[rv$dfB$variable==input$select] <- input$num
})
observeEvent(input$applyChanges,{
# Here we apply the changes that were specified
dfAcol <-as.character(rv$dfB$variable)
rv$dfA[dfAcol] <-
Map(function(x, y) replace(x, x=="*", y), rv$dfA[dfAcol], rv$dfB$Value)
})
output$tableB <- renderDataTable({ rv$dfB })
output$tableA <- renderDataTable({ rv$dfA })
}
shinyApp(ui=ui,server=server)
组合代码 A 和 B
library(shiny)
dfaa <- data.frame(A = c( 1L, 4L, 0L, 1L),
B = c("3","*","*","2"),
C = c("4","5","2","*"),
D = c("*","9","*","4"),stringsAsFactors = F)
dfbb <- data.frame(variable = c("A","B","C","D"),
Value = c( 2L, 1L, 9L, 0L),stringsAsFactors = F)
dfbb["Drop_Variable"] <- "No"
ui <- fluidPage(titlePanel("Sample"),
sidebarLayout(
sidebarPanel(
selectInput("select", label = h3("Select Variable"),
choices = unique(dfbb$variable),
selected = unique(dfbb$variable)[1]),
numericInput("num", label = h3("Replace * in A with"),
value = unique(dfbb$Value)[1]),
selectInput("select2", label = h3("Select any other Variable to drop"),
choices = unique(dfbb$variable),
selected = unique(dfbb$variable)[1]),
selectInput("select3", label = h3("Yes/No"),
choices = list("Yes", "No"),
selected = "No"),
actionButton("applyChanges", "Apply Changes specified in B to A")),
mainPanel(
h3("Table A"), dataTableOutput(outputId="tableA"),
h3("Table B"), dataTableOutput(outputId="tableB")
)))
server <- function(input, output) {
rv <- reactiveValues(dfA=dfaa,dfB=dfbb)
observe({
# update dfB immediately when the variable or value in the ui changes
rv$dfB$Value[rv$dfB$variable==input$select] <- input$num
rv$dfB$Drop_Variable[rv$dfB$variable==input$select2] <- input$select3
})
observeEvent(input$applyChanges,{
# Here we apply the changes that were specified
dfAcol <-as.character(rv$dfB$variable)
rv$dfA[dfAcol] <-
Map(function(x, y) replace(x, x=="*", y), rv$dfA[dfAcol], rv$dfB$Value)
drop <- as.character(rv$dfB$variable[rv$dfB$Drop_Variable == "Yes"])
rv$dfA <- rv$dfA[,!(names(rv$dfA) %in% drop)]
})
output$tableB <- renderDataTable({ rv$dfB })
output$tableA <- renderDataTable({ rv$dfA })
}
shinyApp(ui=ui,server=server)
【问题讨论】:
-
是的,那是行不通的——也许只有一次。您可能需要处理三个表,一个永远不会更改的根表,并始终将表 B 中指定的更改应用于该根表以获取表 A。
-
此外,base R 在空条件下也不能很好地工作。这就是为什么人们倾向于使用像
dplyr和data.table这样对程序员更友好的库。 -
你能告诉我你所说的空条件是什么意思吗?仍在尝试学习 R 和 Shiny :D
-
处理结果,例如没有 data.frame 列被留下,或者只剩下一列。
-
注意这篇文章。如果您只留下一列不被删除,您的程序将失败。 stackoverflow.com/questions/10737452/…。您需要像我刚才那样在
rv$dfA的最终计算中添加,drop=TRUE。
标签: r shiny reactive-programming reactive