Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny: source command prints TRUE on app

Tags:

r

shiny

I am building a shiny application that should have different options for different user categories. Therefore, I have different subfiles where the inputs are definied and the dependent on the role the respective file is loaded via the source command.

It works, however, the source command always prints a TRUE on my shiny app. I can't get rid of it no matter what option of the source command I am trying.

Here a screenshot of the problem

And a minimal example: app.R

library(shiny)
library(ggplot2)

# Define UI for app that draws a histogram ----
ui <- fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarLayout(
        sidebarPanel(
            source("sub.R", echo = FALSE, print.eval = FALSE),
        ),
        mainPanel(
            plotOutput(outputId = "distPlot")
        )
    )
)

server <- function(input, output) {
    output$distPlot <- renderPlot({
        x    <- faithful$waiting
        ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
    })
}

shinyApp(ui = ui, server = server)

And the sourced file sub.R:

sliderInput(inputId = "bins",
            label = "Number of bins:",
            min = 0.1,
            max = 5,
            value = 1)

Thanks for any help in advance

like image 487
kain Avatar asked Oct 15 '25 03:10

kain


1 Answers

I found the fix here: add a [1] at the end of the source(.) command:

library(shiny)
library(ggplot2)

# Define UI for app that draws a histogram ----
ui <- fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarLayout(
        sidebarPanel(
            source("~/stackoverflow/17150062/sub.R", echo = FALSE, print.eval = FALSE)[1]
        ),
        mainPanel(
            plotOutput(outputId = "distPlot")
        )
    )
)

server <- function(input, output) {
    output$distPlot <- renderPlot({
        x    <- faithful$waiting
        ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
    })
}

shinyApp(ui = ui, server = server)

enter image description here

like image 102
r2evans Avatar answered Oct 17 '25 16:10

r2evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!