I have the shiny dashboard below in which I want to use a variable from my pickerInput() and create a plot. The issue is that if I use ,for example name or snID instead of input$DB the plot is created. But when I use input$DB I get: Warning: Error in table: all arguments must have the same length
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
header = dashboardHeader(title = "My dashboard"),
sidebar = dashboardSidebar(
uiOutput("dbs")
),
body = dashboardBody(
plotlyOutput("fn")
)
)
server <- function(input, output, session) {
sts<-c("Rev","Rev")
sID<-c("123","124")
snID<-c("23","34")
name<-c("s","d")
pe<-data.frame(sts,sID,snID,name)
output$dbs<-renderUI({
pickerInput("DB", "Select Database/s",
choices = c("name","snID"),
multiple = F,options = list(`actions-box` = TRUE),
selected = "name")
})
output$fn<-renderPlotly({
#2.2 MAKING A TABLE for public.exists
tbl<-table(pe[[input$DB]], pe$sts)
ggplotly(
ggplot(as.data.frame(tbl), aes(!!sym(input$DB), Freq, fill = sts))
)
})
}
shinyApp(ui, server)
I suspect your output$fn reactive is executing before input$DB has a value. Therefore, add
req(input$DB)
at the start of the reactive, and you should be OK.
In the absence of any demo input data, it's difficult to be definitive.
Related
I'm having some trouble getting my R Shiny code to produce a dynamic dashboard where the user can select 1 or more independent variables in a linear regression model and print the results. I've been able to successfully follow examples where the user only inputted one independent variable, but with multiple independent variables, I have not found the same luck. I'm not sure what I am doing wrong, but I get an error that reads, "invalid term in model formula".
Below is the code I've used so far:
library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
#data(mtcars)
AttributeChoices=c("cyl","disp","hp","drat","wt","qsec","vs")
# Define UI for application
ui = fluidPage(
navbarPage("R Shiny Dashboard",
tabPanel("Welcome",
tabName = "welcome",
icon=icon("door-open"),
fluidPage(theme=shinytheme("cerulean"),
h1("Welcome to my Shiny Dashboard!"),
br(),
p(strong(tags$u("What is this dashboard all about?"))),
p("I'm going to do stuff."),
br(),
p(strong(tags$u("Here's another question."))),
p("Here's my answer."),
br(),
p(strong(tags$u("How can I use this dashboard?"))),
p("You can click on any of the tabs above to see a different analysis of the data.")
)),
tabPanel("Regression",
tabname="regression",
icon=icon("calculator"),
selectInput(inputId = "indep", label = "Independent Variables",
multiple = TRUE, choices = as.list(AttributeChoices), selected = AttributeChoices[1]),
verbatimTextOutput(outputId = "RegOut")
)
))
# Define server logic
server <- function(input, output) {
#-------------------REGRESSION-------------------#
lm_reg <- reactive(
lm(as.formula(paste(mtcars$mpg," ~ ",paste(input$indep,collapse="+"))),data=CFD)
)
output$RegOut = renderPrint({summary(lm_reg())})
}
# Run the application
shinyApp(ui = ui, server = server)
Reading similar posts on StackOverflow seem to suggest the problem might be with the column names having spaces, but that's not the case here in this example. I am not sure how to resolve this issue. Can anyone help point me in the right direction? Thank you!
Here you go, I like to use the recipe package for problems like this instead of relying on very hard string manipulation, the trick is to use the !!! operator, you can even get fancy and let the user pass some select helpers
library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(recipes)
#data(mtcars)
AttributeChoices=c("cyl","disp","hp","drat","wt","qsec","vs")
# Define UI for application
ui = fluidPage(
navbarPage("R Shiny Dashboard",
tabPanel("Welcome",
tabName = "welcome",
icon=icon("door-open"),
fluidPage(theme=shinytheme("cerulean"),
h1("Welcome to my Shiny Dashboard!"),
br(),
p(strong(tags$u("What is this dashboard all about?"))),
p("I'm going to do stuff."),
br(),
p(strong(tags$u("Here's another question."))),
p("Here's my answer."),
br(),
p(strong(tags$u("How can I use this dashboard?"))),
p("You can click on any of the tabs above to see a different analysis of the data.")
)),
tabPanel("Regression",
tabname="regression",
icon=icon("calculator"),
selectInput(inputId = "indep", label = "Independent Variables",
multiple = TRUE, choices = as.list(AttributeChoices), selected = AttributeChoices[1]),
verbatimTextOutput(outputId = "RegOut")
)
))
# Define server logic
server <- function(input, output) {
#-------------------REGRESSION-------------------#
recipe_formula <- reactive(mtcars %>%
recipe() %>%
update_role(mpg,new_role = "outcome") %>%
update_role(!!!input$indep,new_role = "predictor") %>%
formula())
lm_reg <- reactive(
lm(recipe_formula(),data = mtcars)
)
output$RegOut = renderPrint({summary(lm_reg())})
}
# Run the application
shinyApp(ui = ui, server = server)
I have a Shiny app with a datatable. I would like to implement a button at the top of this datatable (but below its title) so that, when I click on it, the LaTeX code necessary to build this table is copied to clipboard.
Basically, this button would work the same way that the "copy" or "csv" buttons (see here part 2) but with LaTeX code.
Here's a reproducible example :
library(DT)
library(shiny)
library(shinydashboard)
library(data.table)
library(stargazer)
library(clipr)
ui <- dashboardPage(
dashboardHeader(title = "test with mtcars", titleWidth = 1000),
dashboardSidebar(
selectizeInput("var.cor", label = "Correlation",
choices = names(mtcars),
selected = c("mpg", "cyl"),
multiple = TRUE)
),
dashboardBody(
tabsetPanel(
tabPanel("test with mtcars",
br(),
box(dataTableOutput("cor"),
width = NULL),
actionButton("copy.latex", label = "Copy to LaTeX")
)
)
)
)
server <- function(input, output) {
var.selected <- reactive({
out <- input$var.cor
out
})
user.selection <- reactive({
mtcars <- mtcars[, var.selected()]
})
output$cor <- renderDataTable({
dtable <- user.selection()
tmp <- datatable(cor(dtable),
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
"copy",
list(
extend = "collection",
text = 'test',
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('test', true, {priority: 'event'});
}")
)
)
)
)
observeEvent(input$test, {
write_clip(stargazer(tmp),
object_type = "auto")
})
tmp
})
observeEvent(input$copy.latex, {
write_clip(stargazer(input$cor),
object_type = "character")
})
}
shinyApp(ui, server)
I tested two things in this code :
firstly, I inspired from here. This is the code of observeEvent nested in renderDataTable. However, either the text in the clipboard is % Error: Unrecognized object type, either I have an error : Error in : Clipboard on X11 requires that the DISPLAY envvar be configured.
secondly, I created a button outside the datatable but it doesn't work because I have Error in : $ operator is invalid for atomic vectors
Does somebody know how to do it ?
To copy the dataframe to clipboard in server:
library(shiny)
library(shinyjs)
library(DT)
table <- iris[1:10,]
ui <- fluidPage(
useShinyjs(),
actionButton("latex","Copy Latex to Clipboard"),
DT::dataTableOutput("table")
)
server <- function(input, output, session) {
output$table <- DT::renderDT(table)
observeEvent(input$latex,{
writeClipboard(paste0(capture.output(xtable(table))[-c(1:2)],collapse = "\n"))
shinyjs::alert("table copied to latex")
})
}
shinyApp(ui, server)
I won't recommend you to do it using DT's button. In order to do it using DT, there are at least 3 steps:
read entire table in the UI of datatable by writing Javascript in action, use Shiny.setInputValue to send the value from UI to server.
use R to parse the list(json) into data frame.
convert the data frame to latex string.
It's much easier to just do the conversion using the source data for datatable.
I have an interactive visualization that connects to a city government's police data API.
When certain combinations of inputs are selected, my API call comes back empty and I get a nasty red error message (as my plot inputs are unavailable).
Can someone tell me how to display a more informative error message along the lines of, "there are no incidents matching your selection, please try again"? I would like this error message to appear as a showNotification and my ggplot not to render.
Below is an extremely stripped down version of what I am doing. Note how when a combination like "AVONDALE" and "CHEMICAL IRRITANT" is selected, the chart renders, whereas when a combination like "ENGLISH WOODS" and "TASER-BEANBAG-PEPPERBALL-40MM FOAM" is selected, an error message is returned. This error message is what I would like to address with a showNotification alert.
Note that this uses the Socrata API, so the package RSocrata must be installed and loaded.
install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage("Example",
theme = shinytheme("united"),
tabPanel("Plot",
sidebarLayout(
sidebarPanel(
# neighborhood selector
selectizeInput("neighbSelect",
"Neighborhoods:",
choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS"),
multiple = FALSE)),
# incident description selector
selectizeInput("incSelect",
"Incident Type:",
choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"),
multiple = FALSE))
),
# Output plot
mainPanel(
plotlyOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
forceInput <- reactive({
forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))
})
# Render plot
output$plot <- renderPlotly({
ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thank you so much for any help anyone can offer!
Im going to give an example with the shinyalert library to have the popup. Here I added the sample choice TEST to indicate no data:
#install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)
library(shinyalert)
# Define UI for application that draws a histogram
ui <- fluidPage(
useShinyalert(),
navbarPage("Example",
theme = shinytheme("united"),
tabPanel("Plot",
sidebarLayout(
sidebarPanel(
# neighborhood selector
selectizeInput("neighbSelect",
"Neighborhoods:",
choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS","TEST"),
multiple = FALSE)),
# incident description selector
selectizeInput("incSelect",
"Incident Type:",
choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"),
multiple = FALSE))
),
# Output plot
mainPanel(
plotlyOutput("plot")
)
)
)
# Define server logic
server <- function(input, output,session) {
forceInput <- reactive({
forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))
if(nrow(forceInput)==0){
shinyalert("Oops!", "No data returned", type = "error")
forceInput <- NULL
}
forceInput
})
# Render plot
output$plot <- renderPlotly({
req(forceInput())
ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have a UI that is projectdashboard in R shiny. I want to be able to type in a text/search box and have the data associated with it show up as i type.
server <- function(input, output,session) {
output$ui_names = renderUI({
name_list = mydata()[,"names"]
pickerInput("name", label=h3(" Names:"),
choices = sort(unique(name_list)),options = list("actions-box" = TRUE,"live-search" = TRUE,"none-selected-text"='Select Names'),
selected = NULL,multiple = TRUE)
})
ui <- dashboardPage(
dashboardHeader(title=textOutput("title"),titleWidth = 1500),
dashboardSidebar(
uiOutput("ui_names")
)
shinyApp(ui = ui, server = server)
This however does not give me expected or working results. How can i put a text/searchbar in the dashboard side bar, that will 'live-search' the data i am feeding it.
you can use the following:
sidebarSearchForm(textId = "searchText", buttonId = "searchButton",label = "Search...")
Please check if this meet your requirements
Back again. Working on a project and I'm stuck. My click isn't working. I've tried every iteration and can't figure it out. Basically I want to select multiple lines in a datatable via a click, at which point I'll do some more filtering. The click I'm having issues with. Here's my code... Do you see anything I'm missing? Thanks.
library(forecast)
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(scales)
library(DT)
library(forecast)
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(scales)
library(DT)
source("NEW.R", local = TRUE)
branch1 <- unique(distinctlineitems$BRANCH)
ui <- navbarPage(
theme = shinytheme("cosmo"),
title = "EXPENDITURES",
tabPanel("TAB1",
sidebarLayout(
sidebarPanel(
checkboxGroupInput("branches",label = NULL,choices = branch1 ,selected = NULL),
actionButton('selectallB','Select All'),
textInput("words", "Search"),
h5("Separate keywords with commas."),
plotOutput("plot", width = "100%"),
plotOutput("season", width = "100%")),
# Show a plot of the generated distribution
mainPanel(
fluidRow(csvDownloadUI("dwnld", "DOWNLOAD"), style = "padding:10px"),
DT::dataTableOutput("table")
server <- function(input, output, session) {
branchfilter <- reactive({
filt <- distinctlineitems[distinctlineitems$BRANCH %in% input$branches,]
return(filt)
})
graphids <- reactive({
if(length(input$table_rows_selected) < 1) return(NULL)
id <- input$table_rows_selected
x <- branchfilter()$REMARKS[id]
})
output$table <- renderDataTable({
test <- DT::datatable(branchfilter(),
filter = "top",
rownames = FALSE,
selection = "multiple")
})
Turns out I was able to answer my own question on this one. Because I was trying to test it under a Reactive I was unable to see the output. In order to test, I had to wrap in an observe statement. So easy. After the fact. Thanks tobiaseli_te.
observe(print(graphids()))