I would like to use the filtered data from datatable to create a ggplot. I implemented as well two widgets, which after filtering the datatable will allow user to choose x & y axis to display. After setting up the widgets and so on i got an error:
Error in `[.data.frame`(data_melt, filtered_data) : undefined columns selected
I have no idea why this happened.
my data example:
Rundheit Diff Charge Ord..Nr. Block.Nr.
1 0.24 0.20 754331 738 1
2 0.26 0.21 783345 738 2
3 0.25 0.15 795656 738 3
4 NA 0.14 798431 738 4
5 NA 0.12 799651 738 5
6 0.24 NA 805454 738 6
The NA values must stay in my data
UI:
ui <- dashboardPage(
dashboardHeader(title = "WW"),
dashboardSidebar(
selectizeInput(inputId = "yaxis",
label = "Y-axis (Diagramm)",
choices = list("Rundheit" = "Rundheit",
"Diff" = "Diff"),
selected = c("Rundheit"), multiple=TRUE),
selectInput(inputId = "xaxis",
label = "X-axis (Diagramm)",
choices = names(data_melt),
selected = "Block.Nr.")
),
dashboardBody(
fluidRow(
tabBox(status = "primary", width = NULL, height = "1000px",
tabPanel(title="Tabelle filtern", div(style = 'overflow-y: scroll; max-height: 950px; position:relative;',
dataTableOutput("tabelle"))),
tabPanel("Diagramm", plotOutput("plot1")),
tabPanel("Histogramm", plotOutput("plot2"))))
))
Server:
server <- function(input, output, session) {
output$tabelle <- renderDataTable({
datatable(data[, c("Rundheit", "Diff", "Charge.", "Ord..Nr.", "Block.Nr.")], class = 'cell-border stripe',
rownames=FALSE, filter="top",
options = list(lengthChange = FALSE, columnDefs = list(list(width = '200px', targets = "_all"), list(bSortable = FALSE, targets = "_all"))), callback=JS("
//hide column filters for two columns
$.each([0, 1], function(i, v) {
$('input.form-control').eq(v).hide()});",
"var tips = ['Rundheit', 'Diff', 'Charge',
'Ord..Nr.', 'Block.Nr.'],
header = table.columns().header();
for (var i = 0; i < tips.length; i++) {
$(header[i]).attr('title', tips[i]);}")) %>%
formatStyle("Rundheit", color = 'red', backgroundColor = 'lightyellow', fontWeight = 'bold')
})
output$plot1 <- renderPlot({
filtered_data <- input$tabelle_rows_all
data_filt <- data_melt[filtered_data]
ggplot(data=data_filt, aes_string(x = input$xaxis, y = input$yaxis), environment = environment())+ geom_line(aes(group=1), size=1) +
theme(axis.text.y=element_text(size=15), axis.text.x=element_text(size=15), axis.title.x = element_text(size=18, face="bold"),axis.title.y = element_text(size=18, face="bold"))
})
}
shinyApp(ui = ui, server = server)
Does anyone have any idea why it is not working, and how i can define columns then.
I have seen post: http://stackoverflow.com/questions/30042456/using-filtered-datatables-in-shiny
However for the code:
[filtered_data, "name of the column"]
is not working by using for example:
data_filt <- data_melt[filtered_data, ]
Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
And
Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
'from' must be of length 1
as well as:
data_filt <- data_melt[filtered_data, input$xaxis]
and it gives an error (depending on the type of the column):
Error : ggplot2 doesn't know how to deal with data of class factor
Error : ggplot2 doesn't know how to deal with data of class numeric
and i need input$yaxis as well to be implemented...
Therefore i tried:
data_filt <- data_melt[filtered_data, c(input$xaxis, input$yaxis)]
Than i got an error
Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
'from' must be of length 1
Just for the Info I played bit with this code, and even by specifying name of the column it throws an error and i cannot even specify more then one
I have tried things like:
[filtered_data, "Rundheit")
[filtered_data, c("Rundheit", "Diff")]
Thank You very much for any ideas
So your code was a bit "messy", with a few compiler errors, some missing code, and I had to enter your data by hand. Not everyone would do that... I am also not sure where the data and data_melt stuff was supposed to happen, so I just went for data_melt. Anyway I got it to work, and I have to admit this is powerful and fascinating functionality. I hope it is what you want, although I did not see all of your error messages per-se.
Your main mistake was setting rownames=F, since the rownames are what the input$tabelle_rows_all uses to filter the table. I also added an nrow guard to the ggplot call to keep it from choking on empty dataframes.
Here is the working code:
library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(DT)
rr <- c(0.24,0.26,0.25,NA,NA,0.24)
dd <- c(0.20,0.21,0.15,0.14,0.12,NA)
cc <- c(74331,783345,795656,798431,799651,805454)
oo <- rep(738,6)
bb <- 1:6
data_melt <- data.frame(Rundheit=rr,Diff=dd,Charge.=cc,Ord..Nr.=oo,Block.Nr.=bb)
ui <- dashboardPage(
dashboardHeader(title = "WW"),
dashboardSidebar(
selectizeInput(inputId = "yaxis",
label = "Y-axis (Diagramm)",
choices = list("Rundheit" = "Rundheit",
"Diff" = "Diff"),
selected = c("Rundheit"), multiple=TRUE),
selectInput(inputId = "xaxis",
label = "X-axis (Diagramm)",
choices = names(data_melt),
selected = "Block.Nr.")
),
dashboardBody(
fluidRow(
tabBox(status = "primary", width = NULL, height = "1000px",
tabPanel(title="Tabelle filtern",
div(style = 'overflow-y: scroll; max-height: 950px; position:relative;',
dataTableOutput("tabelle"))),
tabPanel("Diagramm", plotOutput("plot1")),
tabPanel("Histogramm", plotOutput("plot2"))))
))
server <- function(input, output, session) {
output$tabelle <- renderDataTable({
datatable(data_melt[, c("Rundheit", "Diff", "Charge.", "Ord..Nr.", "Block.Nr.")],
class = 'cell-border stripe',
filter="top",
options = list(lengthChange = FALSE,
columnDefs = list(list(width = '200px', targets = "_all"),
list(bSortable = FALSE, targets = "_all"))),
callback=JS("
//hide column filters for two columns
$.each([0, 1], function(i, v) {
$('input.form-control').eq(v).hide()});
var tips = ['Rundheit', 'Diff', 'Charge',
'Ord..Nr.', 'Block.Nr.'],
header = table.columns().header();
for (var i = 0; i < tips.length; i++) {
$(header[i]).attr('title', tips[i]);}")) %>%
formatStyle("Rundheit", color='red', backgroundColor='lightyellow', fontWeight='bold')
})
output$plot1 <- renderPlot({
filtered_data <- input$tabelle_rows_all
data_filt <- data_melt[filtered_data,]
if (nrow(data_filt>0)){
g <-ggplot(data=data_filt, aes_string( x=input$xaxis, y=input$yaxis),
environment=environment())+
geom_line(aes(group=1), size=1) +
theme(axis.text.y=element_text(size=15),
axis.text.x=element_text(size=15),
axis.title.x = element_text(size=18, face="bold"),
axis.title.y = element_text(size=18, face="bold"))
return(g)
} else {
return(NULL)
}
})
}
shinyApp(ui = ui, server = server)
And here are a couple of screen shots to show it working:

Yielding:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With