I know that using something like this code:
library(shiny)
ui <- fluidPage(
mainPanel(
textOutput("Query_String")
)
)
server <- function(input, output, session) {
observeEvent(session$clientData$url_search,{
Query <- session$clientData$url_search
output$Query_String <- renderText(Query)
# Long list of operations dependant on the parameters passed in the URL
})
}
shinyApp(ui = ui, server = server)
Can enable me to read parameters into my shiny app via URL queries. However it seems like I basically have to wrap my whole server content into one big observe() or observeEvent() to create the needed reactive context.
Is there any way to avoid this?
To avoid recalculating everything if just a single item changed, one wants to create multiple separate observes instead of just a big one. Long code should be refactored in functions e.g. process_query keeping the server function small, allowing to read the overall structure. Important intermediate results can be refactored in their own reactive values (here query). output$Query_String doesn't need to be nested in another reactive context.
process_query <- function(url) {
# many steps to process the url
return(url)
}
server <- function(input, output, session) {
query <- reactive(process_query(session$clientData$url_search))
output$Query_String <- renderText(query())
}
I dont know why this simple code isnt working.
I've a file named app.R and a folder named www with a image named "cat.jpeg" inside.
I tried to use this:
ui <- fluidPage(
h6("My cat:"),
tags$img(src = "cat.jpeg")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
In the output, my image just doest not appear!
If I change the "cat.jpeg" for a url link it works normally.
I want to display a dataframe in a table using grid.table(myDataFrame). I need help figuring out:
what output* and render* functions to use with shiny
what to write exactly in the render* function body
This far I have the following codes
In the UI.R, inside fluidPage and fluidRow:
dataTableOutput("TauxInsertion")
And then in Server.R:
output$TauxInsertion <- renderDataTable({
dataDepartement()
# TauxInsertionTable <- grid.table(dataDepartement())
# TauxInsertionTable
})
dataDepartement is a reactive variable that contains a dataFrame. Returning this data frame inside the renderDataTable gives me a table. But I need to be able to display the row names and add some color and style to the display. The commented part is what I have tried but doesn't display anything.
There are no significant messages in the console. I have also tried options(shiny.trace=TRUE) but to no avail.
I think you have to use functions dedicated to plot. Take a look
library(shiny)
library(grid)
library(gridExtra)
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlot({
grid.table(head(iris,3))
})
}
shinyApp(ui = ui, server = server)
I have a Shiny application and an R Script that I'd like to embed into my Shiny app. The script outputs a ggplot and I am not sure how to make it appear in my Shiny App. I've excluded some of the extraneous code. The script is successfully called, and the variables are stored in my workspace, but then nothing is displayed.
I've included the following in my server.R file:
##server.R##
source("heatmap.R", local=TRUE)
output$heatmap <- renderPlot ({
heatmapOutput
})
##ui.R##
shinyUI(fluidPage(
column(10, plotOutput("heatmap"))
),
Your output must be inside a shinyServer:
shinyServer(function(input, output) {
output$heatmap <- renderPlot ({ heatmapOutput })
}
I have made a shinydashboard app which has now quite an amount of lines of code, and I am wondering if there are ways to split the code into different . R files. I have seen a similar question here, but the answer does not help (especially it says nothing about the code in the server part of the app).
For the ui part, I have created functions called header, sidebar, and body, and then I merely write
ui <- dashboardPage(header(), sidebar(), body())
It works well, and it still works if the functions header, sidebar, and body need to have arguments.
For the server part, I don't think a similar strategy can be applied. I am wondering whether "local" server functions (one per menu item for instance) could be written, and then reunified into one central server function.
Do you think something like that is doable? More generally, thank you for your advice and ideas that could make my code more manageable.
I am not sure if this meets your requirement, you can create different files and do the required computations in those files and save all the objects (data frames or lists or literally anything) into .Rds file using saveRDS() in R and then load that file into server.R using loadRDS() which will have all your saved objects. You can find the documentation here.
Then simply use those objects by calling the names as they are saved earlier. Most of the complex Shiny apps use global.R file (just a general convention, you can use any name) to do the heavy computations and follow the above approach.
You can always use source to call other R files in server.R:
Use source as you usually do in regular R outside any reactive functions.
Use source("xxxxx", local=T) when you want to use it inside a reactive function so the r codes you called will run every time this piece of reactive codes are activated.
For the server side:
server.R:
library(shiny)
source('sub_server_functions.R')
function(input, output, session) {
subServerFunction1(input, output, session)
subServerFunction2(input, output, session)
subServerFunction3(input, output, session)
}
This has worked for me, it's possible you'll need to pass more variables to the subserver functions. But the scoping of the reactive output appears to allow this.
sub_server_functions.R:
subserverfunction1 <- function(input, output, session) {
output$checkboxGroupInput1 <- renderUI({
checkboxGroupInput('test1','test1',choices = c(1,2,3))
})
}
subserverfunction2 <- function(input, output, session) {
output$checkboxGroupInput2 <- renderUI({
checkboxGroupInput('test2','test2',choices = c(1,2,3))
})
}
subserverfunction3 <- function(input, output, session) {
output$checkboxGroupInput3 <- renderUI({
checkboxGroupInput('test3','test3',choices = c(1,2,3))
})
}