I have 2 separate codes which work individually (Code A and Code B). When I club these codes to create single app, it shows error when both inputs are updated. Not sure where the error is?
Code 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)
Code 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)
Combined Code A and 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)