I have the shiny app below with 2 actionButton(). I want when I press Datatable the Datatable2 to be disabled and when I click again on Datatable the Datatable2 to be available for pressing again.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("exc","Datatable"),
actionButton("exc2","Datatable2")
),
mainPanel(
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
This is really straightforward if you use the toggleState() function from the shinyjs package.
The help for that function gives you an extremely similar situation. In your case:
library(shiny)
ui <- fluidPage(
useShinyjs(), #this activates shinyjs
sidebarLayout(
sidebarPanel(
actionButton("exc","Datatable"),
actionButton("exc2","Datatable2")
),
mainPanel(
)
)
)
server <- function(input, output) {
observeEvent(input$exc, {
toggleState("exc2") #identify the element to toggle between active/inactive
})
}
shinyApp(ui = ui, server = server)
Related
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:
I am using below code and try to do below action.
Click on action button to go to next table. How can I do this?
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table"),
tableOutput("tbl1")
)),
fluidRow(box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
Here's one solution:
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table",
onclick = "location.href='#table2';"),
tableOutput("tbl1")
)),
fluidRow(id = "table2", box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
I've added a unique ID to the location in the UI - here the 2nd fluidRow, then added an onclick javascript function to the actionButton also in the UI. No server function means all the work is done by the user's browser which is handy sometimes.
You can add infinite complexity to the Javascript here to customise it to fit your needs.
There is a function in psych package called 'alpha' which gives out various statistics. I want a specific column from the output so I use a code. This code works perfectly in console but it doesn't work when I try to use it in shiny.
library(shiny)
library(mirt)#This contains a dataset called deAyala
library(psych)#This has the alpha() function
server<- shinyServer(
function(input, output) {
output$data <- renderUI({
alpha(deAyala,warnings=FALSE)$item.stats$raw.r #Warning disables the warnings
})
}
)
ui<- shinyUI(fluidPage(
titlePanel(title = h4("Output", align="center")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
uiOutput("data"),
)
)
))
shinyApp(ui = ui, server = server)
You can use renderTable or renderText to display the output instead of renderUI.
library(shiny)
server<- shinyServer(
function(input, output) {
output$data <- renderTable({
alpha(deAyala,warnings=FALSE)$item.stats$raw.r
})
}
)
ui<- shinyUI(fluidPage(
titlePanel(title = h4("Output", align="center")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
tableOutput("data"),
)
)
))
shinyApp(ui = ui, server = server)
I am building multiple lm() models using dplyr. I want to allow a user to change the independent variable value in a Shiny app - via shiny::sliderInput(). But only do so where "goodness of fit" say R^2 is greater than a threshold - otherwise disable the slider. I have tried to use the shinyjs::disable() function. See below, but can't get it to work. Any ideas on what I am doing wrong ?
library(shiny)
library(shinyjs)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("test","Nice number",min = 1,max = 50,value = 30)
),
mainPanel(
textOutput("valueText")
)
)
))
# Define server to disable slider if value selected
server <- shinyServer(function(input, output) {
value <- reactive(input$test)
output$valueText <- renderText(paste(value()))
#How to diasble slider?
reactive(if(value()==35){
shinyjs::disable('test')
}
)
})
# Run the application
shinyApp(ui = ui, server = server)
You have to call useShinyjs() in ui.R.
This is the code:
library(shiny)
library(shinyjs)
# Define UI for application that draws a histogram
ui <- shinyUI(
tagList(
useShinyjs(),
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("test","Nice number",min = 1,max = 50,value = 30)
),
mainPanel(
textOutput("valueText")
)
)
)
)
)
# Define server to disable slider if value selected
server <- shinyServer(function(input, output) {
value <- reactive(input$test)
output$valueText <- renderText(paste(value()))
#How to diasble slider?
observeEvent(value(), {
if(value()==35){
shinyjs::disable('test')
}
})
})
# Run the application
shinyApp(ui = ui, server = server)
I am trying to use conditionalPanel to display message when a file is being loaded. However, the panel is not disappearing once the condition is TRUE. I have created a reproducible code below:
server.R
library(shiny)
print("Loading start")
print(paste("1->",exists('FGram')))
FGram <- readRDS("data/UGram.rds")
print(paste("2->",exists('FGram')))
print("Loading end")
shinyServer( function(input, output, session) {
})
ui.R
library(shiny)
shinyUI( fluidPage(
sidebarLayout(
sidebarPanel(
h4("Side Panel")
)
),
mainPanel(
h4("Main Panel"),
br(),
textOutput("First Line of text.."),
br(),
conditionalPanel(condition = "exists('FGram')", HTML("PLEASE WAIT!! <br>App is loading, may take a while....")),
br(),
h4("Last Line of text..")
)
)
)
Conditions supplied to conditionalPanel are executed in the javascript environment, not the R environment, and therefore cannot reference or inspect variables or functions in the R environment. A solution to your situation would be to use uiOutput, as in the example below.
myGlobalVar <- 1
server <- function(input, output) {
output$condPanel <- renderUI({
if (exists('myGlobalVar'))
HTML("PLEASE WAIT!! <br>App is loading, may take a while....")
})
}
ui <- fluidPage({
uiOutput('condPanel')
})
shinyApp(ui=ui, server=server)