Can someone helps me out?
I already tried to look at different tutorials, and previous questions/answers on Stack. But nothing helped me out.
I am creating a Shiny app, which would show a different output (data table) based on the input value.
Here is my code so far:
library(shiny)
library(DT)
# Define UI for miles per gallon app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Clients per Township - Aggregation"),
# Sidebar panel for inputs ----
sidebarPanel(
helpText("Mean client's penetration (Number of Clients/Number of inhabitants) = 0.0089"),
selectInput("Choice", "Do you want to have a list of townships with client's penetration above or under the mean?", c(" ", "Above","Under"))),
# Main panel for displaying outputs ----
mainPanel(
conditionalPanel(
'input.Choice === "Above"',
DT::dataTableOutput("more_table")
),
conditionalPanel(
'input.Choice === "Under"',
DT::dataTableOutput("less_table")
)
)
)
# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
more_table = DT::renderDataTable({
more_than_mean
})
less_table = DT::renderDataTable({
less_than_mean
})
}
shinyApp(ui, server)
The data tables more_than_mean, and less_than_mean were previously computed.
When I run the app, I don't get an error. BUT no output is showed.
Can someone helps me out? I don't understand why none of the two tables are showed when I run the app.
Thank you!
So after looking carefully again at my code, I found my (stupid) mistake.
I only forgot the "ouput$" before the name of the output tables!
library(shiny)
library(DT)
# Define UI for miles per gallon app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Clients per Township - Aggregation"),
# Sidebar panel for inputs ----
sidebarPanel(
helpText("Mean client's penetration (Number of Clients/Number of inhabitants) = 0.0089"),
selectInput("Choice", "Do you want to have a list of townships with client's penetration above or under the mean?", c(" ", "Above","Under"))),
# Main panel for displaying outputs ----
mainPanel(
conditionalPanel(
'input.Choice === "Above"',
DT::dataTableOutput("more_table")
),
conditionalPanel(
'input.Choice === "Under"',
DT::dataTableOutput("less_table")
)
)
)
# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
output$more_table = DT::renderDataTable({
more_than_mean
})
output$less_table = DT::renderDataTable({
less_than_mean
})
}
shinyApp(ui, server)
Related
I'm building an application in which we have 2 fluidPage(). First fluidPage() have a data table with a hyperlink in one column, which gets linked with other fluidPage().
While looking for this scenario, I came upon this solution which links to another tabPanel().
I tried to create two fluidPage() like this
library(shiny)
library(DT)
server <- function(input, output) {
output$iris_type <- DT::renderDataTable({
datatable(data.frame(Species=paste0("<a href='#filtered_data'",
"alt='",unique(iris$Species),"'",
"onclick=\"",
"$('#filtered_data').trigger('change').trigger('shown');",
"Shiny.onInputChange('species', getAttribute('alt'));",
"\">",
unique(iris$Species),
"</a>")),
escape = FALSE)
})
output$filtered_data <- DT::renderDataTable({
if(is.null(input$species)){
datatable(iris)
}else{
datatable(iris[iris$Species %in% input$species, ])
}
})
}
ui <- shinyUI(fluidPage(
mainPanel(
tabsetPanel(
tabPanel("Iris Type", DT::dataTableOutput("iris_type"))
))
),
fluidPage(
mainPanel(
DT::dataTableOutput("filtered_data")
)
)
)
shinyApp(ui = ui, server = server)
I'm getting an error message
Error in shinyUI(fluidPage(mainPanel(tabsetPanel(tabPanel("Iris Type", :
unused argument (fluidPage(mainPanel(DT::dataTableOutput("filtered_data"))))
Can anyone provide a suitable solution where on clicking the specific species, the corresponding table should get displayed on another page rather than being displayed in another tab?
Thanks in advance!!!
UPDATE: Q2 has been answered. Thanks #Ben! Just looking for assistance with Q1 and selectize.
What I have: Using selectize the user can search a pension plan. Q1: How can I avoid a default plan and simply have a "enter here..."?
What I want: While the plan selection works fine, I'm trying to plot the age of plan members when that plan is selected. Q2: How can I make my output (age) reactive to the input (plan name)?
My ui.r:
fluidPage(
titlePanel("Atlantis Pension Dashboard"),
sidebarLayout(
sidebarPanel(
#Dynamic select type
selectizeInput('Plan_Name', 'Search plan by keyword', choices = unique(Active$Plan_Name)),
multiple = TRUE,
#Static drop down
selectInput('Plan_Name', "All plans",
choices=unique(Active$Plan_Name)),
hr(),
helpText("Data from...")
),
# Spot for hist
mainPanel(
plotOutput("agePlot")
)))
and my server.r:
function(input, output) {
Plan <- reactive(input$Plan_Name)
# Dynamic plan select
output$Plan_Name <- renderPrint({
str(sapply(sprintf('Plan_Name'), function(id) {
input[[id]]
}, simplify = FALSE))
})
# Age histogram dependant on the chosed plan
output$agePlot <- renderPlot({
req(Plan())
hist(Active[Plan()]$age)
}
)
}
What I've tried: damn near everything (being dumb doesn't help).
super new to shiny, have a problem that seems like it should be basic reactive programming but I haven't been able to find a solution that's worked so far.
Essentially, I want to take the user's selected input from the UI and paste it into a simple object in the server that will react/update when a new input is chosen.
The object will be concatenated into a full API call, and I wish to rerun the API call in the server with the reactive object updated each time a new input is chosen for it (note: the API cannot be run without an access code which is part of a corporate account, so apologies for my hesitance to put my full code but I just need help with this one functionality.)
In code below:
with Dollar General as the default selection in the selectInput, I would like the object, query, to be the character string "dollar%20general", and reactively change to "walmart" should Walmart be selected
Thanks!
ui <- fluidPage
sidebarLayout(
sidebarPanel(
selectInput("company", "Choose company:",
c("Dollar General" = "dollar%20general",
"Dollar Tree" = "dollar%20tree",
"Walmart" = "walmart"))
...
server <- function(input,output) {
...
query <- paste(input$company)
...
you can use reactiveValues() and observe. This should work:
library(shiny)
# Define UI for application
ui <- fluidPage(
# your input
sidebarLayout(
sidebarPanel(
selectInput("company", "Choose company:",
c("Dollar General" = "dollar%20general",
"Dollar Tree" = "dollar%20tree",
"Walmart" = "walmart"))
),
# Determine Output
mainPanel(
textOutput("showInput") # you need to render this in your server file
)
)
)
server <- function(input, output) {
# Show what was selected
query <- reactiveValues()
observe(
query$test <- paste(input$company, "and test", sep = " ")
)
output$showInput <- renderText({ #based on what you defined in the ui
query$test
})
}
# Run the application
shinyApp(ui = ui, server = server)
Create two files named ui.R and server.R store the UI logic in ui.R and backend/object logic in server.R. Below is the implementation.
UI file
# UI of app
ui <- fluidPage(
# input
sidebarLayout(
sidebarPanel(
selectInput("company", "Choose company:",
c("Dollar General" = "dollar%20general",
"Dollar Tree" = "dollar%20tree",
"Walmart" = "walmart"))
),
# Output
mainPanel(
textOutput("Input")
)
)
)
Server/Backend File
server <- function(input, output) {
# Show what was selected
output$Input <- renderText({ #based on what you defined in the ui
input$company
})
}
Now store these in a directory and then call runApp function.
~/newdir
|-- ui.R
|-- server.R
runApp("newdir")
I have an application which has 3 tabItems. I want to use a slider on second page to display same result on 3rd page interactively, i.e. if 2nd page slider changes then 3rd page slider should also change respectively.
I have a reactive function on server side
choose_segment <- reactive({
Multiple conditions for dropdown{Due to security cant share the exact code.}
})
and this choose_segment is refered in UI once and now i want to use it on the third page as well, but when i am calling the function on third page it is not displaying any thing on ui and also not giving any error.
in UI it is called inside UIoutput.
uiOutput(choose_segment())
My observations : I think as per my study we can not call one function directly twice, so what i am doing is i have made two different functions and calling same function from them, i.e.
output$chooseSegment1 <- renderUI({
choose_segment()
})
output$chooseSegment2 <- renderUI({
choose_segment()
})
Issue : it is giving me output but they both are not interactive :(
Kindly provide a solution so that i can make both the sliders work in interactive manner.
I have faced the same scenario, in that i was suppose to change the code structure.
I made dynamic output uiOutput to the Dropdown menu ob ui and then used the same in my server as Input$xyz in observe on server and it worked for me.
Code :
UI : column(3, selectInput(inputId="ABC",label= "Choose ABC"))
column(3, selectInput(inputId="ABC1",label= "Choose ABC"))
Server : observe({
if(is.null(tab2_summary())) return(NULL)
updateSelectInput(session, "ABC", value = input$ABC)
})
observe({
updateSelectInput(session, "ABC1", value = input$ABC)
})
observe({
updateSelectInput(session, "ABC", value = input$ABC1)
})
So this is how i was able to make the selectInput interactive on two different page.
For your reference there is one full reproducible code.
Kindly refer,
library(shiny)
# UI ----------------------------------------------------------
ui <- navbarPage("Navbar!",
tabPanel("Plot", sidebarLayout(sidebarPanel(
radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
selected = "speed"
)),
mainPanel( plotOutput("plot"),
textOutput("test2")))), # for input checking
tabPanel("Summary", sidebarLayout(sidebarPanel(
radioButtons("yaxis2", "grouping-var", c("speed"="speed", "dist"="dist")
)),
mainPanel(
verbatimTextOutput("summary"),
textOutput("test1")
)))
)
# Server ------------------------------------------
server <- function(input, output, session) {
observe({
x <- input$yaxis1
updateRadioButtons(session, "yaxis2", selected = x)
})
observe({
y <- input$yaxis2
updateRadioButtons(session, "yaxis1", selected = y)
})
# output$test1 <- renderPrint({cat("yaxis1", input$yaxis1)})
# output$test2 <- renderPrint({cat("yaxis2", input$yaxis2)})
# output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
# output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)
I Hope it will of your help.
I build an R/shiny web app. I want to have a multiple choice box (I use checkboxGroupInput(), but am open to alternatives). However, the list of choices is long and I want to contain it in a relatively small box of options (that shows 5-6 options at a time) with a scroll bar that enables to scroll through the entire list of choices.
Is there a way this can be done?
minimal example:
ui.R
library(shiny)
choices = paste("A",1:30,sep="_")
shinyUI(pageWithSidebar(
# Application title
headerPanel("my title"),
sidebarPanel(
checkboxGroupInput("inp", "choose any of the following", choices)
),
mainPanel(
tableOutput("result")
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
myInput <- reactive({
input$inp
})
output$result <- renderTable({
x = myInput()
if(length(x)==0) {
x = "No Choice Made"
}
matrix(x,ncol=1)
})
})
I found that using selectInput(..., multiple = TRUE) does the trick.