...I have a dataframe consist of 100 samples. There are two column, one url link, second a name or a heading to the url link. I want to display all those headings in shiny interface. Clicking which leads the user to the respective link page. How can I do that? Please helpstrong text
If your dataframe is called df
and columns called url
and heading
this should work...
library(shiny)
library(DT)
df$link <- paste0("<a href='", df$url, "' target='_blank'>", df$heading, "</a>")
ui <- fluidPage(
DT::dataTableOutput("table")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
DT::datatable(df[, "link", drop = FALSE], escape = FALSE)
})
}
shinyApp(ui, server)
Hey paul I exactly used the same code.
But its throwing a error 'data' must be 2-dimensional (e.g. data frame or matrix)
sorry yes the df[, "link"]
subset was converting the data frame to a character vector as there was only 1 column left. Adding drop = FALSE
inside the square brackets will ensure it keeps the data frame class. I've updated the code above to include this.
thanks,
Paul
Thanxxx a lot Paul Its working fine now
Hey Paul I have a date column too within that data. And I want to filter out the samples through selected date range. I am getting an error
Error in UseMethod: no applicable method for 'filter_' applied to an object of class "c('datatables', 'htmlwidget')"
Below is the code
ui <- fluidPage( dateRangeInput('dateRange',
label = 'Filter news by date',
start = as.Date('2001-01-01') , end = as.Date('2018-06-01')),
DT::dataTableOutput("table")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
DT::datatable(df[, c("link", "Date"), drop = FALSE], escape = FALSE) %>%
dplyr::filter(df$Date >= input$dateRange[1] & df$Date <= input$dateRange[2])
})
}
Hi Abhishek, sorry for the late response! I've been on holiday.
So you need to filter the data inside the renderDataTable function before providing it to the table itself.
Something like this:
output$table <- DT::renderDataTable({
table_data <- df %>%
filter(Date >= input$dateRange[1] & Date <= input$dateRange[2]) %>%
select(link, Date)
DT::datatable(table_data, escape = FALSE)
})
Thanks a lot again paul. sorry for the late reply