I`m uploading an Excel file
Choosing specific row in a file with data, for example it looks like this
[
- Now need to generate plot according values in a row I choose. This is the result I get:
library(shiny)
library(readxl)
ui <- fluidPage(
titlePanel("AFSI Data Reformat"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose xlsx file", accept = c(".xlsx")),
numericInput("selected_row", "Select Row Number", value = 1, min = 1, step = 1),
actionButton("generate_plot", "Generate Plot")
),
mainPanel(
tableOutput("selected_row_data"),
plotOutput("selected_row_plot")
)
)
)
server <- function(input, output) {
uploaded_data <- reactiveVal(NULL)
selected_data <- reactiveVal(NULL)
observe({
inFile <- input$file1
if (!is.null(inFile)) {
uploaded_data(readxl::read_excel(inFile$datapath))
}
})
output$selected_row_data <- renderTable({
req(uploaded_data())
selected_row <- input$selected_row
if (selected_row <= nrow(uploaded_data())) {
selected_data(uploaded_data()[selected_row, , drop = FALSE])
} else {
NULL
}
})
output$selected_row_plot <- renderPlot({
req(selected_data())
plot_data <- selected_data()
if (!is.null(plot_data) && all(sapply(plot_data, is.numeric))) {
plot(
x = plot_data[, 1], y = plot_data[, 2],
xlab = colnames(plot_data)[1], ylab = colnames(plot_data)[2],
main = paste("Row", input$selected_row, "Data"),
xlim = range(plot_data[, 1], na.rm = TRUE),
ylim = range(plot_data[, 2], na.rm = TRUE)
)
} else {
# If data cannot be plotted, show a placeholder message
plot(NULL,
xlim = c(0, 1), ylim = c(0, 1),
main = paste("Row", input$selected_row, "Data (Cannot be Plotted)")
)
text(0.5, 0.5, "Data cannot be plotted", col = "red", cex = 1.5)
}
})
}
shinyApp(ui, server)
- There are 128 values, each value represents quarter of a year It starts from 1993Q1,1993Q2,1993Q3,1993Q4...2023Q1,2023Q2,2023Q3,2023Q4
- How can I generate plot so i can see values and their quarter on a plot?(Some of rows can have only 3 out of 128 values-still need a plot for these 3 values, some of them missing values at all - so output for this can be "Cannot generate plot due to missing values")
- Any leads how to make this plot look good?