gvisGeoChart with shiny Dashboard - r

Unable to get output for givsGeoChart in my shiny Dashboard.
Below is the code for the same.
library(shiny)
library(shinydashboard)
library(googleVis)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
htmlOutput("Accidents")
)
)
server <- function(input, output) {
output$Accidents <- renderGvis({
gvisGeoChart(Dum, "States","Road_Accident",
options=list(region="IN",displayMode="regions",resolution="provinces",width="100%"))
})
}
shinyApp(ui, server)
The above code doesn't work.
GeoStates_IN <- gvisGeoChart(Dum, "States","Road_Accident",options=list(region="IN",displayMode="regions",resolution="provinces",width="100%"))
plot(GeoStates_IN)
whereas this code works.Unable to figure out what is missing in above code.
Any kind of help is appreciated.

Problem has something to do with your options() or data. Is your variable dum cotaining columns "States" and "Road_Accident". Have you included the data in server.R?
This works:
library(shiny)
library(shinydashboard)
library(googleVis)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
htmlOutput("Accidents")
)
)
server <- function(input, output) {
output$Accidents <- renderGvis({
data(Exports)
#map<-gvisGeoChart(Exports, "States","Road_Accident",
#options=list(region="IN",displayMode="regions",resolution="provinces",width="100%"))
map<-gvisGeoChart(Exports, locationvar='Country', colorvar='Profit',
options=list(projection="kavrayskiy-vii"))
return(map)
})
}
shinyApp(ui, server)

Related

Understanding R Shiny modules: Error in shiny::NS(id) : argument "id" is missing, with no default

