Very new to R Shiny! I've looked through a good 20 questions but they don't necessarily address the problem I'm facing.
I have a few dataframes generated from API calls that look something like this:
Project.ID Author.ID Author.Name Fav.Color
Test_Project1 1234 Bob Green
Test_Project1 2345 Jane Blue
Test_Project1 2687 Eric Blue
Test_Project1 8765 Tom Red
My goal is to allow users to select a column from the dataframe using a dropdown, select some values to compare from that column using some checkboxes, and add a new column to the same frame reflecting the comparisons that they wanted to make. It should look like this:
Project.ID Author.ID Author.Name Fav.Color RedvBlue GreenvRed
Test_Project1 1234 Bob Green NA Green
Test_Project1 2345 Jane Blue Blue NA
Test_Project1 2687 Eric Blue Blue NA
Test_Project1 8765 Tom Red Red Red
ui.R
ui <- fluidPage(
sidebarPanel(
selectInput("viewType",
label = "Select to view:",
choices = c(' ', "Projects"), #will have other dataframes to select from
selected = ' '),
conditionalPanel(
condition = "input.viewType =='Projects'",
uiOutput("projectSelection"),
uiOutput("showMeta"),
uiOutput("showVal"),
textOutput("text")
)
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
server.R
server <- function(input, output) {
viewSelection <- reactive({
if(input$viewType == "Projects"){
projectDT <- getJSON("an API url")
#replace spaces with dots in headers
names(projectDT) <- gsub(" ", ".", names(projectDT))
#show table
output$mytable <- DT::renderDataTable(DT::datatable(projectDT))
#Display columns from project to view
output$showMeta <- renderUI({
selectInput("metalab",
"Metadata Label:",
c(" ", unique(as.vector(colnames(projectDT))))
)
})
#Display unique column values to choose from in checkbox
#Gives Warning: Error in [.data.frame: undefined columns selected
output$showVal <- renderUI({
checkboxGroupInput("metaval",
"Metadata Value:",
choices = unique(as.vector(unlist(projectDT[input$metalab])))
)
})
}
})
output$mytable <- DT::renderDataTable({DT::datatable(viewSelection())})
}
I'm currently struggling to produce a new column in the dataframe based off the user's selections. So far, it displays what I would like it to in terms of the dropdown and checkboxes but I wasn't able to move any further with that. I'm not exactly sure where my problem lies - is my table rendering improperly, am I not adding a new column correctly?
I tried to access input$metalab and input$metaval but they return NULL outside a renderUI/renderText context. I've tried simply duplicating a column based on user choice but this doesn't work either:
projectDT['newCol'] = projectDT[input$metalab]
Any help is greatly appreciated! Sorry for the long blurb!