0

New member here. I'm trying to show a table in shiny. I would like the data for it to be obtained from a csv file posted on the site. Additionally, so that the user can select the year range for which this data will be loaded. On this site, each year is in a separate csv file. I use purrr to loop through each file name by using map_df which takes the results from each iteration and row binds them into a data frame. Here is my code:

Years <- c(2013, 2014, 2015, 2015, 2016, 2017, 2018, 2019, 2020, 2021)
ui <- fluidPage(
    uiOutput("moreControls"),
    textOutput("test"),
    DT::dataTableOutput("mytable")
)
server <- function(input, output) {
    
    output$moreControls <- renderUI({
        sliderInput("myslider", label = h4("Select years"), min = min(Years),
                    max = max(Years), value = c(2013, 2021), step=1)
    })
    
    output$test <- renderText({min(input$myslider):max(input$myslider)})
    
    inputdata <- reactive({
        try({
            return(
    years.names <- min(input$myslider):max(input$myslider),
    scrape_csv <- function(years.names){
        #create urls
        years.urls <- paste0("https://www.nbp.pl/kursy/Archiwum/archiwum_tab_a_",years.names,".csv")
        #read in file
        df <- read.csv((years.urls), sep = ";",dec=",",header=TRUE,stringsAsFactors=FALSE)
        return(df)
    }
    d1 <- years.names %>% map_df( scrape_csv ),
    d1 <- d1[-2],
    d1 <- d1[-c((length(d1)-3):length(d1))],
    mysub <- function(x) {sub(",",".",x)}
    d <- (apply(d1, 2, mysub )),
    d <- data.frame(apply(d, 2, as.numeric)),
    colnames(d) <- gsub("X","",colnames(d)),
    d <-d[-c(1), ],
    d <- as.data.frame(d),
    d$data <- as.Date(as.character(d$data),format="%Y%m%d"),
    d[,-1] <-round(d[,-1],2),
    ret <- d
            )
        },silent=T)
        
        return(dane)
    })   
         
    output$mytable = DT::renderDataTable({
        DT::datatable(  
            inputdata(),
            rownames = FALSE,
            options = list(
                scrollX = TRUE,
                pageLength = 10,
                lengthMenu = seq(from=10,by=10,to=100)
    ))
})    


            
}  
        
shinyApp(ui = ui, server = server)

When I run the part of the code responsible for reading the csv in a separate R file,:

years.names <- min(Years):max(Years),
    scrape_csv <- function(years.names){
        #create urls
        years.urls <- paste0("https://www.nbp.pl/kursy/Archiwum/archiwum_tab_a_",years.names,".csv")
        #read in file
        df <- read.csv((years.urls), sep = ";",dec=",",header=TRUE,stringsAsFactors=FALSE)
        return(df)
    }
    d1 <- years.names %>% map_df( scrape_csv ),
    d1 <- d1[-2],
    d1 <- d1[-c((length(d1)-3):length(d1))],
    mysub <- function(x) {sub(",",".",x)}
    d <- (apply(d1, 2, mysub )),
    d <- data.frame(apply(d, 2, as.numeric)),
    colnames(d) <- gsub("X","",colnames(d)),
    d <-d[-c(1), ],
    d <- as.data.frame(d),
    d$data <- as.Date(as.character(d$data),format="%Y%m%d"),
    d[,-1] <-round(d[,-1],2),
    ret <- d

obviously without using years.names <- min(input$myslider):max(input$myslider) , I use instead years.names <- min(Years):max(Years) then the program runs correctly. Is there any way to fix this? Best regards

  • Hi! Try using ```input$myslider[1]``` instead of ```min(input$myslider)``` and ```input$myslider[2]``` instead of ```max(input$myslider)```. See [this post](https://stackoverflow.com/questions/38181744/r-shiny-input-slider-range-values) – NovaEthos Jun 08 '21 at 14:33
  • `server` code does not work and returns error. Can you check the code posted? – Ronak Shah Jun 09 '21 at 01:52

0 Answers0