0

I have this piece of code to show some chart + last row from table, but date not showed properly in tableOutput, Any idea please ?

Last row is this

"date","confirmed","diff"
2020-11-19,56898415,650433

but shows this 18585.00 56898415.00 650433.00

Here is full code

ui <- fluidPage(
                titlePanel("Confirmed Cases"),
                
                sidebarLayout(
                  position='right',
                  sidebarPanel(
                  tableOutput("table1")
                    
                  ),
                  mainPanel(
                  
                plotlyOutput("distPlot")
                  ))
)

# Define server logic ----
server <- function(input, output) {
  data <- read_csv("confirmed_all.csv")
  
  data_last <- tail(data,1)
  
  output$table1 <- renderTable(data_last)
  
  output$distPlot <- renderPlotly({
    ggplotly(
    ggplot(data, aes(x = date, y = confirmed)) +
      geom_line(colour = "grey", aes(date, confirmed)) + 
      scale_y_continuous(labels = comma))
    
  })
  
  
}

# Run the app ----
shinyApp(ui = ui, server = server)

Link to my new project http://webcovid19.online/

... OK, looks that R is showing date in numeric output from 1970 (Posix date), but how to convert it back ? My current code looks lhis data_last <- tail(data[ , c("date", "confirmed")],1) to show only specified column

Here is small dataset

"date","confirmed","diff"
2020-01-27,2927,809
2020-01-28,5578,2651
2020-01-29,6167,589
2020-01-30,8235,2068
Andrew
  • 309
  • 1
  • 2
  • 11

1 Answers1

1

Here is a way using kableExtra package:

data <- data.frame(date = c("2020-01-27", "2020-01-28", "2020-01-29", "2020-01-20"),
                   confirmed = c(2927, 5578, 6167, 8253),
                   diff = c(809, 2651, 589, 2068))

data$date <- as.POSIXct(data$date)

library(shiny)
library(kableExtra)
library(knitr)
library(plotly)

ui <- fluidPage(
    titlePanel("Confirmed Cases"),
    
    sidebarLayout(
        position='right',
        sidebarPanel(
            tableOutput("table1")
        ),
        
        mainPanel(
            plotlyOutput("distPlot")
        ))
)

# Define server logic ----
server <- function(input, output) {
    data_last <- tail(data, 1)
    
    output$table1 <- function() {
        data_last %>% 
            knitr::kable(format = "html", row.names = FALSE) %>% 
            kable_styling()
    }
}

# Run the app ----
shinyApp(ui = ui, server = server)
Eric Krantz
  • 1,854
  • 15
  • 25
  • OK, your option works with renderDataTable (as.Date), but how to implement in my universal scenario when I read data from csv file ? also the output from Datatable is uselessly difficult with next page buttons... etc, I just want simple table like in TableOutput, Any idea please ? I updated my Q with dataframe exaple. – Andrew Nov 22 '20 at 13:15
  • ...I just wanna show properly last row from my dataframe csv in simple table – Andrew Nov 22 '20 at 13:41
  • I've updated the answer for better formatting, and removing the row name from the table. To read from the .csv file, just substitute your call to the .csv file back in (i.e., . `data <- read_csv("confirmed_all.csv")`. – Eric Krantz Nov 22 '20 at 16:00
  • Thanks Eric, now is working fine, last question : how change number formatting ? Here is current output > Confirmed 58143122, I would like to have comma separated like 58,143,122 – Andrew Nov 22 '20 at 19:15
  • Probably bring up a new issue for that, or search stack overflow. [Here is a starting point:](https://community.rstudio.com/t/how-to-format-a-variable-with-commas-now-that-scales-has-changed/56311) – Eric Krantz Nov 22 '20 at 21:01
  • Hi Eric, I used this template also for showing last increase numbers...How can I add "+" sign before value ? like "+ 3,565" – Andrew Dec 19 '20 at 21:28
  • 1
    @Andrew You can use `paste0`. For example `x <- 3565`, `y <- paste0("+", x)`. Then display `y` in the table. This can be a new column in your data frame so easy to put in the table. See this post to format with the commas for thousands separator: https://stackoverflow.com/questions/29465941/format-number-in-r-with-both-comma-thousands-separator-and-specified-decimals – Eric Krantz Dec 21 '20 at 16:57
  • Bravo, Thanks Eric :) – Andrew Dec 21 '20 at 20:17
  • I just realized that after this change I lost my decimal comma separation output due to numeric was changed to character ` knitr::kable(format = "html", row.names = FALSE, align='l',col.names = c("daily increase from last day"), ) %>% kable_styling(font_size = 20)` – Andrew Dec 21 '20 at 22:20
  • Hi Eric, Do you have any idea how fix both issues ? ( comma separation digits and paste0 "+" sign in kable output ) Looks that not possible merge both. – Andrew Dec 27 '20 at 23:52
  • Yeah, once you add the '+' sign, its a character vector and your comma isn't going to work. You could write your own function to accept a character vector and add commas to to it. I.e. you will send the function the character vector "+12345678" and it returns "+12,345,678". Good programming exercise! – Eric Krantz Dec 28 '20 at 21:23