Dear colleagues I need your help to let the user to select 'x and y-variable' when the .csv is uploaded.
I have build the app, but the selectInput does not function.
Any help will be much appreciated. Thank you in advance and have a great day.
library(DT)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(ggvis)
library(ggplot2)
library(dplyr)
shinyUI(dashboardPage(
dashboardHeader(title = "Data Visualization"),
dashboardSidebar(
width = 350,
radioButtons(
"fileType_Input",
label = h4("Choose File type"),
choices = list(".csv/txt" = 1, ".xlsx" = 2),
selected = 1,
inline = TRUE
),
fileInput(
'file1',
h4('Upload Your Data'),
accept = c(
'text/csv',
'text/comma-separated-values,text/plain',
'.csv',
'.xlsx'
)),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis Variable:",
choices = colnames(df)),
selectInput(inputId = "y", label = "Select y-axis Variable:",
choices = colnames(df)),
menuItem("Raw Data", tabName = "RawData",
icon = icon("table")),
menuItem("Data Summary", tabName = "DataSummary",
icon = icon("calculator")),
menuItem("Charts", tabName = "Charts",
icon = icon("chart-bar"))
)),
dashboardBody(
tabItems(
tabItem( tabName = "RawData",
fluidRow(
box(
title = "View Data",
width = NULL,
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
DT::dataTableOutput('contents'))
)),
tabItem(tabName = "DataSummary",
box(verbatimTextOutput("summary"))
),
tabItem(tabName = "Charts",
box(
plotOutput('plot'))
)
))
))
shinyServer(function(input, output, session) {
myData <- reactive({
req(input$file1)
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE)
data
})
output$contents <- DT::renderDataTable({
DT::datatable(myData())
})
observe({
data <- myData()
updateSelectInput(session, 'x', choices = names(data))
})
#gets the x variable name, will be used to change the plot legends
xVarName <- reactive({
input$x
})
observe({
data <- myData()
updateSelectInput(session, 'y', choices = names(data))
})
#gets the y variable name, will be used to change the plot legends
yVarName <- reactive({
input$y
})
output$summary <- renderPrint({
summary(myData())
})
output$select <- renderUI({
df <- myData()
selectInput("variable", "Variable:",names(df))
})
output$plot <- renderPlot({
df <- myData()
df <- df[,input$variable]
ggplot(df, aes_string(x = input$x, y = input$y))+
geom_line()
})
})