I am trying to find a way to use the textInput field to allows users to indicate column names of a data frame in Shiny. A little background on the dashboard:
Dashboard allows users to upload a csv file and indicate the columns in the data frame they want to use. Then, they choose column and row names. The data they upload will then be converted into a smaller data frame that will be used for a correspondence analysis.
Everything works fine except for the column/row naming aspect. For some reason, when the program runs it assigns the text input as the name of the first row/column, and then makes the others titled "NA". I tried using strsplit() to break the text input up, but that won't work.
Could someone tell me what I'm doing wrong? Here is what my code and issue look like:
ui <- fluidPage(
# Application title
titlePanel("Perceptual Map Dashboard"),
sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
#Excel doc row and column names
numericInput(inputId="startcol",label="Input start column:",value="", min=1,max=10000),
numericInput(inputId="endcol",label="Input end column:",value="", min=1,max=10000),
#Inputing brands and emotions
br(),
numericInput(inputId = "rownums",label = "How many emotions are you evaluating?",value = "", min = 1,max = 10000),
br(),
textInput ( 'brands', 'List the brands included in your perceptual map (separated by commas):', value=""),
textInput ( 'emotions', 'List the emotions included in your perceptual map (separated by commas):', value=""),
#Removing brands and emotions
#Select graph type
textInput(inputId="plottitle",label="Title your graph:"),
#Upload Excel Grid
fileInput(inputId = 'data', 'Upload CSV File',
accept=c('.csv')),
actionButton("go","Create Map")
),
# Visual Output
mainPanel(
wellPanel(h4('Visual')),
plotOutput(outputId = "plot", width = "100%", height=500),
downloadButton("downloadPlot", "Download Visual")
)
)
)
server <- function(input,output){
observeEvent(input$go,{
x <- read.csv(input$data$datapath, header = F)
print(x)
str(x)
plot1 <- reactive({
x<-x[,as.numeric(input$startcol):as.numeric(input$endcol)]
column.sums<-colSums(x)
print(column.sums)
pmd.matrix<-matrix(column.sums, byrow = T, nrow=as.numeric(input$rownums))
pmd.df2<-as.data.frame(pmd.matrix)
# colnames(pmd.df2) = names
# print(pmd.df2)
# # row.names(pmd.df2)= c(as.character(input$emotions))
print(pmd.df2)
fit <- CA(pmd.df2, graph=F)
K <- plot.CA(fit, col.row = "blue", col.col="black", cex=.6,new.plot=T,
title=input$plottitle)
return(K)
})
output$plot<- renderPlot({
print(plot1())
})
output$downloadPlot2 <- downloadHandler(
filename = "Shinyplot.png",
content = function(file) {
png(file)
print(plot1)
dev.off()
})
})
}
shinyApp(ui = ui, server = server)
ISSUE I AM SEEING:
c("“Burlington”", "”JC Penny”", "”Kohls”", "”TJ Maxx”", "”Marshalls”", "”Nordstrom Rack”", "”Old Navy”", "”Target”", "”Walmart”", "”Macys”", "”Amazon”", "”Nordstrom”", "”Ross”")
1 113
2 113
3 69
4 55
5 91
6 73
7 106
8 77
9 76
10 80
11 58
12 86
NA NA NA NA NA NA NA NA NA NA NA NA
1 63 78 87 68 67 33 57 67 87 96 77 41
2 123 100 71 70 50 77 67 93 33 44 35 36
3 93 84 68 56 79 44 41 41 129 40 132 38
4 43 51 62 63 59 50 58 109 70 82 66 27
5 105 91 65 79 51 70 49 67 40 56 34 36
6 69 71 89 66 55 68 83 51 39 67 34 44
7 53 61 46 59 53 76 53 80 39 65 32 30
8 55 55 52 61 63 48 85 70 57 97 60 32
9 66 68 71 72 71 47 52 57 89 61 94 48
10 61 55 60 56 52 56 39 60 37 39 33 65
11 69 66 56 56 40 63 69 68 27 65 27 69
12 70 66 59 61 46 73 62 72 47 86 113 63
It looks like there may be two issues. Firstly, I assume that the column names are defined in "brands" in the UI. In the example, the naming is commented out, but I've created a minimal example that I think re-creates what you're seeing.
1) Defining "brands" as a comma-separated string as your textInput requests
brands<- "b1,b2,b3"
2) The other issue it looks like "names" variable isn't being defined? Perhaps this is due to commenting-out. It would need to be "names <- input$brands" I assume. Here, outside of shiny environment:
names <- brands
3) Recreating the process with a minimal data.frame 'x'
x<-data.frame(a=c(1,2,3),b=c(1,2,3),c=c(4,3,2))
pmd.matrix <- matrix(colSums(x),byrow=T,nrow=1)
pmd.df2 <- as.data.frame(pmd.matrix)
colnames(pmd.df2) <- names
pmd.df2
> pmd.df2
b1,b2,b3 NA NA
1 6 6 9
names <- unlist(strsplit(brands,","))
colnames(pmd.df2) <- names
pmd.df2
And returning result of named columns:
> pmd.df2
b1 b2 b3
1 6 6 9