我有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)