0
  1. I`m uploading an Excel file

  2. Choosing specific row in a file with data, for example it looks like this

[enter image description here

  1. Now need to generate plot according values in a row I choose. This is the result I get:

Im getting this result

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)
  1. There are 128 values, each value represents quarter of a year It starts from 1993Q1,1993Q2,1993Q3,1993Q4...2023Q1,2023Q2,2023Q3,2023Q4
  2. 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")
  3. Any leads how to make this plot look good?
Ilshat
  • 1
  • 1
  • I'm not totally clear of what your question is. But it seems like you're working on the plotting and the Shiny business is working fine. If that's the case, the maybe post a question just with some sample data and your plot code, and explain what you don't like about the plot. – Michael Dewar Aug 10 '23 at 07:15
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. As is we can't run your code and figure out what's the issue. But from the image of your data I would guess that your data needs some preprocessing as it is in wide format whereas in your app you are assuming you have one column with the time aka plot_data[, 1] and one with the values aka plot_data[, 2]. – stefan Aug 10 '23 at 07:19
  • Sorry for this, here is link for google drive with file im trying to work https://docs.google.com/spreadsheets/d/1B1Odj0Lrt93HjIXnC_8afOlC4FagIrja/edit?usp=sharing&ouid=107697355230079722130&rtpof=true&sd=true Thanks for your responses – Ilshat Aug 10 '23 at 08:32
  • Looking at the logic of your code, the result you're seeing implies that either `!is.null(plot_data)` or `all(sapply(plot_data, is.numeric))` is `FALSE`. So adding `print(plot_data)` immediately before the `if` statement in your `renderPlot` should be informative. If you can't figure out what's wrong on your own, please show us what you see after doing that. – Limey Aug 11 '23 at 10:27
  • Still have same problem when trying to generate plot, i attahed link to an excel file above so everyone can look at how data where given and understand better what im trying to explain with my "good" english :D As you can see values start from cell E6 in an Excel file, row is 6 and column is E(where name of quarters starts which i need to be shown on plot with values) – Ilshat Aug 14 '23 at 10:06

0 Answers0