I am trying to understand the concept of shiny modules. I am attempting to modularize this very simple shinydashboard app:
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(uiOutput("box"))
)
server <- function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
shinyApp(ui, server)
which works as desired.
However, when modularizing I keep getting the following mistake:
Error in shiny::NS(id) : argument "id" is missing, with no default.
#### Modules ####
costumizedValueBox <- function(id) {
ns <- shiny::NS(id)
uiOutput(ns("box"))
}
costumizedValueBoxServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
)
}
#### App ####
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox())
)
server <- function(input, output, session) {
costumizedValueBoxServer()
}
shinyApp(ui, server)
I have tried different modifications of the modularized code, none of which worked, e. g. changing the output (to textOutput) or the general setup. The error message suggests a problem with the NS function but I am not able to figure it out.
Thanks!
This is what #Limey describes in the comments: To learn in detail see here https://shiny.rstudio.com/articles/modules.html
library(shiny)
#### Modules ####
costumizedValueBox <- function(id, label = "ValueBox_custom") {
ns <- shiny::NS(id)
uiOutput(ns("box"))
}
costumizedValueBoxServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
)
}
#### App ####
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox("ValueBox_custom1"))
)
server <- function(input, output, session) {
costumizedValueBoxServer("ValueBox_custom1")
}
shinyApp(ui, server)
Modules (i.e., costumizedValueBoxServer and costumizedValueBox) are functions with a required argument.
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox("my_id")) # pass in an ID
)
server <- function(input, output, session) {
costumizedValueBoxServer("my_id") # pass in the same ID
}
Yeah just when you call your module, make sure to assign an id for it in quotes. Job well done, I've been learning about this too. Make sure your id matches your module calls in both server and UI sections of your app.
Here is a youtube link, you've probably already seen, but the timestamp shows what I am talking about.
https://www.youtube.com/watch?v=oOYaHsPXLvs
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox("VBox_id_1")) # pass in an ID
)
server <- function(input, output, session) {
costumizedValueBoxServer("VBox_id_1") # pass in the same ID
}```

Add subtitle in shinydashboard title section

Is it possible to add a subtitle with smaller font size in shinydashboard title section under the main title?
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title="Dashboard"),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)
Something like this would work:
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(
title= div(h3('Títutlo', style="margin: 0;"), h4('subtitulo', style="margin: 0;"))
),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)

Shiny seesion object is not found when trying to use shinyJS()

In the shiny app below Im trying to use shinyJS() to hide and display text but I get:
Error: shinyjs: could not find the Shiny session object. This usually happens when a shinyjs function is called from a context that wasn't set up by a Shiny session.
Do not bother that dataset does not exist its just an example
## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Biodiversity"),
dashboardSidebar(
actionButton("action","Submit")
),
dashboardBody(
useShinyjs(),
show(
div(id='text_div',
verbatimTextOutput("text")
)
),
uiOutput("help_text"),
plotlyOutput("plot")
)
)
server <- function(input, output) {
output$help_text <- renderUI({
HTML("<b>Click 'Show plot' to show the plot.</b>")
})
react<-eventReactive(input$action,{
hide("help_text")
omited <-subset(omited, omited$scientificName %in% isolate(input$sci)&omited$verbatimScientificName %in% isolate(input$ver))
})
}
shinyApp(ui = ui, server = server)
You can't use show() in the ui, these functions are used in the server. Remove that and it works. Sample:
## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Biodiversity"),
dashboardSidebar(
actionButton("action","Submit")
),
dashboardBody(
useShinyjs(),
div(id='text_div',
verbatimTextOutput("text")
)
,
uiOutput("help_text"),
plotOutput("plot")
)
)
server <- function(input, output) {
output$help_text <- renderUI({
HTML("<b>Click 'Show plot' to show the plot.</b>")
})
observeEvent(input$action,{
hide("help_text")
output$plot <- renderPlot({
plot(1)
})
})}
shinyApp(ui = ui, server = server)
Output:

How to upload RData from ShinyFiles

I want to upload RData files using ShinyFiles, but I don´t know how to do it.
This is not working for me:
library(shiny)
library(shinydashboard)
library(shinyFiles)
# Define UI for application that draws a histogram
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
shinyFilesButton('files', label='File select', title='Please select a file', multiple=FALSE),
verbatimTextOutput("txt")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
shinyFileChoose(input, 'files', root=c(root='/'), filetypes=c('', 'RData'))
output$txt <- renderPrint(
ls(parseFilePaths(roots= "/",selection = input$files))
)
}
# Run the application
shinyApp(ui = ui, server = server)
What´s wrong? I really dont understand how parseFilesPaths really works, because it get the route to the file, but I can not make it working. Also I have tried with
files< - load(parseFilePaths(roots= "/",selection = input$files)$type)
But it also didnt work...
Thansk!!
I have solved it. Here the solution (hope this will be helpfull for others)
# Define UI for application that draws a histogram
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
shinyFilesButton('files', 'File select', 'Please select a file', FALSE),
verbatimTextOutput('filepaths')
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
roots = c(wd='/home/developer/')
reactivity <- reactive({
shinyFileChoose(input, 'files', roots=roots, filetypes=c('', 'RData'))
ruta <- parseFilePaths(roots, input$files)$datapath
load(ruta)
return(parseFilePaths(roots, input$files))
})
output$filepaths <- renderPrint({
reactivity()
})
}
# Run the application
shinyApp(ui = ui, server = server)

Passing dynamic input and updating visual in R shiny

The script below when executed creates a process_map() with a select input. Upon selecting a resource like "r1","r2" etc. we get the corresponding process map. However I want to dynamically pass an input from the selectInput bar and update the process map within R shiny page Snapshot for your reference. Please help.
## app.R ##
install.packages("bupaR")
install.packages("edeaR")
install.packages("eventdataR")
install.packages("processmapR")
install.packages("processmonitR")
install.packages("xesreadR")
install.packages("petrinetR")
install.packages("shiny")
install.packages("shinydashboard")
library(shiny)
library(shinydashboard)
library(bupaR)
library(edeaR)
library(eventdataR)
library(processmapR)
library(processmonitR)
library(xesreadR)
library(petrinetR)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("resources","Select the resource", c("r1","r2","r3","r4","r5"),
selected = "r1",selectize = T)
),
dashboardBody(
filter_resource(patients,resources = c("r1","r2","r4"), reverse = F) %>%
process_map()
))
server <- function(input, output) {
}
shinyApp(ui, server)
You could do
library(shiny)
library(shinydashboard)
library(bupaR)
library(edeaR)
library(eventdataR)
library(processmapR)
library(processmonitR)
library(xesreadR)
library(petrinetR)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("resources","Select the resource", c("r1","r2","r3","r4","r5"),selected = "r1",selectize = T)
),
dashboardBody(
uiOutput("ui")
))
server <- function(input, output) {
output$ui <- renderUI({
r <- input$resources
tagList(filter_resource(patients,resources = r, reverse = F) %>% process_map())
})
}
shinyApp(ui, server)

Resources