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:
Related
Is there any way to retrieve all tab names in a tabsetpanel in Shiny?
for example a code that separately gives the name of the tabs in the tabsets tabs1 and tabs2:
library(shiny)
ui <- fluidPage(
tabsetPanel(id = "tabs1",
tabPanel("Tab1"),
tabPanel("Tab2"),
tabPanel("Tab3"),
tabPanel("Tab4"),
),
tabsetPanel(id = "tabs2",
tabPanel("T1"),
tabPanel("T2"),
tabPanel("T3"),
tabPanel("T4"),
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
We can use htmltools::tagQuery to achive this:
library(shiny)
library(htmltools)
ui <- fluidPage(
tabsetPanel(id = "tabs1",
tabPanel("Tab1"),
tabPanel("Tab2"),
tabPanel("Tab3"),
tabPanel("Tab4"),
),
tabsetPanel(id = "tabs2",
tabPanel("T1"),
tabPanel("T2"),
tabPanel("T3"),
tabPanel("T4"),
)
)
tabs1_panel_names <- sapply(tagQuery(ui)$find("#tabs1")$find("a")$selectedTags(), function(x){tagGetAttribute(x, "data-value")})
print(tabs1_panel_names)
tabs2_panel_names <- sapply(tagQuery(ui)$find("#tabs2")$find("a")$selectedTags(), function(x){tagGetAttribute(x, "data-value")})
print(tabs2_panel_names)
server <- function(input, output, session) {}
shinyApp(ui, server)
User new to Shiny here. It is a very easy mode. But it's not work and it doesn't show any errors.
The same result when I use subset function.
Here's my code:
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable", "Variable:",colnames(iris))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(input$variable>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
input$variable is a character you can use .data pronoun here to use it as column value.
library(dplyr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
varSelectInput("variable", "Variable:",iris)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(.data[[input$variable]]>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
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)
I setup the UI in server.R for more control, but shinyDashboard does not work when defined in server.R.
I use this method with navBarPage without problems.
This code works
library(shiny)
library(shinydashboard)
ui <- dashboardPage( dashboardHeader( ),
dashboardSidebar(),
dashboardBody() )
server <- shinyServer(function(input, output) { })
runApp(list(ui= ui, server = server))
But this one just show an empty page
ui <- uiOutput('dash')
server <- shinyServer(function(input, output) {
output$dash <- renderUI({
dashboardPage(dashboardHeader( ),
dashboardSidebar(),
dashboardBody() )
})
})
runApp(list(ui= ui, server = server))
This is an example using navBarPage, that works fine
ui <- uiOutput('nav')
server <- shinyServer(function(input, output) {
output$nav <- renderUI({
navbarPage("App Title",
tabPanel("Tab 1"),
tabPanel("Tab 2") )
})
})
runApp(list(ui= ui, server = server))
I don't think that you can use only a uiOutput to create a dashboard. I'm assuming that your goal is to create a dynamic dashboard. For that you need to define the header, body and side bar in your UI and use functions such as renderMenu on SERVER to create it. Here is an example to create a dashboard with all the UI defined in the SERVER.
ui <- dashboardPage(
dashboardHeader(title = "My Page"),
dashboardSidebar(sidebarMenuOutput("sideBar_menu_UI")),
dashboardBody(
uiOutput("body_UI"),
uiOutput("test_UI")
)
)
server <- shinyServer(function(input, output, session) {
output$sideBar_menu_UI <- renderMenu({
sidebarMenu(id = "sideBar_Menu",
menuItem("Menu 1", tabName="menu1_tab", icon = icon("calendar")),
menuItem("Menu 2", tabName="menu2_tab", icon = icon("database"))
)
})
output$test_UI <- renderUI ({
tabItems(
tabItem(tabName = "menu1_tab", uiOutput("menu1_UI")),
tabItem(tabName = "menu2_tab", uiOutput("menu2_UI"))
)
})
output$body_UI <- renderUI ({
p("Default content in body outsite any sidebar menus.")
})
output$menu1_UI <- renderUI ({
box("Menu 1 Content")
})
output$menu2_UI <- renderUI ({
box("Menu 2 Content")
})
})
runApp(list(ui= ui, server = server))
In this example, a menu for the sidebar is not selected by default and the content of body_UI will be visible all the time. If you want that your dashboard starts on a specific menu, put the sidebarMenu in your UI. Also you can delete the body_UI.
Below is the code I have written. I am not able to use formattable in my shiny. formattable helps in formatting the tables and improves the visualization also.
library("shinydashboard")
library("shiny")
library("formattable")
body <- dashboardBody(
fluidRow(
column(width = 12,
box(tableOutput(formattable(test.table, list())))
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)
server <- function(input, output) {
test.table <- data.frame(lapply(1:8, function(x) {1:10}))
output$table <- renderTable({test.table})
}
shinyApp(ui = ui, server = server)
you have to use renderFormattable, formattableOutput and formattable, all three for it to work
library("shinydashboard")
library("shiny")
library("formattable")
body <- dashboardBody(
fluidRow(
column(width = 12,
box(formattableOutput("table"))
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)
server <- function(input, output) {
test.table <- data.frame(lapply(1:8, function(x) {1:10}))
output$table <- renderFormattable({formattable(test.table, list())})
}
shinyApp(ui = ui, server = server)