Hello!
I would like to read in an excel file, get the variable names (in the server function), and pass them to a selectInput to use for x and y variable selection. However, I'm having trouble with the output variable itself. Here is the code:
xa.df <- data.frame(x=1:10,y1=rnorm(10),y2=rexp(10))
library(openxlsx)
write.excel(xa.df,file="test1a.xlsx")
library(shiny)
library(readxl)
library(shinyjs)
library(ggplot2)
runApp(
list(
ui = fluidPage(
useShinyjs(),
div(
id="form",
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
conditionalPanel(
condition="output.names1",
print("stuff"),
selectInput("sel1","x variable",choices=output.names1)
),
actionButton("resetAll", "Draw the plot"))
),
mainPanel(
verbatimTextOutput("contents"))
),
server = function(input, output,session){
text_reactive <- eventReactive(input$resetAll, {
print("in event")
print(str(dist()))
print(names(dist()))
print(input$x1)
myx1 <- gsub(" ","",input$x1)
myy1 <- gsub(" ","",input$y1)
print(which(myx1==names(dist())))
x4 <- which(myx1==names(dist()))
x5 <- which(myy1==names(dist()))
x6 <- data.frame(x=dist()[[x4]],y=dist()[[x5]],z=1:10)
print(str(x6))
x6
}
)
dist <- reactive({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
x3 <- as.data.frame(read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1))
return(x3)
})
output$contents <- renderText({
names(dist())
})
output$names1 <- reactive({
names(dist())
})
outputOptions(output,'names1',suspendWhenHidden=FALSE)
})
)
)
I have tried all kinds of variations on output.names1, but no luck.
Any suggestions much appreciated.
Thanks,
Erin